Julia Mathematical Functions
Julia provides a set of efficient, portable standard mathematical functions.
Numeric Comparison Functions
The following table lists the functions used for numeric comparison:
| function | Test whether the following property holds |
|---|---|
isequal(x, y) |
x The difference between y Whether they are exactly identical in value and type |
isfinite(x) |
x Whether it is a finite number |
isinf(x) |
x Whether it is (positive/negative) infinity |
isnan(x) |
x Whether it is NaN |
isapprox(x, y) |
x The difference between y Whether approximately equal (operator ≈) |
isequal considers NaN values to be equal to each other, unlike the == operator:
Example
julia
isequal
(
NaN, NaN
)
true
julia
NaN == NaN
false
julia
isequal
(
[
1
NaN
]
,
[
1
NaN
]
)
true
julia
[
1
NaN
]
==
[
1
NaN
]
false
isequal can also be used to distinguish signed zeros:
Example
julia
0.0
==
0.0
true
julia
isequal
(
0.0
,
0.0
)
false
isapprox is used for approximate comparison of floating-point numbers. Due to floating-point error, directly comparing two floating-point numbers with == often does not behave as expected:
Example
julia
isfinite
(
5
)
true
julia
isfinite
(
NaN32
)
false
julia
0.1
0.2
==
0.3
false
julia
0.1
0.2
≈
0.3
true
Rounding Functions
The following table lists the rounding functions supported by Julia:
| function | Description | Return Type |
|---|---|---|
round(x) |
x Round to the nearest integer |
typeof(x) |
round(T, x) |
x Round to the nearest integer |
T |
floor(x) |
x towards -Inf rounding |
typeof(x) |
floor(T, x) |
x towards -Inf rounding |
T |
ceil(x) |
x towards +Inf round in the direction |
typeof(x) |
ceil(T, x) |
x towards +Inf round in the direction |
T |
trunc(x) |
x Round towards 0 |
typeof(x) |
trunc(T, x) |
x Round 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
Note:floor(3.8) without a type argument returns 3.0 of type Float64; only when a type is specified, floor(Int, 3.8), does it return the integer 3. This differs from the behavior of Python's int().
Division Functions
The following table lists the division functions supported by Julia:
| function | Description |
|---|---|
div(x,y), x÷y |
Truncating division, quotient rounded towards zero |
fld(x,y) |
Floor division; quotient towards -Inf approximately |
cld(x,y) |
Ceiling division; quotient towards +Inf approximately |
rem(x,y) |
Remainder; satisfies x == div(x,y)*y + rem(x,y); sign same as x consistent |
mod(x,y) |
Modulus; satisfies x == fld(x,y)*y + mod(x,y); sign same as y consistent |
mod1(x,y) |
offset by 1 mod; if y>0, return r∈(0,y], if y<0, then r∈[y,0) and satisfies mod(r, y) == mod(x, y) |
mod2pi(x) |
modulo 2pi;0 <= mod2pi(x) < 2pi |
divrem(x,y) |
Return (div(x,y),rem(x,y)) |
fldmod(x,y) |
Return (fld(x,y),mod(x,y)) |
gcd(x,y...) |
x, ygreatest common divisor of ,... |
lcm(x,y...) |
x, yleast common multiple of ,... |
Example
julia
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
(
13
,
4
)
(
3
,
1
)
julia
mod1
(
5
,
4
)
1
julia
gcd
(
6
,
0
)
6
julia
lcm
(
4
,
6
)
12
Sign and Absolute Value Functions
The following table lists the sign and absolute value functions supported by Julia:
| function | Description |
|---|---|
abs(x) |
x magnitude of |
abs2(x) |
x square of the magnitude of |
sign(x) |
indicates x sign of, returns -1, 0, or +1 |
signbit(x) |
indicates whether the sign bit is true or false |
copysign(x,y) |
returns a number whose value equals x the magnitude of, with the sign of y consistent |
flipsign(x,y) |
returns a number whose value equals x the magnitude of, with the sign of x*y consistent |
Example
julia
abs
(
7
)
7
julia
abs
(
5
+3im
)
5.830951894845301
julia
abs2
(
7
)
49
julia
abs2
(
5
+3im
)
34
julia
copysign
(
5
,-
10
)
5
julia
copysign
(
5
,
10
)
5
julia
sign
(
5
)
1
julia
sign
(
5
)
1
julia
signbit
(
5
)
true
julia
signbit
(
5
)
false
julia
flipsign
(
5
,
10
)
5
julia
flipsign
(
5
,-
10
)
5
Power, Logarithm, and Root Functions
The following table lists the elementary mathematical functions supported by Julia:
| function | Description |
|---|---|
sqrt(x), √x |
x 's square root |
cbrt(x), ∛x |
x 's cube root |
hypot(x,y) |
When the lengths of the legs are x and y the length of the hypotenuse of the right triangle |
exp(x) |
the natural exponential function at x the value at |
expm1(x) |
When x when close to 0 exp(x)-1 the exact value of |
ldexp(x,n) |
x*2^n an efficient algorithm for,n is an integer |
log(x) |
x 's natural logarithm |
log(b,x) |
Starts with b with base x logarithm of |
log2(x) |
base 2 x logarithm of |
log10(x) |
base 10 x logarithm of |
log1p(x) |
When x when close to 0 log(1+x) the exact value of |
exponent(x) |
x binary exponent of |
significand(x) |
Float (floating point number) x binary significand (that is, the mantissa) of |
Example
julia
sqrt
(
49
)
7.0
Taking the square root of a negative real number raises an error; you need to pass a complex number to get a complex result
julia
sqrt
(
49
)
ERROR: DomainError with -
49.0
:
sqrt was called with a negative real argument but will only
return
a complex result
if
called with a complex argument.
Try
sqrt
(
Complex
(
x
)
)
.
Stacktrace
:
[
...
]
julia
sqrt
(
49
- 0im
)
0.0
- 7.0im
julia
cbrt
(
8
)
2.0
julia
cbrt
(
8
)
2.0
julia
hypot
(
3
,
4
)
5.0
julia
exp
(
5.0
)
148.4131591025766
julia
expm1
(
1.0
)
1.718281828459045
julia
ldexp
(
4.0
,
2
)
16.0
julia
log
(
4
)
1.3862943611198906
julia
log
(
4
,
2
)
0.5
julia
log2
(
4
)
2.0
julia
log10
(
4
)
0.6020599913279624
julia
log1p
(
4
)
1.6094379124341003
julia
exponent
(
6.8
)
2
julia
significand
(
15.2
)
8
15.2
Note:expm1 and log1p are specialized for the case where x is very close to 0. In that case exp(x)-1 or log(1+x) would lose precision due to floating-point error, while these two functions give accurate results.
Trigonometric and Hyperbolic Functions
Julia also provides all standard trigonometric and hyperbolic functions:
sin cos tan cot sec csc
sinh cosh tanh coth sech csch
asin acos atan acot asec acsc
asinh acosh atanh acoth asech acsch
sinc cosc
In the figure below, an angle measured in radians corresponds to a point on the unit circle whose coordinates define the sine and cosine of the angle.

