Julia Mathematical Functions
Julia provides a set of efficient, portable standard mathematical functions.
* * *
## Numeric Comparison
The following table lists the functions used for numeric comparison:
| Function | Tests whether the following property is satisfied |
| --- | --- |
| `isequal(x, y)` | whether x and y are identical in value and type |
| `isfinite(x)` | whether x is a finite number |
| `isinf(x)` | whether x is (positive/negative) infinity |
| `isnan(x)` | whether x is NaN |
isequal considers NaN values to be equal:
## Example
julia>isequal(NaN, NaN)
true
julia>isequal(, )
true
julia>isequal(NaN, NaN32)
true
isequal can also be used to distinguish signed zeros:
## Example
julia> -0.0 == 0.0
true
julia>isequal(-0.0, 0.0)
false
Other function examples:
## Example
julia> isfinite(5)
true
julia> isfinite(NaN32)
false
* * *
## Rounding Functions
The following table lists the rounding functions supported by Julia:
| Function | Description | Return Type |
| --- | --- | --- |
| `round(x)` | round x to the nearest integer | `typeof(x)` |
| `round(T, x)` | round x to the nearest integer | `T` |
| `floor(x)` | round x towards `-Inf` | `typeof(x)` |
| `floor(T, x)` | round x towards `-Inf` | `T` |
| `ceil(x)` | round x towards `+Inf` | `typeof(x)` |
| `ceil(T, x)` | round x towards `+Inf` | `T` |
| `trunc(x)` | round x towards 0 | `typeof(x)` |
| `trunc(T, x)` | round x towards 0 | `T` |
## Example
julia>round(3.8)
4.0
julia>round(Int, 3.8)
4
julia> floor(3.8)
3.0
julia> floor(Int, 3.8)
3
julia> ceil(3.8)
4.0
julia> ceil(Int, 3.8)
4
julia> trunc(3.8)
3.0
julia> trunc(Int, 3.8)
3
* * *
## Division Functions
The following table lists the division functions supported by Julia:
| Function | Description |
| --- | --- |
| `div(x,y)`, `xΓ·y` | truncated division; for any type of division, the result truncates the decimal portion, leaving only the integer part, with the quotient rounded towards zero |
| `fld(x,y)` | floor division; quotient rounded towards `-Inf` |
| `cld(x,y)` | ceiling division; quotient rounded towards `+Inf` |
| `rem(x,y)` | remainder; satisfies `x == div(x,y)*y + rem(x,y)`; sign matches x |
| `mod(x,y)` | modulo; satisfies `x == fld(x,y)*y + mod(x,y)`; sign matches y |
| `mod1(x,y)` | offset-1 mod; if y>0, returns rβ(0,y]; if y<0, rβ[y,0) and satisfies `mod(r, y) == mod(x, y)` |
| `mod2pi(x)` | modulo 2pi; `0 <= mod2pi(x) div(11, 4)
2
julia> div(7, 4)
1
julia> fld(11, 4)
2
julia> fld(-5,3)
-2
julia> fld(7.5,3.3)
2.0
julia> cld(7.5,3.3)
3.0
julia> mod(5, 0:2)
2
julia> mod(3, 0:2)
0
julia> mod(8.9,2)
0.9000000000000004
julia> rem(8,4)
0
julia> rem(9,4)
1
julia> mod2pi(7*pi/5)
4.39822971502571
julia>divrem(8,3)
(2, 2)
julia> fldmod(12,4)
(3, 0)
julia> fldmod(13,4)
(3, 1)
julia> mod1(5,4)
1
julia> gcd(6,0)
6
julia> gcd(1//3,2//3)
1//3
julia> lcm(1//3,2//3)
2//3
* * *
## Sign and Absolute Value Functions
The following table lists the sign and absolute value functions supported by Julia:
| Function | Description |
| --- | --- |
| `abs(x)` | absolute value of x |
| `abs2(x)` | squared absolute value of x |
| `sign(x)` | sign of x, returns -1, 0, or +1 |
| `signbit(x)` | sign bit, returns true or false |
| `copys
YouTip