Mathematical Operations - BenoitKnecht/julia GitHub Wiki
Julia provides a complete collection of basic arithmetic and bitwise operators across all of its numeric primitive types, as well as providing portable, efficient implementations of a comprehensive collection of standard mathematical functions.
## Arithmetic and Bitwise OperatorsThe following arithmetic operators are supported on all primitive numeric types:
-
+x
— unary plus is the identity operation. -
-x
— unary minus maps values to their additive inverses. -
x + y
— binary plus performs addition. -
x - y
— binary minus performs subtraction. -
x * y
— times performs multiplication. -
x / y
— divide performs division.
The following bitwise operators are supported on all primitive integer types:
-
~x
— bitwise not. -
x & y
— bitwise and. -
x | y
— bitwise or. -
x $ y
— bitwise xor. -
x >>> y
— logical shift right. -
x >> y
— arithmetic shift right. -
x << y
— logical/arithmetic shift left.
Here are some simple examples using arithmetic operators:
julia> 1 + 2 + 3
6
julia> 1 - 2
-1
julia> 3*2/12
0.5
(By convention, we tend to space less tightly binding operators less tightly, but there are no syntactic constraints.)
Julia has a type promotion system that allows arithmetic operations on mixtures of argument types to "just work" naturally and automatically (see Conversion and Promotion for details of the promotion system):
julia> 1 + 2.5
3.5
julia> 0.5*12
6.0
julia> 3*2/12 + 1
1.5
The above expressions all promote to Float64
.
However, more nuanced promotions also work:
julia> uint8(12) - int8(15)
-3
julia> typeof(ans)
Int16
julia> uint8(12) - float32(1.5)
10.5
julia> typeof(ans)
Float32
Here are some examples with bitwise operators:
julia> ~123
-124
julia> ~uint32(123)
4294967172
julia> ~uint8(123)
132
julia> 123 & 234
106
julia> 123 | 234
251
julia> typeof(ans)
Int64
julia> uint8(123) | uint16(234)
251
julia> typeof(ans)
Uint16
julia> 123 $ 234
145
As a general rule of thumb, arguments are promoted to the smallest type that can accurately represent all of the arguments.
Every binary arithmetic and bitwise operator also has an updating version that assigns the result of the operation back into its left operand.
For example, the updating form of +
is the +=
operator.
Writing x += 3
is equivalent to writing x = x + 3
:
julia> x = 1
1
julia> x += 3
4
julia> x
4
The updating versions of all the binary arithmetic and bitwise operators are:
+= -= *= /= &= |= $= >>>= >>= <<=
Standard comparison operations are defined for all the primitive numeric types:
-
==
— equality. -
!=
— inequality. -
<
— less than. -
<=
— less than or equal to. -
>
— greater than. -
>=
— greater than or equal to.
Here are some simple examples:
julia> 1 == 1
true
julia> 1 == 2
false
julia> 1 != 2
true
julia> 1 == 1.0
true
julia> 1 < 2
true
julia> 1.0 > 3
false
julia> 1 >= 1.0
true
julia> -1 <= 1
true
julia> -1 <= -1
true
julia> -1 <= -2
false
julia> 3 < -0.5
false
As is evident here, promotion also applies to comparisons: the comparisons are performed in whatever type the arguments are promoted to, which is generally the smallest type in which the values can be faithfully represented.
After promotion to a common type, integers are compared in the standard manner: by comparison of bits. Floating-point numbers are compared according to the IEEE 754 standard:
- finite numbers are ordered in the usual manner
-
Inf
is equal to itself and greater than everything else exceptNaN
-
-Inf
is equal to itself and less then everything else exceptNaN
-
NaN
is not equal to, less than, or greater than anything, including itself.
The last point is potentially suprprising and thus worth noting:
julia> NaN == NaN
false
julia> NaN != NaN
true
julia> NaN < NaN
false
julia> NaN > NaN
false
For situations where one wants to compare floating-point values so that NaN
equals NaN
, such as hash key comparisons, the function isequal
is also provided, which considers NaN
s to be equal to each other:
julia> isequal(NaN,NaN)
true
Unlike most languages, with the notable exception of Python, comparisons can be arbitrarily chained:
julia> 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
true
Chaining comparisons is often quite convenient in numerical code. Only as many initial comparisons and their operand expressions as are necessary to determine the final truth value of the entire chain are evaluated. See Short-Circuit Evaluation for further discussion of this behavior.
## Mathematical FunctionsJulia provides a comprehensive collection of mathematical functions and operators. These mathematical operations are defined over as broad a class of numerical values as permit sensible definitions, including integers, floating-point numbers, rationals, and complexes, wherever such definitions make sense.
-
round(x)
— roundx
to the nearest integer. -
iround(x)
— roundx
to the nearest integer, giving an integer-typed result. -
floor(x)
— roundx
towards-Inf
. -
ceil(x)
— roundx
towards+Inf
. -
trunc(x)
— roundx
towards zero. -
itrunc(x)
— roundx
towards zero, giving an integer-typed result. -
div(x,y)
— truncated division; quotient rounded towards zero. -
fld(x,y)
— floored division; quotient rounded towards-Inf
. -
rem(x,y)
— remainder; satisfiesx == div(x,y)*y + rem(x,y)
, implying that sign matchesx
. -
mod(x,y)
— modulus; satisfiesx == fld(x,y)*y + mod(x,y)
, implying that sign matchesy
. -
gcd(x,y...)
— greatest common divisor ofx
,y
... with sign matchingx
. -
lcm(x,y...)
— least common multiple ofx
,y
... with sign matchingx
. -
abs(x)
— a positive value with the magnitude ofx
. -
abs2(x)
— the squared magnitude ofx
. -
sign(x)
— indicates the sign ofx
, returning -1, 0, or +1. -
signbit(x)
— indicates the sign bit ofx
, returning -1 or +1. -
copysign(x,y)
— a value with the magnitude ofx
and the sign ofy
. -
sqrt(x)
— the square root ofx
. -
cbrt(x)
— the cube root ofx
. -
hypot(x,y)
— accuratesqrt(x^2 + y^2)
for all values ofx
andy
. -
pow(x,y)
—x
raised to the exponenty
. -
exp(x)
— the natural exponential function atx
. -
expm1(x)
— accurateexp(x)-1
forx
near zero. -
ldexp(x,n)
—x*2^n
computed efficiently for integraln
. -
log(x)
— the natural logarithm ofx
. -
log(b,x)
— the baseb
logarithm ofx
. -
log2(x)
— the base 2 logarithm ofx
. -
log10(x)
— the base 10 logarithm ofx
. -
log1p(x)
— accuratelog(1+x)
forx
near zero. -
logb(x)
— returns the binary exponent ofx
. -
erf(x)
— the error function atx
. -
erfc(x)
— accurate1-erf(x)
for largex
. -
gamma(x)
— the gamma function atx
. -
lgamma(x)
— accuratelog(gamma(x))
for largex
.
For an overview of why functions like hypot
, expm1
, log1p
, and erfc
are necessary and useful, see John D. Cook's excellent pair of blog posts on the subject:
expm1, log1p, erfc, and hypot.
All the standard trigonometric functions are also defined:
sin cos tan cot sec csc
sinh cosh tanh coth sech csch
asin acos atan acot asec acsc
acoth asech acsch sinc cosc atan2
These are all single-argument functions, with the exception of atan2
, which gives the angle in radians between the x-axis and the point specified by its arguments, interpreted as x and y coordinates.
For notational convenience, there are equivalent operator forms for the mod
and pow
functions:
-
x % y
is equivalent tomod(x,y)
. -
x ^ y
is equivalent topow(x,y)
.
Like arithmetic and bitwise operators, %
and ^
also have updating forms.
As with other operators, x %= y
means x = x % y
and x ^= y
means x = x^y
:
julia> x = 2; x ^= 5; x
32
julia> x = 7; x %= 4; x
3
Previous: Integers and Floating-Point Numbers — Next: Complex and Rational Numbers
© 2010-2011 Stefan Karpinski, Jeff Bezanson, Viral Shah, Alan Edelman.
The Julia Manual — All Rights Reserved.