Integers and Floating-Point Numbers
Integers and floating-point values are the basic building blocks of arithmetic and computation. Built-in representations of such values are called numeric primitives, while representations of integers and floating-point numbers as immediate values in code are known as numeric literals. For example, 1 is an integer literal, while 1.0 is a floating-point literal; their binary in-memory representations as objects are numeric primitives. Julia provides a broad range of primitive numeric types, and a full complement of arithmetic and bitwise operators as well as standard mathematical functions are defined over them. The following are Julia’s primitive numeric types:- Integer types:
- Int8 — signed 8-bit integers ranging from –27 to 27 – 1.
- Uint8 — unsigned 8-bit integers ranging from 0 to 28 – 1.
- Int16 — signed 16-bit integers ranging from –215 to 215 – 1.
- Uint16 — unsigned 16-bit integers ranging from 0 to 216 – 1.
- Int32 — signed 32-bit integers ranging from –231 to 231 – 1.
- Uint32 — unsigned 32-bit integers ranging from 0 to 232 – 1.
- Int64 — signed 64-bit integers ranging from –263 to 263 – 1.
- Uint64 — unsigned 64-bit integers ranging from 0 to 264 – 1.
- Bool — either true or false, which correspond numerically to 1 and 0.
- Char — a 32-bit numeric type representing a Unicode character (see Strings for more details).
- Floating-point types:
- Float32 — IEEE 754 32-bit floating-point numbers.
- Float64 — IEEE 754 64-bit floating-point numbers.
Integers
Literal integers are represented in the standard manner:julia> 1
1
julia> 1234
1234
# 32-bit system:
julia> typeof(1)
Int32
# 64-bit system:
julia> typeof(1)
Int64
# 32-bit system:
julia> Int
Int32
# 64-bit system:
julia> Int
Int64
# 32-bit system:
julia> Uint
Uint32
# 64-bit system:
julia> Uint
Uint64
# 32-bit or 64-bit system:
julia> typeof(3000000000)
Int64
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
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 = {Int8,Int16,Int32,Int64,Uint8,Uint16,Uint32,Uint64}
println("$(lpad(T,6)): [$(typemin(T)),$(typemax(T))]")
end
Int8: [-128,127]
Int16: [-32768,32767]
Int32: [-2147483648,2147483647]
Int64: [-9223372036854775808,9223372036854775807]
Uint8: [0x00,0xff]
Uint16: [0x0000,0xffff]
Uint32: [0x00000000,0xffffffff]
Uint64: [0x0000000000000000,0xffffffffffffffff]
Floating-Point Numbers
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
1e+10
julia> 2.5e-4
0.00025
julia> float32(-1.5)
-1.5
julia> typeof(ans)
Float32
- Inf — positive infinity — a value larger than all finite floating-point values
- -Inf — negative infinity — a value smaller than all finite floating-point values
- NaN — not a number — a value incomparable to all floating-point values (including itself).
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
NaN
julia> (typemin(Float32),typemax(Float32))
(-Inf,Inf)
julia> (typemin(Float64),typemax(Float64))
(-Inf,Inf)
Floating-point types also support the eps function, which gives the distance between 1.0 and the next largest representable floating-point value:
julia> eps(Float32)
1.192092896e-07
julia> eps(Float64)
2.22044604925031308e-16
julia> eps(1.0)
2.22044604925031308e-16
julia> eps(1000.)
1.13686837721616030e-13
julia> eps(1e-27)
1.79366203433576585e-43
julia> eps(0.0)
4.94065645841246544e-324
Background and References
For a brief but lucid presentation of how floating-point numbers are represented, see John D. Cook’s article on the subject as well as his introduction to some of the issues arising from how this representation differs in behavior from the idealized abstraction of real numbers. For an excellent, in-depth discussion of floating-point numbers and issues of numerical accuracy encountered when computing with them, see David Goldberg’s paper What Every Computer Scientist Should Know About Floating-Point Arithmetic. For even more extensive documentation of the history of, rationale for, and issues with floating-point numbers, as well as discussion of many other topics in numerical computing, see the collected writings of William Kahan, commonly known as the “Father of Floating-Point”. Of particular interest may be An Interview with the Old Man of Floating-Point.Numeric Literal Coefficients
To make common numeric formulas and expressions clearer, Julia allows variables to be immediately preceded by a numeric literal, implying multiplication. This makes writing polynomial expressions much cleaner:julia> x = 3
3
julia> 2x^2 - 3x + 1
10
julia> 1.5x^2 - .5x + 1
13.0
julia> 2(x-1)^2 - 3(x-1) + 1
3
julia> (x-1)x
6
julia> (x-1)(x+1)
type error: apply: expected Function, got Int64
julia> x(x+1)
type error: apply: expected Function, got Int64
The above syntactic enhancements significantly reduce the visual noise incurred when writing common mathematical formulae. Note that no whitespace may come between a numeric literal coefficient and the identifier or parenthesized expression which it multiplies.
Syntax Conflicts
Juxtaposed literal coefficient syntax conflicts with two numeric literal syntaxes: hexadecimal integer literals and engineering notation for floating-point literals. Here are some situations where syntactic conflicts arise:- The hexadecimal integer literal expression 0xff could be interpreted as the numeric literal 0 multiplied by the variable xff.
- The floating-point literal expression 1e10 could be interpreted as the numeric literal 1 multiplied by the variable e10, and similarly with the equivalent E form.
- Expressions starting with 0x are always hexadecimal literals.
- Expressions starting with a numeric literal followed by e or E are always floating-point literals.
No comments:
Post a Comment
Thank you