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

  1. julia> 1
  2. 1
  3. julia> 1234
  4. 1234

The default type for an integer literal depends on whether the target system has a 32-bit architecture or a 64-bit architecture

  1. # 32-bit system:
  2. julia> typeof(1)
  3. Int32
  4. # 64-bit system:
  5. julia> typeof(1)
  6. Int64

The Julia internal variable Sys.WORD_SIZE indicates whether the target system is 32-bit or 64-bit

  1. # 32-bit system:
  2. julia> Sys.WORD_SIZE
  3. 32
  4. # 64-bit system:
  5. julia> Sys.WORD_SIZE
  6. 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

  1. # 32-bit or 64-bit system:
  2. julia> typeof(3000000000)
  3. Int64p

同时,Julia也支持直接输入十六进制(Hexadecimal)、二进制(Binary)和八进制(Octal)

  • The size of the unsigned value is determined by the number of hex digits used
  1. julia> 0x1
  2. 0x01
  3. julia> typeof(ans)
  4. UInt8
  5. julia> 0x123
  6. 0x0123
  7. julia> typeof(ans)
  8. UInt16
  9. julia> 0x1234567
  10. 0x01234567
  11. julia> typeof(ans)
  12. UInt32
  13. julia> 0x123456789abcdef
  14. 0x0123456789abcdef
  15. julia> typeof(ans)
  16. UInt64
  17. julia> 0b10
  18. 0x02
  19. julia> typeof(ans)
  20. UInt8
  21. julia> 0o10
  22. 0x08
  23. julia> typeof(ans)
  24. UInt8

The minimum and maximum representable values of primitive numeric types such as integers are given by the typemin() and typemax() functions

  1. julia> (typemin(Int32), typemax(Int32))
  2. (-2147483648, 2147483647)
  3. julia> for T in [Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128]
  4. println("$(lpad(T,7)): [$(typemin(T)),$(typemax(T))]")
  5. end
  6. Int8: [-128,127]
  7. Int16: [-32768,32767]
  8. Int32: [-2147483648,2147483647]
  9. Int64: [-9223372036854775808,9223372036854775807]
  10. Int128: [-170141183460469231731687303715884105728,170141183460469231731687303715884105727]
  11. UInt8: [0,255]
  12. UInt16: [0,65535]
  13. UInt32: [0,4294967295]
  14. UInt64: [0,18446744073709551615]
  15. UInt128: [0,340282366920938463463374607431768211455]

2.1.1 Overflow behavior