Example
julia
pi
π =
3.1415926535897
...
julia
sin
(
0
)
0.0
julia
sin
(
pi/
6
)
0.49999999999999994
julia
sin
(
pi/
4
)
0.7071067811865475
julia
cos
(
0
)
1.0
julia
cos
(
pi/
6
)
0.8660254037844387
julia
cos
(
pi/
3
)
0.5000000000000001
All the functions provided above are single-argument functions, though atan can also take two arguments to represent the traditional atan2 function:
atan(y)
atan(y, x)
Compute the arctangent of y or y/x respectively. The two-argument version can determine the correct quadrant from the signs of x and y:
Example
julia
theta = 3pi/
4
2.356194490192345
julia
x, y =
(
cos
(
theta
)
, sin
(
theta
)
)
(
0.7071067811865475
,
0.7071067811865476
)
julia
atan
(
y/x
)
0.7853981633974484
julia
atan
(
y, x
)
2.356194490192345
In addition, sinpi(x) and cospi(x) are used to compute sin(pix) and cos(pix) more accurately.
Degree-Based Trigonometric Functions
The arguments of the functions above are all in radians. To compute trigonometric functions in degrees, just add the d suffix, for example sind(x) computes the sine of x degrees:
sind cosd tand cotd secd cscd
asind acosd atand acotd asecd acscd
Example
In cos(56), 56 is in radians
julia
cos
(
56
)
0.853220107722584
In cosd(56), 56 is in degrees
julia
cosd
(
56
)
0.5591929034707468
julia
sind
(
90
)
1.0