- Julia Tutorial
- Julia - Home
- Julia - Overview
- Julia - Environment Setup
- Julia - Basic Syntax
- Julia - Arrays
- Julia - Tuples
- Integers & Floating-Point Numbers
- Julia - Rational & Complex Numbers
- Julia - Basic Operators
- Basic Mathematical Functions
- Julia - Strings
- Julia - Functions
- Julia - Flow Control
- Julia - Dictionaries & Sets
- Julia - Date & Time
- Julia - Files I/O
- Julia - Metaprogramming
- Julia - Plotting
- Julia - Data Frames
- Working with Datasets
- Julia - Modules and Packages
- Working with Graphics
- Julia - Networking
- Julia - Databases
- Julia Useful Resources
- Julia - Quick Guide
- Julia - Useful Resources
- Julia - Discussion

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.50 is a floating-point literal

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 −

Type | Signed? | Number of bits | Smallest value | Largest value |
---|---|---|---|---|

Int8 | ✓ | 8 | -2^7 | 2^7 – 1 |

UInt8 | 8 | 0 | 2^8 – 1 | |

Int16 | ✓ | 16 | -2^15 | 2^15 – 1 |

UInt16 | 16 | 0 | 2^16 – 1 | |

Int32 | ✓ | 32 | -2^31 | 2^31 – 1 |

UInt32 | 32 | 0 | 2^32 – 1 | |

Int64 | ✓ | 64 | -2^63 | 2^63 – 1 |

UInt64 | 64 | 0 | 2^64 – 1 | |

Int128 | ✓ | 128 | -2^127 | 2^127 – 1 |

UInt128 | 128 | 0 | 2^128 – 1 | |

Bool | N/A | 8 | false (0) | true (1) |

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 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 −

Type | Precision | Number of bits |
---|---|---|

Float16 | half | 16 |

Float32 | single | 32 |

Float64 | double | 64 |

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.

Float16 | Float32 | Float64 | Name | Description |
---|---|---|---|---|

Inf16 | Inf32 | Inf | positive infinity | It is the value greater than all finite floating-point values |

-Inf16 | -Inf32 | -Inf | negative infinity | It is the value less than all finite floating-point values |

NaN16 | NaN32 | NaN | not a number | It is a value not == to any floating-point value (including itself) |

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

Advertisements