1.1. Julia as a calculator#
1.1.1. Arithmetics#
The first thing to try in Julia is the evaluation of arithmetic expressions:
123.4 + 234.5 # This is a comment, ignored by Julia
357.9
7 / 3 # Integer division returns floating point
2.3333333333333335
7 ÷ 3 # Truncated integer division (type \div and press tab)
2
7 % 3 # Remainder
1
2^7 - 4 # All integer expression which returns an integer
124
3 * (4.2 - 9.7) # Use parentheses to control the order of operations
-16.499999999999996
# We will later learn why the previous result is not exact
5.6e7 ^ 0.1 # Scientific notation, means 5.6⋅10⁷ (not e⁷)
5.954136718963118
1.1.2. Multiple lines in a cell#
Note that if you have many lines in a cell, the Julia notebook only displays the output of the last:
1 + 2
34 / 3
11.333333333333334
You can suppress the output by ending the line with a semi-colon, and you can use the
println
function to get more control over what is shown in the output, including descriptive text:
println(1 + 2.1)
println("The result is ", -123 / 1000000)
34 / 3; # The output will not be displayed
3.1
The result is -0.000123
1.1.3. Primitive numeric data types#
The previous examples showed that Julia often automatically chooses the right data type for numbers (e.g. integers or floating point numbers)
However, it is often useful to know what types are used, and to choose them manually
Use the
typeof
function to find the type of a value:
typeof(1.23)
Float64
typeof(-745)
Int64
typeof(1e20)
Float64
1.1.3.1. Integer types#
These are Julia’s so-called primitive integer types:
Type |
Signed? |
Number of bits |
Smallest value |
Largest value |
---|---|---|---|---|
|
✓ |
8 |
-2^7 |
2^7 - 1 |
|
|
8 |
0 |
2^8 - 1 |
|
✓ |
16 |
-2^15 |
2^15 - 1 |
|
|
16 |
0 |
2^16 - 1 |
|
✓ |
32 |
-2^31 |
2^31 - 1 |
|
|
32 |
0 |
2^32 - 1 |
|
✓ |
64 |
-2^63 |
2^63 - 1 |
|
|
64 |
0 |
2^64 - 1 |
|
✓ |
128 |
-2^127 |
2^127 - 1 |
|
|
128 |
0 |
2^128 - 1 |
|
N/A |
8 |
|
|
You can find the minimum and maximum representable values of primitive numeric types using the typemin
and typemax
functions:
println(typemin(Int64))
println(typemax(Int64))
println(typemin(Int128))
println(typemax(Int128))
-9223372036854775808
9223372036854775807
-170141183460469231731687303715884105728
170141183460469231731687303715884105727
By default, Julia on a 64-bit system uses Int64
for integers. This can result in overflow for large numbers. Consider for example the computation of \(1000^7\):
1000^7
3875820019684212736
which is clearly completely wrong. But this particular number can be represented by an Int128
(or floating-point types):
Int128(1000)^7
1000000000000000000000
1.1.3.2. Floating point types#
These are Julia’s primitive floating-point types:
Type |
Precision |
Number of bits |
---|---|---|
|
half |
16 |
|
single |
32 |
|
double |
64 |
We will learn more about the term precision later on, but for now it is enough to know that
The default floating point type is
Float64
, or double precisionDouble precision represents about 16 decimal digits of a number
The largest and smallest double precision positive numbers are about \(10^{308}\) and \(10^{-308}\)
1.1.4. Arithmetic operations and order of operations#
Expression |
Name |
Description |
---|---|---|
|
unary plus |
the identity operation |
|
unary minus |
maps values to their additive inverses |
|
binary plus |
performs addition |
|
binary minus |
performs subtraction |
|
times |
performs multiplication |
|
divide |
performs division |
|
integer divide |
x / y, truncated to an integer |
|
inverse divide |
equivalent to |
|
power |
raises |
|
remainder |
equivalent to |
Order of operations:
Parentheses
Power
Multiplication / division
Addition / subtraction
Operators with the same precedence are evaluated from left to right (except exponentiation)
For example:
360 / 2*pi
evaluates to180 * pi
Use parentheses if you want
360 / (2*pi)