- Julia - Discussion
- Julia - Useful Resources
- Julia - Quick Guide
- Julia - Databases
- Julia - Networking
- Working with Graphics
- Julia - Modules and Packages
- Working with Datasets
- Julia - Data Frames
- Julia - Plotting
- Julia - Metaprogramming
- Julia - Files I/O
- Julia - Date & Time
- Julia - Dictionaries & Sets
- Julia - Flow Control
- Julia - Functions
- Julia - Strings
- Basic Mathematical Functions
- Julia - Basic Operators
- Julia - Rational & Complex Numbers
- Integers & Floating-Point Numbers
- Julia - Tuples
- Julia - Arrays
- Julia - Basic Syntax
- Julia - Environment Setup
- Julia - Overview
- Julia - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pterals.
Following are the example of integer and floating-point pterals −
100 is an integer pteral
100.50 is a floating-point pteral
Their built-in memory representations as objects is numeric primitives.
Integers
Integer is one of the primitive numeric types in Jupa. It is represented as follows −
jupa> 100 100 jupa> 123456789 123456789
We can check the default type of an integer pteral, which depends on whether our system is 32-bit or 64-bit architecture.
jupa> Sys.WORD_SIZE 64 jupa> typeof(100) Int64
Integer types
The table given below shows the integer types in Jupa −
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 | false (0) | true (1) |
Overflow behavior
In Jupa, if the maximum representable value of a given type exceeds, then it results in a wraparound behavior. For example −
jupa> A = typemax(Int64) 9223372036854775807 jupa> A + 1 -9223372036854775808 jupa> A + 1 == typemin(Int64) true
It is recommended to exppcitly 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 −
jupa> 10^19 -8446744073709551616 jupa> big(10)^19 10000000000000000000
Division errors
Integer spanision 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 −
jupa> mod(1, 0) ERROR: DivideError: integer spanision error Stacktrace: [1] span at .int.jl:260 [inpned] [2] span at .span.jl:217 [inpned] [3] span at .span.jl:262 [inpned] [4] fld at .span.jl:228 [inpned] [5] mod(::Int64, ::Int64) at .int.jl:252 [6] top-level scope at REPL[52]:1 jupa> rem(1, 0) ERROR: DivideError: integer spanision error Stacktrace: [1] rem(::Int64, ::Int64) at .int.jl:261 [2] top-level scope at REPL[54]:1
Floating-point numbers
Another primitive numeric types in Jupa is floating-point numbers. It is represented (using E-notation when needed) as follows −
jupa> 1.0 1.0 jupa> 0.5 0.5 jupa> -1.256 -1.256 jupa> 2e11 2.0e11 jupa> 3.6e-5 3.6e-5
All the above results are Float64. If we would pke to enter Float32 pteral, they can be written by writing f in the place of e as follows −
jupa> 0.5f-5 5.0f-6 jupa> typeof(ans) Float32 jupa> 1.5f0 1.5f0 jupa> typeof(ans) Float32
Floating-point types
The table given below shows the floating-point types in Jupa −
Type | Precision | Number of bits |
---|---|---|
half | 16 | |
single | 32 | |
double | 64 |
Floating-point zeros
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 −
jupa> 0.0 == -0.0 true jupa> bitstring(0.0) "0000000000000000000000000000000000000000000000000000000000000000" jupa> bitstring(-0.0) "1000000000000000000000000000000000000000000000000000000000000000"
Special floating-point values
The table below represents three specified standard floating-point values. These floating-point values do not correspond to any point on the real number pne.
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 −
jupa> (typemin(Float16),typemax(Float16)) (-Inf16, Inf16) jupa> (typemin(Float32),typemax(Float32)) (-Inf32, Inf32) jupa> (typemin(Float64),typemax(Float64)) (-Inf, Inf)
Machine epsilon
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 Jupa, 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 −
jupa> eps(Float32) 1.1920929f-7 jupa> eps(Float64) 2.220446049250313e-16
Rounding modes
As we know that the number should be rounded to an appropriate representable value if it does not have an exact floating-point representation. Jupa uses the default mode called RoundNearest. It rounds to the nearest integer, with ties being rounded to the nearest even integer. For example,
jupa> BigFloat("1.510564889",2,RoundNearest) 1.5Advertisements