2.1 Intergers
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) |
Literal integers are represented in the standard manner
julia> 1
1
julia> 1234
1234
The default type for an integer literal depends on whether the target system has a 32-bit architecture or a 64-bit architecture
# 32-bit system:
julia> typeof(1)
Int32
# 64-bit system:
julia> typeof(1)
Int64
The Julia internal variable Sys.WORD_SIZE indicates whether the target system is 32-bit or 64-bit
# 32-bit system:
julia> Sys.WORD_SIZE
32
# 64-bit system:
julia> Sys.WORD_SIZE
64
Larger integer literals that cannot be represented using only 32 bits but can be represented in 64 bits always create 64-bit integers, regardless of the system type
# 32-bit or 64-bit system:
julia> typeof(3000000000)
Int64p
同时,Julia也支持直接输入十六进制(Hexadecimal)、二进制(Binary)和八进制(Octal)
- The size of the unsigned value is determined by the number of hex digits used
julia> 0x1
0x01
julia> typeof(ans)
UInt8
julia> 0x123
0x0123
julia> typeof(ans)
UInt16
julia> 0x1234567
0x01234567
julia> typeof(ans)
UInt32
julia> 0x123456789abcdef
0x0123456789abcdef
julia> typeof(ans)
UInt64
julia> 0b10
0x02
julia> typeof(ans)
UInt8
julia> 0o10
0x08
julia> typeof(ans)
UInt8
The minimum and maximum representable values of primitive numeric types such as integers are given by the typemin() and typemax() functions
julia> (typemin(Int32), typemax(Int32))
(-2147483648, 2147483647)
julia> for T in [Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128]
println("$(lpad(T,7)): [$(typemin(T)),$(typemax(T))]")
end
Int8: [-128,127]
Int16: [-32768,32767]
Int32: [-2147483648,2147483647]
Int64: [-9223372036854775808,9223372036854775807]
Int128: [-170141183460469231731687303715884105728,170141183460469231731687303715884105727]
UInt8: [0,255]
UInt16: [0,65535]
UInt32: [0,4294967295]
UInt64: [0,18446744073709551615]
UInt128: [0,340282366920938463463374607431768211455]
2.1.1 Overflow behavior
In Julia, exceeding the maximum representable value of a given type results in a wraparound behavior
julia> x = typemax(Int64)
9223372036854775807
julia> x + 1
-9223372036854775808
julia> x + 1 == typemin(Int64)
true
Thus, arithmetic with Julia integers is actually a form of modular arithmetic.
In applications where overflow is possible, explicit checking for wraparound produced by overflow is essential; otherwise, the BigInt type in Arbitrary Precision Arithmetic is recommended instead.
2.1.2 Division errors
Integer division (the div
function) has two exceptional cases: dividing by zero, and dividing the lowest negative number (typemin()) by -1.
julia> 34/0
Inf
julia> div(34,0)
ERROR: DivideError: integer division error
Stacktrace:
[1] div(::Int64, ::Int64) at ./int.jl:182
2.2 Floating-point
Type | Precision | Number of bits |
---|---|---|
Float16 | half | 16 |
Float32 | single | 32 |
Float64 | double | 64 |
Literal floating-point numbers are represented in the standard formats
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
The above results are all Float64 values. Literal Float32 values can be entered by writing an f
in place of e
julia> 0.5f0
0.5f0
julia> typeof(ans)
Float32
julia> 2.5f-4
0.00025f0
Values can be converted to Float32 easily
julia> Float32(-1.5)
-1.5f0
julia> typeof(ans)
Float32
The underscore _
can be used as digit separator
julia> 10_000, 0.000_000_005, 0xdead_beef, 0b1011_0010
(10000, 5.0e-9, 0xdeadbeef, 0xb2)
2.2.1 Floating point zero
Floating-point numbers have two zeros, positive zero and negative zero. They are equal to each other but have different binary representations, as can be seen using the bits
function:
julia> 0.0 == -0.0
true
julia> bits(0.0)
"0000000000000000000000000000000000000000000000000000000000000000"
julia> bits(-0.0)
"1000000000000000000000000000000000000000000000000000000000000000"
2.2.2 Special floating-point values
Float16 | Float32 | Float64 | Name | Description |
---|---|---|---|---|
Inf16 |
Inf32 |
Inf |
positive infinity | a value greater than all finite floating-point values |
-Inf16 |
-Inf32 |
-Inf |
negative infinity | a value less than all finite floating-point values |
NaN16 |
NaN32 |
NaN |
not a number | a value not == to any floating-point value (including itself) |
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
The typemin() and typemax() functions also apply to floating-point types
julia> (typemin(Float16),typemax(Float16))
(-Inf16, Inf16)
julia> (typemin(Float32),typemax(Float32))
(-Inf32, Inf32)
julia> (typemin(Float64),typemax(Float64))
(-Inf, Inf)
2.2.3 Machine epsilon
Most real numbers cannot be represented exactly with floating-point numbers, and so for many purposes it is important to know the distance between two adjacent representable floating-point numbers, which is often known as machine epsilon.
Julia provides eps(), which gives the distance between 1.0
and the next larger representable floating-point value:
julia> eps(Float32)
1.1920929f-7
julia> eps(Float64)
2.220446049250313e-16
julia> eps() # same as eps(Float64)
2.220446049250313e-16
eps 函数也可以取浮点数作为参数,给出这个值和下一个可表示的浮点数的绝对差,即, eps(x) 的结果与 同类型,且满足 x + eps(x) 是下一个比 x 稍大的、可表示的浮点数:
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
相邻的两个浮点数之间的距离并不是固定的,数值越小,间距越小;数值越大, 间距越大。换句话说,浮点数在0附近最稠密,随着数值越来越大,数值越来越稀疏,数值间的距离呈指数增长。 eps(1.0) 与eps(Float64) 相同,因为 1.0 是 64 位浮点数。
Julia also provides the nextfloat() and prevfloat() functions which return the next largest or smallest representable floating-point number to the argument respectively:
julia> x = 1.25f0
1.25f0
julia> nextfloat(x)
1.2500001f0
julia> prevfloat(x)
1.2499999f0
julia> bits(prevfloat(x))
"00111111100111111111111111111111"
julia> bits(x)
"00111111101000000000000000000000"
julia> bits(nextfloat(x))
"00111111101000000000000000000001"
2.2.4 Rounding mode
If a number doesn’t have an exact floating-point representation, it must be rounded to an appropriate representable value, however, if wanted, the manner in which this rounding is done can be changed according to the rounding modes presented in the IEEE 754 standard.
julia> x = 1.1; y = 0.1;
julia> x + y
1.2000000000000002
julia> setrounding(Float64,RoundDown) do
x + y
end
1.2
2.3 Arbitrary precision arithmetic
To allow computations with arbitrary-precision integers and floating point numbers, Julia wraps the GNU Multiple Precision Arithmetic Library (GMP) and the GNU MPFR Library, respectively.
The BigInt and BigFloat types are available in Julia for arbitrary precision integer and floating point numbers respectively.
Constructors exist to create these types from primitive numerical types, and parse() can be used to construct them from AbstractStrings
. Once created, they participate in arithmetic with all other numeric types thanks to Julia’s type promotion and conversion mechanism:
- Julia可以通过基本数值类型、以及parse()函数通过字符串创建BigInt和BigFloat型数据
julia> BigInt(typemax(Int64)) + 1
9223372036854775808
julia> parse(BigInt, "123456789012345678901234567890") + 1
123456789012345678901234567891
julia> parse(BigFloat, "1.23456789012345678901")
1.234567890123456789010000000000000000000000000000000000000000000000000000000004
julia> BigFloat(2.0^66) / 3
2.459565876494606882133333333333333333333333333333333333333333333333333333333344e+19
julia> factorial(BigInt(40))
815915283247897734345611269596115894272000000000
但是需要注意:BigInt、BigFloat型数据不会自动生成,需要显示声明
julia> x = typemin(Int64)
-9223372036854775808
julia> x = x - 1
9223372036854775807
julia> typeof(x)
Int64
julia> y = BigInt(typemin(Int64))
-9223372036854775808
julia> y = y - 1
-9223372036854775809
julia> typeof(y)
BigInt
当然了,Julia也支持修改默认的精度和rounding mode,可以调用setprecision() 和 setrounding()函数
绝大多数时候没有修改的必要
julia> setrounding(BigFloat, RoundUp) do
BigFloat(1) + parse(BigFloat, "0.1")
end
1.100000000000000000000000000000000000000000000000000000000000000000000000000003
julia> setrounding(BigFloat, RoundDown) do
BigFloat(1) + parse(BigFloat, "0.1")
end
1.099999999999999999999999999999999999999999999999999999999999999999999999999986
julia> setprecision(40) do
BigFloat(1) + parse(BigFloat, "0.1")
end
1.1000000000004
2.4 Numeric literal coefficient
Julia支持数值与变量直接拼接成一个数据公式,中间不能有空格
julia> x = 3
3
julia> 2x^2 - 3x + 1
10
julia> 1.5x^2 - .5x + 1
13.0
Neither juxtaposition of two parenthesized expressions, nor placing a variable before a parenthesized expression, however, can be used to imply multiplication:
Julia支持括号表达式,但是不支持括号表达式并列和括号表达式前加变量,如果发生上述情况会报错
julia> (x-1)x
6
julia> (x-1)(x+1)
ERROR: MethodError: objects of type Int64 are not callable
julia> x(x+1)
ERROR: MethodError: objects of type Int64 are not callable
2.5 Literal zero and one
Julia provides functions which return literal 0 and 1 corresponding to a specified type or the type of a given variable.
Function | Description |
---|---|
zero(x) | Literal zero of type x or type of variable x |
one(x) | Literal one of type x or type of variable x |
These functions are useful in Numeric Comparisons to avoid overhead from unnecessary type conversion.
julia> zero(Float32)
0.0f0
julia> zero(1.0)
0.0
julia> one(Int32)
1
julia> one(BigFloat)
1.000000000000000000000000000000000000000000000000000000000000000000000000000000