Julia Math Functions
Julia provides a set of efficient, portable standard math functions.
Numeric Comparison
The following table lists the functions used for numeric comparison:
| Function | Test whether the following property is satisfied |
|---|---|
isequal(x, y) |
xandyWhether the value and type are exactly the same |
isfinite(x) |
xWhether it is a finite number |
isinf(x) |
xWhether it is (positive/negative) infinity |
isnan(x) |
xWhether it isNaN |
isequal considers NaN values as equal:
Example
true
julia> isequal([1 NaN], [1 NaN])
true
julia> isequal(NaN, NaN32)
true
isequal can also be used to distinguish signed zeros:
Example
true
julia> isequal(-0.0, 0.0)
false
Examples of other functions:
Example
true
julia> isfinite(NaN32)
false
Rounding Functions
The following table lists the rounding functions supported by Julia:
| Function | Description | Return Type |
|---|---|---|
round(x) |
xRound to the nearest integer |
typeof(x) |
round(T, x) |
xRound to the nearest integer |
T |
floor(x) |
xtoward-InfRounding |
typeof(x) |
floor(T, x) |
xtoward-InfRounding |
T |
ceil(x) |
xtoward+InfDirectional rounding |
typeof(x) |
ceil(T, x) |
xtoward+InfDirectional rounding |
T |
trunc(x) |
xRound toward zero |
typeof(x) |
trunc(T, x) |
xRound toward zero |
T |
Example
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: regardless of type, the result of division omits the fractional part, leaving the integer part, and the quotient approximates toward zero. |
fld(x,y) |
Floor division; the quotient toward-Infapproximates |
cld(x,y) |
Ceiling division; the quotient toward+Infapproximates |
rem(x,y) |
Remainder; satisfiesx == div(x,y)*y + rem(x,y); the sign is consistent withxconsistent |
mod(x,y) |
Modulo; satisfiesx == fld(x,y)*y + mod(x,y); the sign is consistent withyconsistent |
mod1(x,y) |
Offset by 1mod; ify>0, then returnr∈(0,y], ify<0, thenr∈[y,0)and satisfiesmod(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
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) |
xthe modulus of |
abs2(x) |
xthe square of the modulus of |
sign(x) |
representsxthe 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 is equal toxthe modulus of , and the sign is consistent withyconsistent |
flipsign(x,y) |
returns a number whose value is equal toxthe modulus of , and the sign is consistent withx*yconsistent |
Example
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
Sign and Absolute Value Functions
The following table lists the sign and absolute value functions supported by Julia:
| Function | Description |
|---|---|
sqrt(x), √x |
xthe square root of |
cbrt(x), ∛x |
xthe cube root of |
hypot(x,y) |
when the lengths of the legs arexandy, the length of the hypotenuse of a right triangle |
exp(x) |
the natural exponential function atxthe value at |
expm1(x) |
Whenxwhen close to 0, theexp(x)-1exact value of |
ldexp(x,n) |
x*2^nefficient algorithm for ,nbeing an integer |
log(x) |
xthe natural logarithm of |
log(b,x) |
withbas the basexthe logarithm of |
log2(x) |
Base 2xlogarithm of |
log10(x) |
Base 10xlogarithm of |
log1p(x) |
Whenxwhen close to 0, thelog(1+x)exact value of |
exponent(x) |
xthe binary exponent of |
significand(x) |
floating-point numberxthe binary significand (that is, the mantissa) of |
Example
7.0
julia> sqrt(-49)
ERROR: DomainError with -49.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
[1] throw_complex_domainerror(::Symbol, ::Float64) at .\math.jl:33
[2] sqrt at .\math.jl:573 [inlined]
[3] sqrt(::Int64) at .\math.jl:599
[4] top-level scope at REPL[43]:1
julia> cbrt(8)
2.0
julia> cbrt(-8)
-2.0
julia> a = Int64(5)^10;
julia> hypot(a, a)
1.3810679320049757e7
julia> exp(5.0)
148.4131591025766
julia> expm1(10)
22025.465794806718
julia> expm1(1.0)
1.718281828459045
julia> ldexp(4.0, 2)
16.0
julia> log(5,2)
0.43067655807339306
julia> log(4,2)
0.5
julia> log(4)
1.3862943611198906
julia> log2(4)
2.0
julia> log10(4)
0.6020599913279624
julia> log1p(4)
1.6094379124341003
julia> log1p(-2)
ERROR: DomainError with -2.0:
log1p will only return a complex result if called with a complex argument. Try log1p(Complex(x)).
Stacktrace:
[1] throw_complex_domainerror(::Symbol, ::Float64) at .\math.jl:33
[2] log1p(::Float64) at .\special\log.jl:356
[3] log1p(::Int64) at .\special\log.jl:395
[4] top-level scope at REPL[65]:1
julia> exponent(6.8)
2
julia> significand(15.2)/10.2
0.18627450980392157
julia> significand(15.2)*8
15.2
Trigonometric and Hyperbolic Functions
Julia also provides all the 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 in radians corresponds to a point on the unit circle, whose coordinates define the sine and cosine of the angle.

Example
π = 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, but atan can also accept two parameters to represent the traditional atan2 function.
atan(y) atan(y, x)
Compute the arctangent of y or y/x, respectively.
Example
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(pi*x) and cos(pi*x) more accurately, respectively.
To compute trigonometric functions in degrees rather than radians, use d as a suffix. For example, sind(x) computes the sine of x, where x is an angle in degrees. The following is a complete list of trigonometric functions for angle variables:
sind cosd tand cotd secd cscd asind acosd atand acotd asecd acscd
Example
0.853220107722584
julia> cosd(56)
0.5591929034707468