数值类型
标准数值类型
下面展示了 Base 中 Number 的所有子类型的类型树。 抽象类型已经标出,其余的是具体类型。
Number (Abstract Type)
├─ Complex
└─ Real (Abstract Type)
├─ AbstractFloat (Abstract Type)
│ ├─ Float16
│ ├─ Float32
│ ├─ Float64
│ └─ BigFloat
├─ Integer (Abstract Type)
│ ├─ Bool
│ ├─ Signed (Abstract Type)
│ │ ├─ Int8
│ │ ├─ Int16
│ │ ├─ Int32
│ │ ├─ Int64
│ │ ├─ Int128
│ │ └─ BigInt
│ └─ Unsigned (Abstract Type)
│ ├─ UInt8
│ ├─ UInt16
│ ├─ UInt32
│ ├─ UInt64
│ └─ UInt128
├─ Rational
└─ AbstractIrrational (Abstract Type)
└─ Irrational抽象数值类型
Core.Number — Type
NumberAbstract supertype for all number types.
Core.AbstractFloat — Type
AbstractFloat <: RealAbstract supertype for all floating point numbers.
Core.Integer — Type
Integer <: RealAbstract supertype for all integers.
Core.Signed — Type
Signed <: IntegerAbstract supertype for all signed integers.
Core.Unsigned — Type
Unsigned <: IntegerAbstract supertype for all unsigned integers.
Base.AbstractIrrational — Type
AbstractIrrational <: RealNumber type representing an exact irrational value, which is automatically rounded to the correct precision in arithmetic operations with other numeric quantities.
Subtypes MyIrrational <: AbstractIrrational should implement at least ==(::MyIrrational, ::MyIrrational), hash(x::MyIrrational, h::UInt), and convert(::Type{F}, x::MyIrrational) where {F <: Union{BigFloat,Float32,Float64}}.
If a subtype is used to represent values that may occasionally be rational (e.g. a square-root type that represents √n for integers n will give a rational result when n is a perfect square), then it should also implement isinteger, iszero, isone, and == with Real values (since all of these default to false for AbstractIrrational types), as well as defining hash to equal that of the corresponding Rational.
具体数值类型
Core.Float16 — Type
Float16 <: AbstractFloat16-bit floating point number type (IEEE 754 standard).
Binary format: 1 sign, 5 exponent, 10 fraction bits.
Core.Float32 — Type
Float32 <: AbstractFloat32-bit floating point number type (IEEE 754 standard).
Binary format: 1 sign, 8 exponent, 23 fraction bits.
Core.Float64 — Type
Float64 <: AbstractFloat64-bit floating point number type (IEEE 754 standard).
Binary format: 1 sign, 11 exponent, 52 fraction bits.
Base.MPFR.BigFloat — Type
BigFloat <: AbstractFloatArbitrary precision floating point number type.
Core.Bool — Type
Bool <: IntegerBoolean type, containing the values true and false.
Bool is a kind of number: false is numerically equal to 0 and true is numerically equal to 1. Moreover, false acts as a multiplicative "strong zero":
julia> false == 0
true
julia> true == 1
true
julia> 0 * NaN
NaN
julia> false * NaN
0.0Core.UInt8 — Type
UInt8 <: Unsigned8-bit unsigned integer type.
Core.Int16 — Type
Int16 <: Signed16-bit signed integer type.
Core.UInt16 — Type
UInt16 <: Unsigned16-bit unsigned integer type.
Core.Int32 — Type
Int32 <: Signed32-bit signed integer type.
Core.UInt32 — Type
UInt32 <: Unsigned32-bit unsigned integer type.
Core.Int64 — Type
Int64 <: Signed64-bit signed integer type.
Core.UInt64 — Type
UInt64 <: Unsigned64-bit unsigned integer type.
Core.Int128 — Type
Int128 <: Signed128-bit signed integer type.
Core.UInt128 — Type
UInt128 <: Unsigned128-bit unsigned integer type.
Base.GMP.BigInt — Type
BigInt <: SignedArbitrary precision integer type.
Base.Complex — Type
Complex{T<:Real} <: NumberComplex number type with real and imaginary part of type T.
ComplexF16, ComplexF32 and ComplexF64 are aliases for Complex{Float16}, Complex{Float32} and Complex{Float64} respectively.
Base.Rational — Type
Rational{T<:Integer} <: RealRational number type, with numerator and denominator of type T. Rationals are checked for overflow.
Base.Irrational — Type
Irrational{sym} <: AbstractIrrationalNumber type representing an exact irrational value denoted by the symbol sym, such as π, ℯ and γ.
See also AbstractIrrational.
数据格式
Base.digits — Function
digits([T<:Integer], n::Integer; base::T = 10, pad::Integer = 1)Return an array with element type T (default Int) of the digits of n in the given base, optionally padded with zeros to a specified size. More significant digits are at higher indices, such that n == sum(digits[k]*base^(k-1) for k=1:length(digits)).
See also ndigits, digits!, and for base 2 also bitstring, count_ones.
Examples
julia> digits(10)
2-element Vector{Int64}:
0
1
julia> digits(10, base = 2)
4-element Vector{Int64}:
0
1
0
1
julia> digits(-256, base = 10, pad = 5)
5-element Vector{Int64}:
-6
-5
-2
0
0
julia> n = rand(-999:999);
julia> n == evalpoly(13, digits(n, base = 13))
trueBase.digits! — Function
digits!(array, n::Integer; base::Integer = 10)Fills an array of the digits of n in the given base. More significant digits are at higher indices. If the array length is insufficient, the least significant digits are filled up to the array length. If the array length is excessive, the excess portion is filled with zeros.
Examples
julia> digits!([2, 2, 2, 2], 10, base = 2)
4-element Vector{Int64}:
0
1
0
1
julia> digits!([2, 2, 2, 2, 2, 2], 10, base = 2)
6-element Vector{Int64}:
0
1
0
1
0
0Base.bitstring — Function
bitstring(n)A string giving the literal bit representation of a primitive type.
See also count_ones, count_zeros, digits.
Examples
julia> bitstring(Int32(4))
"00000000000000000000000000000100"
julia> bitstring(2.2)
"0100000000000001100110011001100110011001100110011001100110011010"Base.parse — Function
parse(::Type{Platform}, triplet::AbstractString)Parses a string platform triplet back into a Platform object.
parse(type, str; base)Parse a string as a number. For Integer types, a base can be specified (the default is 10). For floating-point types, the string is parsed as a decimal floating-point number. Complex types are parsed from decimal strings of the form "R±Iim" as a Complex(R,I) of the requested type; "i" or "j" can also be used instead of "im", and "R" or "Iim" are also permitted. If the string does not contain a valid number, an error is raised.
Examples
julia> parse(Int, "1234")
1234
julia> parse(Int, "1234", base = 5)
194
julia> parse(Int, "afc", base = 16)
2812
julia> parse(Float64, "1.2e-3")
0.0012
julia> parse(Complex{Float64}, "3.2e-1 + 4.5im")
0.32 + 4.5imBase.tryparse — Function
tryparse(type, str; base)Like parse, but returns either a value of the requested type, or nothing if the string does not contain a valid number.
Base.signed — Function
signed(T::Integer)Convert an integer bitstype to the signed type of the same size.
Examples
julia> signed(UInt16)
Int16
julia> signed(UInt64)
Int64signed(x)Convert a number to a signed integer. If the argument is unsigned, it is reinterpreted as signed without checking for overflow.
Base.unsigned — Function
unsigned(T::Integer)Convert an integer bitstype to the unsigned type of the same size.
Examples
julia> unsigned(Int16)
UInt16
julia> unsigned(UInt64)
UInt64Base.float — Method
float(x)Convert a number or array to a floating point data type.
See also: complex, oftype, convert.
Examples
julia> float(1:1000)
1.0:1.0:1000.0
julia> float(typemax(Int32))
2.147483647e9Base.Math.significand — Function
significand(x)Extract the significand (a.k.a. mantissa) of a floating-point number. If x is a non-zero finite number, then the result will be a number of the same type and sign as x, and whose absolute value is on the interval $[1,2)$. Otherwise x is returned.
Examples
julia> significand(15.2)
1.9
julia> significand(-15.2)
-1.9
julia> significand(-15.2) * 2^3
-15.2
julia> significand(-Inf), significand(Inf), significand(NaN)
(-Inf, Inf, NaN)Base.Math.exponent — Function
exponent(x) -> IntReturns the largest integer y such that 2^y ≤ abs(x). For a normalized floating-point number x, this corresponds to the exponent of x.
Examples
julia> exponent(8)
3
julia> exponent(64//1)
6
julia> exponent(6.5)
2
julia> exponent(16.0)
4
julia> exponent(3.142e-4)
-12Base.complex — Method
complex(r, [i])Convert real numbers or arrays to complex. i defaults to zero.
Examples
julia> complex(7)
7 + 0im
julia> complex([1, 2, 3])
3-element Vector{Complex{Int64}}:
1 + 0im
2 + 0im
3 + 0imBase.bswap — Function
bswap(n)Reverse the byte order of n.
(See also ntoh and hton to convert between the current native byte order and big-endian order.)
Examples
julia> a = bswap(0x10203040)
0x40302010
julia> bswap(a)
0x10203040
julia> string(1, base = 2)
"1"
julia> string(bswap(1), base = 2)
"100000000000000000000000000000000000000000000000000000000"Base.hex2bytes — Function
hex2bytes(itr)Given an iterable itr of ASCII codes for a sequence of hexadecimal digits, returns a Vector{UInt8} of bytes corresponding to the binary representation: each successive pair of hexadecimal digits in itr gives the value of one byte in the return vector.
The length of itr must be even, and the returned array has half of the length of itr. See also hex2bytes! for an in-place version, and bytes2hex for the inverse.
Calling hex2bytes with iterators producing UInt8 values requires Julia 1.7 or later. In earlier versions, you can collect the iterator before calling hex2bytes.
Examples
julia> s = string(12345, base = 16)
"3039"
julia> hex2bytes(s)
2-element Vector{UInt8}:
0x30
0x39
julia> a = b"01abEF"
6-element Base.CodeUnits{UInt8, String}:
0x30
0x31
0x61
0x62
0x45
0x46
julia> hex2bytes(a)
3-element Vector{UInt8}:
0x01
0xab
0xefBase.hex2bytes! — Function
hex2bytes!(dest::AbstractVector{UInt8}, itr)Convert an iterable itr of bytes representing a hexadecimal string to its binary representation, similar to hex2bytes except that the output is written in-place to dest. The length of dest must be half the length of itr.
Base.bytes2hex — Function
bytes2hex(itr) -> String
bytes2hex(io::IO, itr)Convert an iterator itr of bytes to its hexadecimal string representation, either returning a String via bytes2hex(itr) or writing the string to an io stream via bytes2hex(io, itr). The hexadecimal characters are all lowercase.
Calling bytes2hex with arbitrary iterators producing UInt8 values requires Julia 1.7 or later. In earlier versions, you can collect the iterator before calling bytes2hex.
Examples
julia> a = string(12345, base = 16)
"3039"
julia> b = hex2bytes(a)
2-element Vector{UInt8}:
0x30
0x39
julia> bytes2hex(b)
"3039"常用数值函数和常量
Base.one — Function
one(x)
one(T::type)Return a multiplicative identity for x: a value such that one(x)*x == x*one(x) == x. Alternatively one(T) can take a type T, in which case one returns a multiplicative identity for any x of type T.
If possible, one(x) returns a value of the same type as x, and one(T) returns a value of type T. However, this may not be the case for types representing dimensionful quantities (e.g. time in days), since the multiplicative identity must be dimensionless. In that case, one(x) should return an identity value of the same precision (and shape, for matrices) as x.
If you want a quantity that is of the same type as x, or of type T, even if x is dimensionful, use oneunit instead.
See also the identity function, and I in LinearAlgebra for the identity matrix.
Examples
julia> one(3.7)
1.0
julia> one(Int)
1
julia> import Dates; one(Dates.Day(1))
1Base.oneunit — Function
oneunit(x::T)
oneunit(T::Type)Return T(one(x)), where T is either the type of the argument or (if a type is passed) the argument. This differs from one for dimensionful quantities: one is dimensionless (a multiplicative identity) while oneunit is dimensionful (of the same type as x, or of type T).
Examples
julia> oneunit(3.7)
1.0
julia> import Dates; oneunit(Dates.Day)
1 dayBase.MathConstants.pi — Constant
π
piThe constant pi.
Unicode π can be typed by writing \pi then pressing tab in the Julia REPL, and in many editors.
See also: sinpi, sincospi, deg2rad.
Examples
julia> pi
π = 3.1415926535897...
julia> 1/2pi
0.15915494309189535Base.MathConstants.ℯ — Constant
ℯ
eThe constant ℯ.
Unicode ℯ can be typed by writing \euler and pressing tab in the Julia REPL, and in many editors.
Examples
julia> ℯ
ℯ = 2.7182818284590...
julia> log(ℯ)
1
julia> ℯ^(im)π ≈ -1
trueBase.MathConstants.catalan — Constant
catalanCatalan's constant.
Examples
julia> Base.MathConstants.catalan
catalan = 0.9159655941772...
julia> sum(log(x)/(1+x^2) for x in 1:0.01:10^6) * 0.01
0.9159466120554123Base.MathConstants.eulergamma — Constant
γ
eulergammaEuler's constant.
Examples
julia> Base.MathConstants.eulergamma
γ = 0.5772156649015...
julia> dx = 10^-6;
julia> sum(-exp(-x) * log(x) for x in dx:dx:100) * dx
0.5772078382499133Base.MathConstants.golden — Constant
φ
goldenThe golden ratio.
Examples
julia> Base.MathConstants.golden
φ = 1.6180339887498...
julia> (2ans - 1)^2 ≈ 5
trueBase.Inf32 — Constant
Inf32Positive infinity of type Float32.
Base.Inf16 — Constant
Inf16Positive infinity of type Float16.
Base.NaN32 — Constant
NaN32A not-a-number value of type Float32.
Base.NaN16 — Constant
NaN16A not-a-number value of type Float16.
Base.issubnormal — Function
issubnormal(f) -> BoolTest whether a floating point number is subnormal.
An IEEE floating point number is subnormal when its exponent bits are zero and its significand is not zero.
Examples
julia> floatmin(Float32)
1.1754944f-38
julia> issubnormal(1.0f-37)
false
julia> issubnormal(1.0f-38)
trueBase.isfinite — Function
isfinite(f) -> BoolTest whether a number is finite.
Examples
julia> isfinite(5)
true
julia> isfinite(NaN32)
falseBase.isone — Function
isone(x)Return true if x == one(x); if x is an array, this checks whether x is an identity matrix.
Examples
julia> isone(1.0)
true
julia> isone([1 0; 0 2])
false
julia> isone([1 0; 0 true])
trueBase.nextfloat — Function
nextfloat(x::AbstractFloat, n::Integer)The result of n iterative applications of nextfloat to x if n >= 0, or -n applications of prevfloat if n < 0.
nextfloat(x::AbstractFloat)Return the smallest floating point number y of the same type as x such x < y. If no such y exists (e.g. if x is Inf or NaN), then return x.
See also: prevfloat, eps, issubnormal.
Base.prevfloat — Function
prevfloat(x::AbstractFloat, n::Integer)The result of n iterative applications of prevfloat to x if n >= 0, or -n applications of nextfloat if n < 0.
prevfloat(x::AbstractFloat)Return the largest floating point number y of the same type as x such y < x. If no such y exists (e.g. if x is -Inf or NaN), then return x.
Base.isinteger — Function
isinteger(x) -> BoolTest whether x is numerically equal to some integer.
Examples
julia> isinteger(4.0)
trueBase.isreal — Function
isreal(x) -> BoolTest whether x or all its elements are numerically equal to some real number including infinities and NaNs. isreal(x) is true if isequal(x, real(x)) is true.
Examples
julia> isreal(5.)
true
julia> isreal(1 - 3im)
false
julia> isreal(Inf + 0im)
true
julia> isreal([4.; complex(0,1)])
falseCore.Float32 — Method
Float32(x [, mode::RoundingMode])Create a Float32 from x. If x is not exactly representable then mode determines how x is rounded.
Examples
julia> Float32(1/3, RoundDown)
0.3333333f0
julia> Float32(1/3, RoundUp)
0.33333334f0See RoundingMode for available rounding modes.
Core.Float64 — Method
Float64(x [, mode::RoundingMode])Create a Float64 from x. If x is not exactly representable then mode determines how x is rounded.
Examples
julia> Float64(pi, RoundDown)
3.141592653589793
julia> Float64(pi, RoundUp)
3.1415926535897936See RoundingMode for available rounding modes.
Base.Rounding.rounding — Function
rounding(T)Get the current floating point rounding mode for type T, controlling the rounding of basic arithmetic functions (+, -, *, / and sqrt) and type conversion.
See RoundingMode for available modes.
Base.Rounding.setrounding — Method
setrounding(T, mode)Set the rounding mode of floating point type T, controlling the rounding of basic arithmetic functions (+, -, *, / and sqrt) and type conversion. Other numerical functions may give incorrect or invalid values when using rounding modes other than the default RoundNearest.
Note that this is currently only supported for T == BigFloat.
Base.Rounding.setrounding — Method
setrounding(f::Function, T, mode)Change the rounding mode of floating point type T for the duration of f. It is logically equivalent to:
old = rounding(T)
setrounding(T, mode)
f()
setrounding(T, old)See RoundingMode for available rounding modes.
Base.Rounding.get_zero_subnormals — Function
get_zero_subnormals() -> BoolReturn false if operations on subnormal floating-point values ("denormals") obey rules for IEEE arithmetic, and true if they might be converted to zeros.
Base.Rounding.set_zero_subnormals — Function
set_zero_subnormals(yes::Bool) -> BoolIf yes is false, subsequent floating-point operations follow rules for IEEE arithmetic on subnormal values ("denormals"). Otherwise, floating-point operations are permitted (but not required) to convert subnormal inputs or outputs to zero. Returns true unless yes==true but the hardware does not support zeroing of subnormal numbers.
set_zero_subnormals(true) can speed up some computations on some hardware. However, it can break identities such as (x-y==0) == (x==y).
整型
Base.count_ones — Function
count_ones(x::Integer) -> IntegerNumber of ones in the binary representation of x.
Examples
julia> count_ones(7)
3
julia> count_ones(Int32(-1))
32Base.count_zeros — Function
count_zeros(x::Integer) -> IntegerNumber of zeros in the binary representation of x.
Examples
julia> count_zeros(Int32(2 ^ 16 - 1))
16
julia> count_zeros(-1)
0Base.leading_zeros — Function
leading_zeros(x::Integer) -> IntegerNumber of zeros leading the binary representation of x.
Examples
julia> leading_zeros(Int32(1))
31Base.leading_ones — Function
leading_ones(x::Integer) -> IntegerNumber of ones leading the binary representation of x.
Examples
julia> leading_ones(UInt32(2 ^ 32 - 2))
31Base.trailing_zeros — Function
trailing_zeros(x::Integer) -> IntegerNumber of zeros trailing the binary representation of x.
Examples
julia> trailing_zeros(2)
1Base.trailing_ones — Function
trailing_ones(x::Integer) -> IntegerNumber of ones trailing the binary representation of x.
Examples
julia> trailing_ones(3)
2Base.isodd — Function
isodd(x::Number) -> BoolReturn true if x is an odd integer (that is, an integer not divisible by 2), and false otherwise.
Examples
julia> isodd(9)
true
julia> isodd(10)
falseBase.iseven — Function
iseven(x::Number) -> BoolReturn true if x is an even integer (that is, an integer divisible by 2), and false otherwise.
Examples
julia> iseven(9)
false
julia> iseven(10)
trueCore.@int128_str — Macro
@int128_str strParse str as an Int128. Throw an ArgumentError if the string is not a valid integer.
Examples
julia> int128"123456789123"
123456789123
julia> int128"123456789123.4"
ERROR: LoadError: ArgumentError: invalid base 10 digit '.' in "123456789123.4"
[...]Core.@uint128_str — Macro
@uint128_str strParse str as an UInt128. Throw an ArgumentError if the string is not a valid integer.
Examples
julia> uint128"123456789123"
0x00000000000000000000001cbe991a83
julia> uint128"-123456789123"
ERROR: LoadError: ArgumentError: invalid base 10 digit '-' in "-123456789123"
[...]BigFloats 和 BigInts
The BigFloat and BigInt types implements arbitrary-precision floating point and integer arithmetic, respectively. For BigFloat the GNU MPFR library is used, and for BigInt the GNU Multiple Precision Arithmetic Library (GMP) is used.
Base.MPFR.BigFloat — Method
BigFloat(x::Union{Real, AbstractString} [, rounding::RoundingMode=rounding(BigFloat)]; [precision::Integer=precision(BigFloat)])Create an arbitrary precision floating point number from x, with precision precision. The rounding argument specifies the direction in which the result should be rounded if the conversion cannot be done exactly. If not provided, these are set by the current global values.
BigFloat(x::Real) is the same as convert(BigFloat,x), except if x itself is already BigFloat, in which case it will return a value with the precision set to the current global precision; convert will always return x.
BigFloat(x::AbstractString) is identical to parse. This is provided for convenience since decimal literals are converted to Float64 when parsed, so BigFloat(2.1) may not yield what you expect.
See also:
precision as a keyword argument requires at least Julia 1.1. In Julia 1.0 precision is the second positional argument (BigFloat(x, precision)).
Examples
julia> BigFloat(2.1) # 2.1 here is a Float64
2.100000000000000088817841970012523233890533447265625
julia> BigFloat("2.1") # the closest BigFloat to 2.1
2.099999999999999999999999999999999999999999999999999999999999999999999999999986
julia> BigFloat("2.1", RoundUp)
2.100000000000000000000000000000000000000000000000000000000000000000000000000021
julia> BigFloat("2.1", RoundUp, precision=128)
2.100000000000000000000000000000000000007Base.precision — Function
precision(num::AbstractFloat; base::Integer=2)
precision(T::Type; base::Integer=2)Get the precision of a floating point number, as defined by the effective number of bits in the significand, or the precision of a floating-point type T (its current default, if T is a variable-precision type like BigFloat).
If base is specified, then it returns the maximum corresponding number of significand digits in that base.
Base.MPFR.setprecision — Function
setprecision([T=BigFloat,] precision::Int; base=2)Set the precision (in bits, by default) to be used for T arithmetic. If base is specified, then the precision is the minimum required to give at least precision digits in the given base.
setprecision(f::Function, [T=BigFloat,] precision::Integer; base=2)Change the T arithmetic precision (in the given base) for the duration of f. It is logically equivalent to:
old = precision(BigFloat)
setprecision(BigFloat, precision)
f()
setprecision(BigFloat, old)Often used as setprecision(T, precision) do ... end
Note: nextfloat(), prevfloat() do not use the precision mentioned by setprecision.
Base.GMP.BigInt — Method
BigInt(x)Create an arbitrary precision integer. x may be an Int (or anything that can be converted to an Int). The usual mathematical operators are defined for this type, and results are promoted to a BigInt.
Instances can be constructed from strings via parse, or using the big string literal.
Examples
julia> parse(BigInt, "42")
42
julia> big"313"
313
julia> BigInt(10)^19
10000000000000000000Core.@big_str — Macro
@big_str strParse a string into a BigInt or BigFloat, and throw an ArgumentError if the string is not a valid number. For integers _ is allowed in the string as a separator.
Examples
julia> big"123_456"
123456
julia> big"7891.5"
7891.5
julia> big"_"
ERROR: ArgumentError: invalid number format _ for BigInt or BigFloat
[...]