In programming languages, there are basic mathematical operations and scientific calculations, which commonly use data types such as integers and floating-point numbers.
Another term is called a "literal", which refers to a notation used in source code to represent a fixed value. Integers, floating-point numbers, and strings, etc., are all literals.
For example:
a=1 # a is a variable, 1 is an integer literal
b=1.0 # b is a variable, 1.0 is a floating-point literal
Julia provides a rich set of primitive numeric types and defines a comprehensive set of arithmetic operations based on them. It also provides bitwise operators and some standard mathematical functions.
Integer Types
| Type | Signed? | Bits | Minimum Value | Maximum 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) |
Integer literal forms:
Examples
julia>1
1
julia>1234
1234
The default type of integer literals depends on whether the target system is 32-bit or 64-bit architecture (currently most systems are 64-bit):
Examples
julia> typeof(1)
Int32
julia> typeof(1)
Int64
The built-in variable Sys.WORD_SIZE indicates whether the target system is 32-bit or 64-bit architecture:
Examples
julia> Sys.WORD_SIZE
32
julia> Sys.WORD_SIZE
64
Julia also defines Int and UInt types, which are aliases for the native signed and unsigned integer types of the system.
Examples
julia>Int
Int32
julia> UInt
UInt32
julia>Int
Int64
julia> UInt
UInt64
Overflow Behavior
In Julia, exceeding the maximum value that can be represented by a type results in wraparound behavior:Examples
julia> x = typemax(Int64)
9223372036854775807
julia> x + 1
-9223372036854775808
julia> x + 1 == typemin(Int64)
true
Therefore, Julia's integer arithmetic is essentially a form of modular arithmetic, reflecting the characteristics of modern computer implementations of underlying arithmetic. In programs where overflow may occur, explicit checks for boundary values should be made. Otherwise, it is recommended to use the arbitrary precision arithmetic type BigInt as an alternative.
Here is an example of overflow behavior and how to handle it:
Examples
julia>10^19
-8446744073709551616
julia>big(10)^19
10000000000000000000
Division Error
Integer division triggers a DivideError exception in two exceptional cases:- Dividing by zero
- Dividing by the smallest negative number
Examples
julia> mod(1, 0)
ERROR: DivideError: integer division error
Stacktrace:
div at .int.jl:260
div at .div.jl:217
div at .div.jl:262
fld at .div.jl:228
mod(::Int64, ::Int64) at .int.jl:252
top-level scope at REPL:1
julia> rem(1, 0)
ERROR: DivideError: integer division error
Stacktrace:
rem(::Int64, ::Int64) at .int.jl:261
top-level scope at REPL:1
Floating Point Types
The following table lists the floating point types supported by Julia:| Type | Precision | Bits |
|---|---|---|
| Float16 | Half Precision | 16 |
| Float32 | Single Precision | 32 |
| Float64 | Double Precision | 64 |
Examples
julia>1.0
1.0
julia>1.
1.0
julia>0.5
0.5
julia>.5
0.5
julia>-1.23
-1.23
julia>1e10
1.0e10
julia>2.5e-4
0.00025
Note:
In scientific notation, to simplify formulas, you can use the E format. For example, 1.03 times 10 to the power of 8 can be written as "1.03E+08", where "E" stands for exponent (exponent).
All the above results are values of type Float64. Using f instead of e will give a literal of type Float32:
Examples
julia>x = 0.5f0
0.5f0
julia>typeof(x)
Float32
julia>2.5f-4
0.00025f0
Numbers can easily be converted to type Float32:
julia>x = Float32(-1.5)
-1.5f0
julia>typeof(x)
Float32
Hexadecimal floating point literals exist but are only applicable to values of type Float64. Generally, they use a p prefix and an index base 2:
Examples
julia>0x1p0
1.0
julia>0x1.8p3
12.0
julia>x = 0x.4p-1
0.125
julia>typeof(x)
Float64
Julia also supports half-precision floating point numbers (Float16), but they are implemented using software simulation of Float32.
julia>sizeof(Float16(4.))
2
julia>2*Float16(4.)
Float16(8.0)
An underscore (_) can be used as a digit separator:
Examples
julia>10_000, 0.000_000_005, 0xdead_beef, 0b1011_0010
(10000, 5.0e-9, 0xdeadbeef, 0xb2)
Zeros in Floating Point Numbers
There are two kinds of zeros in floating point numbers: positive zero and negative zero. They are equal but have different binary representations, which can be viewed using the bitstring function:Examples
julia>0.0 == -0.0
true
julia>bitstring(0.0)
"0000000000000000000000000000000000000000000000000000000000000000"
julia>bitstring(-0.0)
"1000000000000000000000000000000000000000000000000000000000000000"
Special Floating Point Values
There are three specific standard floating point values that do not correspond to any point on the real number axis:| Float16 | Float32 | Float64 | Name | Description |
|---|---|---|---|---|
| Inf16 | Inf32 | Inf | Positive Infinity | A number greater than all finite floating point numbers |
| -Inf16 | -Inf32 | -Inf | Negative Infinity | A number less than all finite floating point numbers |
| NaN16 | NaN32 | NaN | Not a Number | A value that is not equal (==) to any floating point value (including itself) |
Examples
julia>1/Inf
0.0
julia>1/0
Inf
julia>-5/0
-Inf
julia>0.000001/0
Inf
julia>0/0
NaN
julia>500 + Inf
Inf
julia>500 - Inf
-Inf
julia>Inf + Inf
Inf
julia>Inf - Inf
NaN
julia>Inf * Inf
Inf
julia>Inf / Inf
NaN
julia>0*Inf
NaN
julia>NaN == NaN
false
julia>NaN != NaN
true
julia>NaN < NaN
false
julia>NaN > NaN
false
We can also use the typemin and typemax functions:
Examples
julia>(typemin(Float16),typemax(Float16))
(-Inf16, Inf16)
julia>(typemin(Float32),typemax(Float32))
(-Inf32, Inf32)
julia>(typemin(Float64),typemax(Float64))
(-Inf, Inf)
Machine Precision
Most real numbers cannot be accurately represented as floating point numbers, so it is necessary to know the distance between two adjacent representable floating point numbers, which is usually called machine precision. Julia provides the eps function, which can give the difference between 1.0 and the next floating point number that Julia can represent:Examples
julia>eps(Float32)
1.1920929f-7
julia>eps(Float64)
2.220446049250313e-16
julia>eps() # same as eps(Float64)
2.220446049250313e-16
These values are 2.0^-23 for Float32 and 2.0^-52 for Float64. The eps function can also accept a floating point value as a parameter and then give the absolute difference between this value and the next representable floating point value. That is, eps(x) produces a value of the same type as x, and x + eps(x) is exactly the next representable floating point value larger than x:
Examples
julia>eps(1.0)
2.220446049250313e-16
julia>eps(1000.)
1.1368683772161603e-13
julia>eps(1e-27)
1.793662034335766e-43
julia>eps(0.0)
5.0e-324
The distance between two adjacent representable floating point numbers is not constant; the smaller the number, the smaller the interval, and the larger the number, the larger the interval. In other words, representable floating point numbers are the densest around the zero point on the real number axis and become increasingly sparse as they move away from the zero point in either direction. By definition, eps(1.0) is equal to eps(Float64) because 1.0 is a 64-bit floating point value.
Julia also provides the nextfloat and prevfloat functions, which return the next larger or smaller representable floating point number based on the parameter:
Examples
julia>x = 1.25f0
1.25f0
julia>nextfloat(x)
1.2500001f0
julia>prevfloat(x)
1.2499999f0
julia>bitstring(prevfloat(x))
"00111111100111111111111111111111"
julia>bitstring(x)
"00111111101000000000000000000000"
julia>bitstring(nextfloat(x))
"00111111101000000000000000000001"
This example illustrates the general principle that adjacent representable floating point numbers also have adjacent binary integer representations.
Rounding Modes
A number must be rounded to an appropriate representable value if it does not have an exact floating point representation. The default mode used by Julia is RoundNearest, which means rounding to the nearest representable value, and the value being rounded uses the least number of significant digits possible.Examples
julia>BigFloat("1.510564889",2,RoundNearest)
1.5
julia>BigFloat("1.550564889",2,RoundNearest)
1.5
julia>BigFloat("1.560564889",2,RoundNearest)
1.5
Literals for 0 and 1
Julia provides functions to return the zero and one literals for a specific type or the type of a given variable.| Function | Description |
|---|---|
| zero(x) | The zero literal for the type of x or the type of variable x |
| one(x) | The one literal for the type of x or the type of variable x |
Examples
julia>zero(Float32)
0.0f0
julia>zero(1.0)
0.0
julia>one(Int32)
1
julia>one(BigFloat)
1.0
Type Conversion
Type conversion is the process of changing a variable from one type to another data type. For example, if you want to store a float type value into a simple integer, you need to force cast the float type to an int type. You can use the type casting operator to explicitly convert a value from one type to another, as shown below: Julia supports three types of numeric conversions, each handling imprecise conversions differently. First Type: T(x) or convert(T,x) Both of these will convert x to type T. * If T is a floating-point type, the result of the conversion is the nearest representable value, which could be positive or negative infinity. * If T is an integer type, when x cannot be represented by type T, an InexactError will be thrown. Second Type: x % T can also convert integer x to integer type T, consistent with the result of x modulo 2^n, where n is the number of bits in T. Third Type: Rounding functions receive an optional T type parameter. For example, round(Int,x) is equivalent to Int(round(x)).Examples
julia>Int8(127)
127
julia>Int8(128)
ERROR: InexactError: trunc(Int8, 128)
Stacktrace:
[...]
julia>Int8(127.0)
127
julia>Int8(3.14)
ERROR: InexactError: Int8(3.14)
Stacktrace:
[...]
julia>Int8(128.0)
ERROR: InexactError: Int8(128.0)
Stacktrace:
[...]
julia>127%Int8
127
julia>128%Int8
-128
julia>round(Int8,127.4)
127
julia>round(Int8,127.6)
ERROR: InexactError: trunc(Int8, 128.0)
Stacktrace:
[...]
YouTip