”;
Let us try to understand basic mathematical functions with the help of example in this chapter.
Numerical Conversions
In Julia, the user gets three different forms of numerical conversion. All the three differ in their handling of inexact conversions. They are as follows −
T(x) or convert(T, x) − This notation converts x to a value of T. The result depends upon following two cases −
-
T is a floating-point type − In this case the result will be the nearest representable value. This value could be positive or negative infinity.
-
T is an integer type − The result will raise an InexactError if and only if x is not representable by T.
x%T − This notation will convert an integer x to a value of integer type T corresponding to x modulo 2^n. Here n represents the number of bits in T. In simple words, this notation truncates the binary representation to fit.
Rounding functions − This notation takes a type T as an optional argument for calculation. Eg − Round(Int, a) is shorthand for Int(round(a)).
Example
The example given below represent the various forms described above −
julia> Int8(110) 110 julia> Int8(128) ERROR: InexactError: trunc(Int8, 128) Stacktrace: [1] throw_inexacterror(::Symbol, ::Type{Int8}, ::Int64) at .boot.jl:558 [2] checked_trunc_sint at .boot.jl:580 [inlined] [3] toInt8 at .boot.jl:595 [inlined] [4] Int8(::Int64) at .boot.jl:705 [5] top-level scope at REPL[4]:1 julia> Int8(110.0) 110 julia> Int8(3.14) ERROR: InexactError: Int8(3.14) Stacktrace: [1] Int8(::Float64) at .float.jl:689 [2] top-level scope at REPL[6]:1 julia> Int8(128.0) ERROR: InexactError: Int8(128.0) Stacktrace: [1] Int8(::Float64) at .float.jl:689 [2] top-level scope at REPL[7]:1 julia> 110%Int8 110 julia> 128%Int8 -128 julia> round(Int8, 110.35) 110 julia> round(Int8, 127.52) ERROR: InexactError: trunc(Int8, 128.0) Stacktrace: [1] trunc at .float.jl:682 [inlined] [2] round(::Type{Int8}, ::Float64) at .float.jl:367 [3] top-level scope at REPL[14]:1
Rounding functions
Following table shows rounding functions that are supported on Julia’s primitive numeric types −
Function | Description | Return type |
---|---|---|
round(x) | This function will round x to the nearest integer. | typeof(x) |
round(T, x) | This function will round x to the nearest integer. | T |
floor(x) | This function will round x towards -Inf returns the nearest integral value of the same type as x. This value will be less than or equal to x. | typeof(x) |
floor(T, x) | This function will round x towards -Inf and converts the result to type T. It will throw an InexactError if the value is not representable. | T |
floor(T, x) | This function will round x towards -Inf and converts the result to type T. It will throw an InexactError if the value is not representable. | T |
ceil(x) | This function will round x towards +Inf and returns the nearest integral value of the same type as x. This value will be greater than or equal to x. | typeof(x) |
ceil(T, x) | This function will round x towards +Inf and converts the result to type T. It will throw an InexactError if the value is not representable. | T |
trunc(x) | This function will round x towards zero and returns the nearest integral value of the same type as x. The absolute value will be less than or equal to x. | typeof(x) |
trunc(T, x) | This function will round x towards zero and converts the result to type T. It will throw an InexactError if the value is not representable. | T |
Example
The example given below represent the rounding functions −
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
Following table shows the division functions that are supported on Julia’s primitive numeric types −
Sl.No | Function & Description |
---|---|
1 |
div(x,y), x÷y
It is the quotation from Euclidean division. Also called truncated division. It computes x/y and the quotient will be rounded towards zero.
|
2 |
fld(x,y)
It is the floored division. The quotient will be rounded towards -Inf i.e. largest integer less than or equal to x/y. It is shorthand for div(x, y, RoundDown).
|
3 |
cld(x,y)
It is ceiling division. The quotient will be rounded towards +Inf i.e. smallest integer less than or equal to x/y. It is shorthand for div(x, y, RoundUp).
|
4 |
rem(x,y)
remainder; satisfies x == div(x,y)*y + rem(x,y); sign matches x
|
5 |
mod(x,y)
It is modulus after flooring division. This function satisfies the equation x == fld(x,y)*y + mod(x,y). The sign matches y.
|
6 |
mod1(x,y)
This is same as mod with offset 1. It returns r∈(0,y] for y>0 or r∈[y,0) for y<0, where mod(r, y) == mod(x, y).
|
7 |
mod2pi(x)
It is modulus with respect to 2pi. It satisfies 0 <= mod2pi(x) < 2pi
|
8 |
divrem(x,y)
It is the quotient and remainder from Euclidean division. It equivalents to (div(x,y),rem(x,y)).
|
9 |
fldmod(x,y)
It is the floored quotation and modulus after division. It is equivalent to (fld(x,y),mod(x,y))
|
10 |
gcd(x,y…)
It is the greatest positive common divisor of x, y,…
|
11 |
lcm(x,y…)
It represents the least positive common multiple of x, y,…
|
Example
The example given below represent the division functions −
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
Following table shows the sign and absolute value functions that are supported on Julia’s primitive numeric types −
Sl.No | Function & Function |
---|---|
1 |
abs(x)
It the absolute value of x. It returns a positive value with the magnitude of x.
|
2 |
abs2(x)
It returns the squared absolute value of x.
|
3 |
sign(x)
This function indicates the sign of x. It will return -1, 0, or +1.
|
4 |
signbit(x)
This function indicates whether the sign bit is on (true) or off (false). In simple words, it will return true if the value of the sign of x is -ve, otherwise it will return false.
|
5 |
copysign(x,y)
It returns a value Z which has the magnitude of x and the same sign as y.
|
6 |
flipsign(x,y)
It returns a value with the magnitude of x and the sign of x*y. The sign will be flipped if y is negative. Example: abs(x) = flipsign(x,x).
|
Example
The example given below represent the sign and absolute value functions −
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, Logs, and Roots
Following table shows the Power, Logs, and Root functions that are supported on Julia’s primitive numeric types −
Sl.No | Function & Description |
---|---|
1 |
sqrt(x), √x
It will return the square root of x. For negative real arguments, it will throw DomainError.
|
2 |
cbrt(x), ∛x
It will return the cube root of x. It also accepts the negative values.
|
3 |
hypot(x,y)
It will compute the hypotenuse √|𝑥|2+|𝑦|2of right-angled triangle with other sides of length x and y. It is an implementation of an improved algorithm for hypot(a,b) by Carlos and F.Borges.
|
4 |
exp(x)
It will compute the natural base exponential of x i.e. 𝑒𝑥
|
5 |
expm1(x)
It will accurately compute 𝑒𝑥−1 for x near zero.
|
6 |
ldexp(x,n)
It will compute 𝑋 ∗ 2𝑛 efficiently for integer values of n.
|
7 |
log(x)
It will compute the natural logarithm of x. For negative real arguments, it will throw DomainError.
|
8 |
log(b,x)
It will compute the base b logarithm of x. For negative real arguments, it will throw DomainError.
|
9 |
log2(x)
It will compute the base 2 logarithm of x. For negative real arguments, it will throw DomainError.
|
10 |
log10(x)
It will compute the base 10 logarithm of x. For negative real arguments, it will throw DomainError.
|
11 |
log1p(x)
It will accurately compute the log(1+x) for x near zero. For negative real arguments, it will throw DomainError.
|
12 |
exponent(x)
It will calculate the binary exponent of x.
|
13 |
significand(x)
It will extract the binary significand (a.k.a. mantissa) of a floating-point number x in binary representation. If x = non-zero finite number, it will return a number of the same type on the interval [1,2), else x will be returned.
|
Example
The example given below represent the Power, Logs, and Roots functions −
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 .speciallog.jl:356 [3] log1p(::Int64) at .speciallog.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
Following is the list of 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
Julia also provides two additional functions namely sinpi(x) and cospi(x) for accurately computing sin(pi*x) and cos(pi*x).
If you want to compute the trigonometric functions with degrees, then suffix the functions with d as follows −
sind cosd tand cotd secd cscd asind acosd atand acotd asecd acscd
Some of the example are given below −
julia> cos(56) 0.853220107722584 julia> cosd(56) 0.5591929034707468
”;