Easy Tutorial
❮ Julia File Julia Tutorial ❯

Julia Mathematical Functions

Julia provides a set of efficient and portable standard mathematical functions.


Numerical Comparison

The following table lists functions used for numerical comparison:

Function Tests if the following property holds
isequal(x, y) Whether x and y are identical in both 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 NaNs to be equal:

Example

julia> isequal(NaN, NaN)
true

julia> isequal([1 NaN], [1 NaN])
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 rounding functions supported by Julia:

Function Description Return Type
round(x) Rounds x to the nearest integer typeof(x)
round(T, x) Rounds x to the nearest integer T
floor(x) Rounds x towards -Inf typeof(x)
floor(T, x) Rounds x towards -Inf T
ceil(x) Rounds x towards +Inf typeof(x)
ceil(T, x) Rounds x towards +Inf T
trunc(x) Rounds x towards 0 typeof(x)
trunc(T, x) Rounds 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 division functions supported by Julia:

Function Description
div(x,y), x÷y Truncated division; the result is truncated towards zero
fld(x,y) Floor division; the result is truncated towards -Inf
cld(x,y) Ceiling division; the result is truncated towards +Inf
rem(x,y) Remainder; satisfies x == div(x,y)*y + rem(x,y); sign matches x
mod(x,y) Modulus; satisfies x == fld(x,y)*y + mod(x,y); sign matches y
mod1(x,y) Offset mod by 1; returns r ∈ (0, y] if y > 0, r ∈ [y, 0) if y < 0, and satisfies mod(r, y) == mod(x, y)
mod2pi(x) Modulus with 2pi; 0 <= mod2pi(x) < 2pi
divrem(x,y) Returns (div(x,y), rem(x,y))
fldmod(x,y) Returns (fld(x,y), mod(x,y))
gcd(x,y...) Greatest common divisor of x, y, ...
lcm(x,y...) Least common multiple of x, y, ...

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(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 table below lists the symbolic and absolute value functions supported by Julia:

Function Description
abs(x) The magnitude of x
abs2(x) The square of the magnitude of x
sign(x) Indicates the sign of x, returns -1, 0, or +1
signbit(x) Indicates whether the sign bit is true or false
copysign(x,y) Returns a number with the magnitude of x and the sign of y
flipsign(x,y) Returns a number with the magnitude of x and the sign of x*y

Examples

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

Symbolic and Absolute Value Functions

The table below lists the symbolic and absolute value functions supported by Julia:

Function Description
sqrt(x), √x The square root of x
cbrt(x), ∛x The cube root of x
hypot(x,y) The length of the hypotenuse of a right-angled triangle with legs of length x and y
exp(x) The natural exponential function at x
expm1(x) The exact value of exp(x)-1 for x close to 0
ldexp(x,n) Efficient computation of x*2^n, where n is an integer
log(x) The natural logarithm of x
log(b,x) The logarithm of x to base b
log2(x) The logarithm of x to base 2
log10(x) The logarithm of x to base 10
log1p(x) The exact value of log(1+x) for x close to 0
exponent(x) The binary exponent of x
significand(x) The binary significand (or mantissa) of the floating-point number x

Examples

julia> sqrt(49)
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

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 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 diagram below, the angle in radians corresponds to a point on the unit circle, whose coordinates define the sine and cosine of the angle.

Examples

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

The functions provided above are all single-parameter functions, although atan can also accept two parameters to represent the traditional atan2 function.

atan(y)
atan(y, x)

These compute the arctangent of y or y/x, respectively.

Examples

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

Additionally, sinpi(x) and cospi(x) are used for more accurate computation of sin(pi*x) and cos(pi*x), respectively.

To compute trigonometric functions of angles instead of radians, use the d suffix. For example, sind(x) computes the sine of x, where x is an angle. Here is the complete list of trigonometric functions with angle variables:

sind   cosd   tand   cotd   secd   cscd
asind  acosd  atand  acotd  asecd  acscd

Examples

julia> cos(56)
0.853220107722584

julia> cosd(56)
0.5591929034707468
❮ Julia File Julia Tutorial ❯