In Julia, exceeding the maximum representable value of a given type results in a wraparound behavior

  1. julia> x = typemax(Int64)
  2. 9223372036854775807
  3. julia> x + 1
  4. -9223372036854775808
  5. julia> x + 1 == typemin(Int64)
  6. 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.

  1. julia> 34/0
  2. Inf
  3. julia> div(34,0)
  4. ERROR: DivideError: integer division error
  5. Stacktrace:
  6. [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

  1. julia> 1.0
  2. 1.0
  3. julia> 1.
  4. 1.0
  5. julia> 0.5
  6. 0.5
  7. julia> .5
  8. 0.5
  9. julia> -1.23
  10. -1.23
  11. julia> 1e10
  12. 1.0e10
  13. julia> 2.5e-4
  14. 0.00025

The above results are all Float64 values. Literal Float32 values can be entered by writing an f in place of e

  1. julia> 0.5f0
  2. 0.5f0
  3. julia> typeof(ans)
  4. Float32
  5. julia> 2.5f-4
  6. 0.00025f0

Values can be converted to Float32 easily

  1. julia> Float32(-1.5)
  2. -1.5f0
  3. julia> typeof(ans)
  4. Float32

The underscore _ can be used as digit separator

  1. julia> 10_000, 0.000_000_005, 0xdead_beef, 0b1011_0010
  2. (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:

  1. julia> 0.0 == -0.0
  2. true
  3. julia> bits(0.0)
  4. "0000000000000000000000000000000000000000000000000000000000000000"
  5. julia> bits(-0.0)
  6. "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)
  1. julia> 1/Inf
  2. 0.0
  3. julia> 1/0
  4. Inf
  5. julia> -5/0
  6. -Inf
  7. julia> 0.000001/0
  8. Inf
  9. julia> 0/0
  10. NaN
  11. julia> 500 + Inf
  12. Inf
  13. julia> 500 - Inf
  14. -Inf
  15. julia> Inf + Inf
  16. Inf
  17. julia> Inf - Inf
  18. NaN
  19. julia> Inf * Inf
  20. Inf
  21. julia> Inf / Inf
  22. NaN
  23. julia> 0 * Inf
  24. NaN

The typemin() and typemax() functions also apply to floating-point types

  1. julia> (typemin(Float16),typemax(Float16))
  2. (-Inf16, Inf16)
  3. julia> (typemin(Float32),typemax(Float32))
  4. (-Inf32, Inf32)
  5. julia> (typemin(Float64),typemax(Float64))
  6. (-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:

  1. julia> eps(Float32)
  2. 1.1920929f-7
  3. julia> eps(Float64)
  4. 2.220446049250313e-16
  5. julia> eps() # same as eps(Float64)
  6. 2.220446049250313e-16

eps 函数也可以取浮点数作为参数,给出这个值和下一个可表示的浮点数的绝对差,即, eps(x) 的结果与 同类型,且满足 x + eps(x) 是下一个比 x 稍大的、可表示的浮点数:

  1. julia> eps(1.0)
  2. 2.220446049250313e-16
  3. julia> eps(1000.)
  4. 1.1368683772161603e-13
  5. julia> eps(1e-27)
  6. 1.793662034335766e-43
  7. julia> eps(0.0)
  8. 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:

  1. julia> x = 1.25f0
  2. 1.25f0
  3. julia> nextfloat(x)
  4. 1.2500001f0
  5. julia> prevfloat(x)
  6. 1.2499999f0
  7. julia> bits(prevfloat(x))
  8. "00111111100111111111111111111111"
  9. julia> bits(x)
  10. "00111111101000000000000000000000"
  11. julia> bits(nextfloat(x))
  12. "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.

  1. julia> x = 1.1; y = 0.1;
  2. julia> x + y
  3. 1.2000000000000002
  4. julia> setrounding(Float64,RoundDown) do
  5. x + y
  6. end
  7. 1.2

2.3 Arbitrary precision arithmetic

  1. 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.

  2. 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型数据
  1. julia> BigInt(typemax(Int64)) + 1
  2. 9223372036854775808
  3. julia> parse(BigInt, "123456789012345678901234567890") + 1
  4. 123456789012345678901234567891
  5. julia> parse(BigFloat, "1.23456789012345678901")
  6. 1.234567890123456789010000000000000000000000000000000000000000000000000000000004
  7. julia> BigFloat(2.0^66) / 3
  8. 2.459565876494606882133333333333333333333333333333333333333333333333333333333344e+19
  9. julia> factorial(BigInt(40))
  10. 815915283247897734345611269596115894272000000000

但是需要注意:BigInt、BigFloat型数据不会自动生成,需要显示声明

  1. julia> x = typemin(Int64)
  2. -9223372036854775808
  3. julia> x = x - 1
  4. 9223372036854775807
  5. julia> typeof(x)
  6. Int64
  7. julia> y = BigInt(typemin(Int64))
  8. -9223372036854775808
  9. julia> y = y - 1
  10. -9223372036854775809
  11. julia> typeof(y)
  12. BigInt

当然了,Julia也支持修改默认的精度和rounding mode,可以调用setprecision()setrounding()函数
绝大多数时候没有修改的必要

  1. julia> setrounding(BigFloat, RoundUp) do
  2. BigFloat(1) + parse(BigFloat, "0.1")
  3. end
  4. 1.100000000000000000000000000000000000000000000000000000000000000000000000000003
  5. julia> setrounding(BigFloat, RoundDown) do
  6. BigFloat(1) + parse(BigFloat, "0.1")
  7. end
  8. 1.099999999999999999999999999999999999999999999999999999999999999999999999999986
  9. julia> setprecision(40) do
  10. BigFloat(1) + parse(BigFloat, "0.1")
  11. end
  12. 1.1000000000004

2.4 Numeric literal coefficient

Julia支持数值与变量直接拼接成一个数据公式,中间不能有空格

  1. julia> x = 3
  2. 3
  3. julia> 2x^2 - 3x + 1
  4. 10
  5. julia> 1.5x^2 - .5x + 1
  6. 13.0

Neither juxtaposition of two parenthesized expressions, nor placing a variable before a parenthesized expression, however, can be used to imply multiplication:
Julia支持括号表达式,但是不支持括号表达式并列和括号表达式前加变量,如果发生上述情况会报错

  1. julia> (x-1)x
  2. 6
  3. julia> (x-1)(x+1)
  4. ERROR: MethodError: objects of type Int64 are not callable
  5. julia> x(x+1)
  6. 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.

  1. julia> zero(Float32)
  2. 0.0f0
  3. julia> zero(1.0)
  4. 0.0
  5. julia> one(Int32)
  6. 1
  7. julia> one(BigFloat)
  8. 1.000000000000000000000000000000000000000000000000000000000000000000000000000000