Integers and Floating-Point Numbers

In any programming language, there are two basic building blocks of arithmetic and computation. They are integers and floating-point values. Built-in representation of the values of integers and floating-point are called numeric primitives. On the other hand, their representation as immediate values in code are called numeric literals.
Following are the example of integer and floating-point literals −
100 is an integer literal
100 is an integer literal
100.50 is a floating-point literal
100.50 is a floating-point literal
Their built-in memory representations as objects is numeric primitives.
Their built-in memory representations as objects is numeric primitives.
Integer is one of the primitive numeric types in Julia. It is represented as follows −
julia> 100
100

julia> 123456789
123456789

We can check the default type of an integer literal, which depends on whether our system is 32-bit or 64-bit architecture.
julia> Sys.WORD_SIZE
64

julia> typeof(100)
Int64

The table given below shows the integer types in Julia −
In Julia, if the maximum representable value of a given type exceeds, then it results in a wraparound behavior. For example −
julia> A = typemax(Int64)
9223372036854775807

julia> A + 1
-9223372036854775808

julia> A + 1 == typemin(Int64)
true

It is recommended to explicitly check for wraparound produced by overflow especially where overflow is possible. Otherwise use BigInt type in Arbitrary Precision Arithmetic.
Below is an example of overflow behavior and how we can resolve it −
julia> 10^19
-8446744073709551616

julia> big(10)^19
10000000000000000000

Integer division throws a DivideError in the following two exceptional cases −
Dividing by zero
Dividing by zero
Dividing the lowest negative number
Dividing the lowest negative number
The rem (remainder) and mod (modulus) functions will throw a DivideError whenever their second argument is zero. The example are given below −
julia> mod(1, 0)
ERROR: DivideError: integer division error
Stacktrace:
 [1] div at .\int.jl:260 [inlined]
 [2] div at .\div.jl:217 [inlined]
 [3] div at .\div.jl:262 [inlined]
 [4] fld at .\div.jl:228 [inlined]
 [5] mod(::Int64, ::Int64) at .\int.jl:252
 [6] top-level scope at REPL[52]:1
 
 
julia> rem(1, 0)
ERROR: DivideError: integer division error
Stacktrace:
 [1] rem(::Int64, ::Int64) at .\int.jl:261
 [2] top-level scope at REPL[54]:1

Another primitive numeric types in Julia is floating-point numbers. It is represented (using E-notation when needed) as follows −
julia> 1.0
1.0

julia> 0.5
0.5

julia> -1.256
-1.256

julia> 2e11
2.0e11

julia> 3.6e-5
3.6e-5

All the above results are Float64. If we would like to enter Float32 literal, they can be written by writing f in the place of e as follows −
julia> 0.5f-5
5.0f-6

julia> typeof(ans)
Float32

julia> 1.5f0
1.5f0

julia> typeof(ans)
Float32

The table given below shows the floating-point types in Julia −
There are two kind of floating-point zeros, one is positive zero and other is negative zero. They are same but their binary representation is different. It can be seen in the example below −
julia> 0.0 == -0.0
true

julia> bitstring(0.0)
"0000000000000000000000000000000000000000000000000000000000000000"

julia> bitstring(-0.0)
"1000000000000000000000000000000000000000000000000000000000000000"

The table below represents three specified standard floating-point values. These floating-point values do not correspond to any point on the real number line.
We can also apply typemin and typemax functions as follows −
julia> (typemin(Float16),typemax(Float16))
(-Inf16, Inf16)

julia> (typemin(Float32),typemax(Float32))
(-Inf32, Inf32)

julia> (typemin(Float64),typemax(Float64))
(-Inf, Inf)

Machine epsilon is the distance between two adjacent representable floating-point numbers. It is important to know machine epsilon because most of the real numbers cannot be represented exactly with floating-point numbers.
In Julia, we have eps() function that gives us the distance between 1.0 and the next larger representable floating-point value. The example is given below −
julia> eps(Float32)
1.1920929f-7

julia> eps(Float64)
2.220446049250313e-16

As we know that the number should be rounded to an appropriate representable value if it does not have an exact floating-point representation. Julia uses the default mode called RoundNearest. It rounds to the nearest integer, with ties being rounded to the nearest even integer. For example,
julia> BigFloat("1.510564889",2,RoundNearest)
1.5


    73 Lectures 
     4 hours 

 Lemuel Ogbunude

    24 Lectures 
     3 hours 

 Mohammad Nauman

    29 Lectures 
     2.5 hours 

 Stone River ELearning
 Print
 Add Notes
 Bookmark this page