Numeric types

Numeric types

First, let’s focus on different and most basic (and useful for daily use) numerical types of objects in Julia.

Type

Desc.

Smallest number

Largest number

Int8

signed integers

\(-2^7\)

\(2^7-1\)

UInt8

unsigned integers (read: natural numbers)

\(0\)

\(2^8\)

Int16

signed integers

\(-2^{15}\)

\(2^{15}-1\)

UInt16

unsigned integers (read: natural numbers)

\(0\)

\(2^{16}\)

Int64

signed integers

\(-2^{63}\)

\(2^{63}-1\)

UInt64

unsigned integers (read: natural numbers)

\(0\)

\(2^{64}\)

There is also an alias Int for integers with the default number of bits for your architecture (32 or 64).

x = 1
1
typeof(x)
Int64

We can explicitly force Julia to set the precision of our integers:

α = Int8(2)
2
Int8(2^7-1)
127

However, we have to remember about the ranges of used types:

Int8(2^8) #Error message
InexactError: trunc(Int8, 256)

Stacktrace:
 [1] throw_inexacterror(f::Symbol, #unused#::Type{Int8}, val::Int64)
   @ Core ./boot.jl:602
 [2] checked_trunc_sint
   @ ./boot.jl:624 [inlined]
 [3] toInt8
   @ ./boot.jl:639 [inlined]
 [4] Int8(x::Int64)
   @ Core ./boot.jl:749
 [5] top-level scope
   @ In[5]:1
 [6] eval
   @ ./boot.jl:360 [inlined]
 [7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1094

Unsigned integers are not super useful due to their default hexadecimal notation:

@show UInt(1018);
UInt(1018) = 0x00000000000003fa

Another type of numbers in Julia are floating-point values

Type

Number of bits

Float16

16

Float32

32

Float64

64

Similarly to integers, float refers to floating-point values with the default number of bits for your architecture (32 or 64).

w = .2
0.2
typeof(w)
Float64

Basic arithmetic operations

On the surface, arithmetic operations (+, -, *, /) in Julia are very similar to the ones known from such languages as Matlab:

x = 1
y = 2
x + y
3

For multiplication, we do not have to use * for unambiguous cases. 2.2*x and 2.2x will give the same results:

x = 1.5
2.2x 
3.3000000000000003

However, I do not recommend it because in my opinion it makes the code a bit less legible. Besides, there are cases when it does not work:

πx
UndefVarError: πx not defined

Stacktrace:
 [1] top-level scope
   @ :0
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1094

For presenting results of the operation macro @show can be quite useful:

x = 1
y = 2
@show x + y + .2
x + y + 0.2 = 3.2
3.2
x = 1
y = 2
println("x + y + .2 is equal to $(x + y + .2)")
x + y + .2 is equal to 3.2

You can make several assignments in one line using separator ,. Then, instead of three lines:

x = 12.3;
y = 1;
🤩 = -10;

you can just use one line:

x, y, 🤩 = 12.3, 1, -10
(12.3, 1, -10)
@show 🤩
@show x
@show y
@show y, x
🤩 = -10
x = 12.3
y = 1
(y, x) = (1, 12.3)
(1, 12.3)

Operations (+, -, *, /, \, ÷, %, ^) where one object is on both sides of the assignment operator (e.g., x=x+1) can be shortened:

@show x;
@show x += 1;
@show x *= 2;
@show x ^= 2;
x = 12.3
x += 1 = 13.3
x *= 2 = 26.6
x ^= 2 = 707.5600000000001

If we combine two different numeric types, Julia will try to coerce the less general object to more general one:

x = Int(42)
y = float(.964)
@show z = x+y

typeof(z)
z = x + y = 42.964
Float64

You can use also Lisp-like syntax (which we find in R as well. Sometimes this can be very useful, and other times, it can be quite scary):

+(x, y)
42.964
@show  ^(*( 3, +(1, 2) ), 5)
(3 * (1 + 2)) ^ 5 = 59049
59049

There are several ways to divide numbers. The most natural one is by using /:

@show 1/2 
1 / 2 = 0.5
0.5

If for some reason, we want to use common fractions instead of decimals, then we have to use // instead:

1//2
1//2

Within common fractions, we can perform arithmetic operations:

@show 1//2+1//3;
1 // 2 + 1 // 3 = 5//6
@show (1//2)^10;
(1 // 2) ^ 10 = 1//1024

But due to coercion in operations over different types, we might lose it:

@show 1//2 + .25;
@show 1//2 + π;
1 // 2 + 0.25 = 0.75
1 // 2 + π = 3.641592653589793

We perform integer division by using div or ÷ (to get this symbol type \div and <tab>):

@show div(5,3);
@show   ÷(5,3);
@show     5÷3;
div(5, 3) = 1
5 ÷ 3 = 1
5 ÷ 3 = 1

We get remainder by using rem or %:

@show rem(5,3);
@show   %(5,3);
@show     5%3;
rem(5, 3) = 2
5 % 3 = 2
5 % 3 = 2