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.
The following arithmetic operators are supported on all primitive numeric types:
The following bitwise operators are supported on all primitive integer types:
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’s promotion system makes arithmetic operations on mixtures of argument types “just work” naturally and automatically. See Conversion and Promotion for details of the promotion system.
Here are some examples with bitwise operators:
julia> ~123
-124
julia> 123 & 234
106
julia> 123 | 234
251
julia> 123 $ 234
145
julia> ~uint32(123)
0xffffff84
julia> ~uint8(123)
0x84
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:
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
Integers are compared in the standard manner — by comparison of bits. Floating-point numbers are compared according to the IEEE 754 standard:
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 NaNs to be equal to each other:
julia> isequal(NaN,NaN)
true
Mixed-type comparisons between signed integers, unsigned integers, and floats can be very tricky. A great deal of care has been taken to ensure that Julia does them correctly.
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. Chained numeric comparisons use the & operator, which allows them to work on arrays. For example, 0 < A < 1 gives a boolean array whose entries are true where the corresponding elements of A are between 0 and 1.
Note the evaluation behavior of chained comparisons:
v(x) = (println(x); x)
julia> v(1) > v(2) <= v(3)
2
1
3
false
The middle expression is only evaluated once, rather than twice as it would be if the expression were written as v(1) > v(2) & v(2) <= v(3). However, the order of evaluations in a chained comparison is undefined. It is strongly recommended not to use expressions with side effects (such as printing) in chained comparisons. If side effects are required, the short-circuit && operator should be used explicitly (see Short-Circuit Evaluation).
Julia 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.
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. In order to compute trigonometric functions with degrees instead of radians, suffix the function with d. For example, sind(x) computes the sine of x where x is specified in degrees.
For notational convenience, there are equivalent operator forms for the rem and pow functions:
In the former case, the spelled-out rem operator is the “canonical” form, and the % operator form is retained for compatibility with other systems, whereas in the latter case, the ^ operator form is canonical and the spelled-out pow form is retained for compatibility. 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