Movatterモバイル変換


[0]ホーム

URL:


GitHub

Numbers

Standard Numeric Types

A type tree for all subtypes ofNumber inBase is shown below. Abstract types have been marked, the rest are concrete types.

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

Abstract number types

Core.NumberType
Number

Abstract supertype for all number types.

source
Core.RealType
Real <: Number

Abstract supertype for all real numbers.

source
Core.AbstractFloatType
AbstractFloat <: Real

Abstract supertype for all floating point numbers.

source
Core.IntegerType
Integer <: Real

Abstract supertype for all integers (e.g.Signed,Unsigned, andBool).

See alsoisinteger,trunc,div.

Examples

julia> 42 isa Integertruejulia> 1.0 isa Integerfalsejulia> isinteger(1.0)true
source
Core.SignedType
Signed <: Integer

Abstract supertype for all signed integers.

source
Core.UnsignedType
Unsigned <: Integer

Abstract supertype for all unsigned integers.

Built-in unsigned integers are printed in hexadecimal, with prefix0x, and can be entered in the same way.

Examples

julia> typemax(UInt8)0xffjulia> Int(0x00d)13julia> unsigned(true)0x0000000000000001
source
Base.AbstractIrrationalType
AbstractIrrational <: Real

Number type representing an exact irrational value, which is automatically rounded to the correct precision in arithmetic operations with other numeric quantities.

SubtypesMyIrrational <: AbstractIrrational should implement at least==(::MyIrrational, ::MyIrrational),hash(x::MyIrrational, h::UInt), andconvert(::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 integersn will give a rational result whenn is a perfect square), then it should also implementisinteger,iszero,isone, and== withReal values (since all of these default tofalse forAbstractIrrational types), as well as defininghash to equal that of the correspondingRational.

source

Concrete number types

Core.Float16Type
Float16 <: AbstractFloat <: Real

16-bit floating point number type (IEEE 754 standard). Binary format is 1 sign, 5 exponent, 10 fraction bits.

source
Core.Float32Type
Float32 <: AbstractFloat <: Real

32-bit floating point number type (IEEE 754 standard). Binary format is 1 sign, 8 exponent, 23 fraction bits.

The exponent for scientific notation should be entered as lower-casef, thus2f3 === 2.0f0 * 10^3 === Float32(2_000). For array literals and comprehensions, the element type can be specified before the square brackets:Float32[1,4,9] == Float32[i^2 for i in 1:3].

See alsoInf32,NaN32,Float16,exponent,frexp.

source
Core.Float64Type
Float64 <: AbstractFloat <: Real

64-bit floating point number type (IEEE 754 standard). Binary format is 1 sign, 11 exponent, 52 fraction bits. Seebitstring,signbit,exponent,frexp, andsignificand to access various bits.

This is the default for floating point literals,1.0 isa Float64, and for many operations such as1/2, 2pi, log(2), range(0,90,length=4). Unlike integers, this default does not change withSys.WORD_SIZE.

The exponent for scientific notation can be entered ase orE, thus2e3 === 2.0E3 === 2.0 * 10^3. Doing so is strongly preferred over10^n because integers overflow, thus2.0 * 10^19 < 0 but2e19 > 0.

See alsoInf,NaN,floatmax,Float32,Complex.

source
Base.MPFR.BigFloatType
BigFloat <: AbstractFloat

Arbitrary precision floating point number type.

source
Core.BoolType
Bool <: Integer

Boolean type, containing the valuestrue andfalse.

Bool is a kind of number:false is numerically equal to0 andtrue is numerically equal to1. Moreover,false acts as a multiplicative "strong zero" againstNaN andInf:

julia> [true, false] == [1, 0]truejulia> 42.0 + true43.0julia> 0 .* (NaN, Inf, -Inf)(NaN, NaN, NaN)julia> false .* (NaN, Inf, -Inf)(0.0, 0.0, -0.0)

Branches viaif and other conditionals only acceptBool. There are no "truthy" values in Julia.

Comparisons typically returnBool, and broadcasted comparisons may returnBitArray instead of anArray{Bool}.

julia> [1 2 3 4 5] .< pi1×5 BitMatrix: 1  1  1  0  0julia> map(>(pi), [1 2 3 4 5])1×5 Matrix{Bool}: 0  0  0  1  1

See alsotrues,falses,ifelse.

source
Core.Int8Type
Int8 <: Signed <: Integer

8-bit signed integer type.

Represents numbersn ∈ -128:127. Note that such integers overflow without warning, thustypemax(Int8) + Int8(1) < 0.

See alsoInt,widen,BigInt.

source
Core.UInt8Type
UInt8 <: Unsigned <: Integer

8-bit unsigned integer type.

Printed in hexadecimal, thus 0x07 == 7.

source
Core.Int16Type
Int16 <: Signed <: Integer

16-bit signed integer type.

Represents numbersn ∈ -32768:32767. Note that such integers overflow without warning, thustypemax(Int16) + Int16(1) < 0.

See alsoInt,widen,BigInt.

source
Core.UInt16Type
UInt16 <: Unsigned <: Integer

16-bit unsigned integer type.

Printed in hexadecimal, thus 0x000f == 15.

source
Core.Int32Type
Int32 <: Signed <: Integer

32-bit signed integer type.

Note that such integers overflow without warning, thustypemax(Int32) + Int32(1) < 0.

See alsoInt,widen,BigInt.

source
Core.UInt32Type
UInt32 <: Unsigned <: Integer

32-bit unsigned integer type.

Printed in hexadecimal, thus 0x0000001f == 31.

source
Core.Int64Type
Int64 <: Signed <: Integer

64-bit signed integer type.

Note that such integers overflow without warning, thustypemax(Int64) + Int64(1) < 0.

See alsoInt,widen,BigInt.

source
Core.UInt64Type
UInt64 <: Unsigned <: Integer

64-bit unsigned integer type.

Printed in hexadecimal, thus 0x000000000000003f == 63.

source
Core.Int128Type
Int128 <: Signed <: Integer

128-bit signed integer type.

Note that such integers overflow without warning, thustypemax(Int128) + Int128(1) < 0.

See alsoInt,widen,BigInt.

source
Core.UInt128Type
UInt128 <: Unsigned <: Integer

128-bit unsigned integer type.

Printed in hexadecimal, thus 0x0000000000000000000000000000007f == 127.

source
Core.IntType
Int

Sys.WORD_SIZE-bit signed integer type,Int <: Signed <: Integer <: Real.

This is the default type of most integer literals and is an alias for eitherInt32 orInt64, depending onSys.WORD_SIZE. It is the type returned by functions such aslength, and the standard type for indexing arrays.

Note that integers overflow without warning, thustypemax(Int) + 1 < 0 and10^19 < 0. Overflow can be avoided by usingBigInt. Very large integer literals will use a wider type, for instance10_000_000_000_000_000_000 isa Int128.

Integer division isdiv alias÷, whereas/ acting on integers returnsFloat64.

See alsoInt64,widen,typemax,bitstring.

source
Core.UIntType
UInt

Sys.WORD_SIZE-bit unsigned integer type,UInt <: Unsigned <: Integer.

LikeInt, the aliasUInt may point to eitherUInt32 orUInt64, according to the value ofSys.WORD_SIZE on a given computer.

Printed and parsed in hexadecimal:UInt(15) === 0x000000000000000f.

source
Base.GMP.BigIntType
BigInt <: Signed

Arbitrary precision integer type.

source
Base.ComplexType
Complex{T<:Real} <: Number

Complex number type with real and imaginary part of typeT.

ComplexF16,ComplexF32 andComplexF64 are aliases forComplex{Float16},Complex{Float32} andComplex{Float64} respectively.

See also:Real,complex,real.

source
Base.RationalType
Rational{T<:Integer} <: Real

Rational number type, with numerator and denominator of typeT. Rationals are checked for overflow.

source
Base.IrrationalType
Irrational{sym} <: AbstractIrrational

Number type representing an exact irrational value denoted by the symbolsym, such asπ, andγ.

See alsoAbstractIrrational.

source

Data Formats

Base.digitsFunction
digits([T<:Integer], n::Integer; base::T = 10, pad::Integer = 1)

Return an array with element typeT (defaultInt) of the digits ofn in the given base, optionally padded with zeros to a specified size. More significant digits are at higher indices, such thatn == sum(digits[k]*base^(k-1) for k in eachindex(digits)).

See alsondigits,digits!, and for base 2 alsobitstring,count_ones.

Examples

julia> digits(10)2-element Vector{Int64}: 0 1julia> digits(10, base = 2)4-element Vector{Int64}: 0 1 0 1julia> digits(-256, base = 10, pad = 5)5-element Vector{Int64}: -6 -5 -2  0  0julia> n = rand(-999:999);julia> n == evalpoly(13, digits(n, base = 13))true
source
Base.digits!Function
digits!(array, n::Integer; base::Integer = 10)

Fills an array of the digits ofn 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 1julia> digits!([2, 2, 2, 2, 2, 2], 10, base = 2)6-element Vector{Int64}: 0 1 0 1 0 0
source
Base.bitstringFunction
bitstring(n)

A string giving the literal bit representation of a primitive type.

See alsocount_ones,count_zeros,digits.

Examples

julia> bitstring(Int32(4))"00000000000000000000000000000100"julia> bitstring(2.2)"0100000000000001100110011001100110011001100110011001100110011010"
source
Base.parseFunction
parse(::Type{SimpleColor}, rgb::String)

An analogue oftryparse(SimpleColor, rgb::String) (which see), that raises an error instead of returningnothing.

parse(::Type{Platform}, triplet::AbstractString)

Parses a string platform triplet back into aPlatform object.

source
parse(type, str; base)

Parse a string as a number. ForInteger 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 aComplex(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.

Julia 1.1

parse(Bool, str) requires at least Julia 1.1.

Examples

julia> parse(Int, "1234")1234julia> parse(Int, "1234", base = 5)194julia> parse(Int, "afc", base = 16)2812julia> parse(Float64, "1.2e-3")0.0012julia> parse(Complex{Float64}, "3.2e-1 + 4.5im")0.32 + 4.5im
source
Base.tryparseFunction
tryparse(::Type{SimpleColor}, rgb::String)

Attempt to parsergb as aSimpleColor. Ifrgb starts with# and has a length of 7, it is converted into aRGBTuple-backedSimpleColor. Ifrgb starts witha-z,rgb is interpreted as a color name and converted to aSymbol-backedSimpleColor.

Otherwise,nothing is returned.

Examples

julia> tryparse(SimpleColor, "blue")SimpleColor(blue)julia> tryparse(SimpleColor, "#9558b2")SimpleColor(#9558b2)julia> tryparse(SimpleColor, "#nocolor")
tryparse(type, str; base)

Likeparse, but returns either a value of the requested type, ornothing if the string does not contain a valid number.

source
Base.bigFunction
big(x)

Convert a number to a maximum precision representation (typicallyBigInt orBigFloat). SeeBigFloat for information about some pitfalls with floating-point numbers.

source
Base.signedFunction
signed(T::Integer)

Convert an integer bitstype to the signed type of the same size.

Examples

julia> signed(UInt16)Int16julia> signed(UInt64)Int64
source
signed(x)

Convert a number to a signed integer. If the argument is unsigned, it is reinterpreted as signed without checking for overflow.

See also:unsigned,sign,signbit.

source
Base.unsignedFunction
unsigned(T::Integer)

Convert an integer bitstype to the unsigned type of the same size.

Examples

julia> unsigned(Int16)UInt16julia> unsigned(UInt64)UInt64
source
Base.floatMethod
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.0julia> float(typemax(Int32))2.147483647e9
source
Base.Math.significandFunction
significand(x)

Extract the significand (a.k.a. mantissa) of a floating-point number. Ifx is a non-zero finite number, then the result will be a number of the same type and sign asx, and whose absolute value is on the interval$[1,2)$. Otherwisex is returned.

See alsofrexp,exponent.

Examples

julia> significand(15.2)1.9julia> significand(-15.2)-1.9julia> significand(-15.2) * 2^3-15.2julia> significand(-Inf), significand(Inf), significand(NaN)(-Inf, Inf, NaN)
source
Base.Math.exponentFunction
exponent(x::Real) -> Int

Returns the largest integery such that2^y ≤ abs(x).

Throws aDomainError whenx is zero, infinite, orNaN. For any other non-subnormal floating-point numberx, this corresponds to the exponent bits ofx.

See alsosignbit,significand,frexp,issubnormal,log2,ldexp.

Examples

julia> exponent(8)3julia> exponent(6.5)2julia> exponent(-1//4)-2julia> exponent(3.142e-4)-12julia> exponent(floatmin(Float32)), exponent(nextfloat(0.0f0))(-126, -149)julia> exponent(0.0)ERROR: DomainError with 0.0:Cannot be ±0.0.[...]
source
Base.complexMethod
complex(r, [i])

Convert real numbers or arrays to complex.i defaults to zero.

Examples

julia> complex(7)7 + 0imjulia> complex([1, 2, 3])3-element Vector{Complex{Int64}}: 1 + 0im 2 + 0im 3 + 0im
source
Base.bswapFunction
bswap(n)

Reverse the byte order ofn.

(See alsontoh andhton to convert between the current native byte order and big-endian order.)

Examples

julia> a = bswap(0x10203040)0x40302010julia> bswap(a)0x10203040julia> string(1, base = 2)"1"julia> string(bswap(1), base = 2)"100000000000000000000000000000000000000000000000000000000"
source
Base.hex2bytesFunction
hex2bytes(itr)

Given an iterableitr of ASCII codes for a sequence of hexadecimal digits, returns aVector{UInt8} of bytes corresponding to the binary representation: each successive pair of hexadecimal digits initr gives the value of one byte in the return vector.

The length ofitr must be even, and the returned array has half of the length ofitr. See alsohex2bytes! for an in-place version, andbytes2hex for the inverse.

Julia 1.7

Callinghex2bytes with iterators producingUInt8 values requires Julia 1.7 or later. In earlier versions, you cancollect the iterator before callinghex2bytes.

Examples

julia> s = string(12345, base = 16)"3039"julia> hex2bytes(s)2-element Vector{UInt8}: 0x30 0x39julia> a = b"01abEF"6-element Base.CodeUnits{UInt8, String}: 0x30 0x31 0x61 0x62 0x45 0x46julia> hex2bytes(a)3-element Vector{UInt8}: 0x01 0xab 0xef
source
Base.hex2bytes!Function
hex2bytes!(dest::AbstractVector{UInt8}, itr)

Convert an iterableitr of bytes representing a hexadecimal string to its binary representation, similar tohex2bytes except that the output is written in-place todest. The length ofdest must be half the length ofitr.

Julia 1.7

Calling hex2bytes! with iterators producing UInt8 requires version 1.7. In earlier versions, you can collect the iterable before calling instead.

source
Base.bytes2hexFunction
bytes2hex(itr) -> Stringbytes2hex(io::IO, itr)

Convert an iteratoritr of bytes to its hexadecimal string representation, either returning aString viabytes2hex(itr) or writing the string to anio stream viabytes2hex(io, itr). The hexadecimal characters are all lowercase.

Julia 1.7

Callingbytes2hex with arbitrary iterators producingUInt8 values requires Julia 1.7 or later. In earlier versions, you cancollect the iterator before callingbytes2hex.

Examples

julia> a = string(12345, base = 16)"3039"julia> b = hex2bytes(a)2-element Vector{UInt8}: 0x30 0x39julia> bytes2hex(b)"3039"
source

General Number Functions and Constants

Base.oneFunction
one(x)one(T::type)

Return a multiplicative identity forx: a value such thatone(x)*x == x*one(x) == x. Alternativelyone(T) can take a typeT, in which caseone returns a multiplicative identity for anyx of typeT.

If possible,one(x) returns a value of the same type asx, andone(T) returns a value of typeT. 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) asx.

If you want a quantity that is of the same type asx, or of typeT, even ifx is dimensionful, useoneunit instead.

See also theidentity function, andI inLinearAlgebra for the identity matrix.

Examples

julia> one(3.7)1.0julia> one(Int)1julia> import Dates; one(Dates.Day(1))1
source
Base.oneunitFunction
oneunit(x::T)oneunit(T::Type)

ReturnT(one(x)), whereT is either the type of the argument or (if a type is passed) the argument. This differs fromone for dimensionful quantities:one is dimensionless (a multiplicative identity) whileoneunit is dimensionful (of the same type asx, or of typeT).

Examples

julia> oneunit(3.7)1.0julia> import Dates; oneunit(Dates.Day)1 day
source
Base.zeroFunction
zero(x)zero(::Type)

Get the additive identity element for the type ofx (x can also specify the type itself).

See alsoiszero,one,oneunit,oftype.

Examples

julia> zero(1)0julia> zero(big"2.0")0.0julia> zero(rand(2,2))2×2 Matrix{Float64}: 0.0  0.0 0.0  0.0
source
Base.imConstant
im

The imaginary unit.

See also:imag,angle,complex.

Examples

julia> im * im-1 + 0imjulia> (2.0 + 3im)^2-5.0 + 12.0im
source
Base.MathConstants.piConstant
πpi

The 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/2pi0.15915494309189535
source
Base.MathConstants.ℯConstant
ℯe

The constant ℯ.

Unicode can be typed by writing\euler and pressing tab in the Julia REPL, and in many editors.

See also:exp,cis,cispi.

Examples

julia> ℯℯ = 2.7182818284590...julia> log(ℯ)1julia> ℯ^(im)π ≈ -1true
source
Base.MathConstants.catalanConstant
catalan

Catalan's constant.

Examples

julia> Base.MathConstants.catalancatalan = 0.9159655941772...julia> sum(log(x)/(1+x^2) for x in 1:0.01:10^6) * 0.010.9159466120554123
source
Base.MathConstants.eulergammaConstant
γeulergamma

Euler'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) * dx0.5772078382499133
source
Base.MathConstants.goldenConstant
φgolden

The golden ratio.

Examples

julia> Base.MathConstants.goldenφ = 1.6180339887498...julia> (2ans - 1)^2 ≈ 5true
source
Base.InfConstant
Inf, Inf64

Positive infinity of typeFloat64.

See also:isfinite,typemax,NaN,Inf32.

Examples

julia> π/0Infjulia> +1.0 / -0.0-Infjulia> ℯ^-Inf0.0
source
Base.Inf64Constant
Inf, Inf64

Positive infinity of typeFloat64.

See also:isfinite,typemax,NaN,Inf32.

Examples

julia> π/0Infjulia> +1.0 / -0.0-Infjulia> ℯ^-Inf0.0
source
Base.Inf32Constant
Inf32

Positive infinity of typeFloat32.

source
Base.Inf16Constant
Inf16

Positive infinity of typeFloat16.

source
Base.NaNConstant
NaN, NaN64

A not-a-number value of typeFloat64.

See also:isnan,missing,NaN32,Inf.

Examples

julia> 0/0NaNjulia> Inf - InfNaNjulia> NaN == NaN, isequal(NaN, NaN), isnan(NaN)(false, true, true)
Note

Always useisnan orisequal for checking forNaN. Usingx === NaN may give unexpected results:

julia> reinterpret(UInt32, NaN32)0x7fc00000julia> NaN32p1 = reinterpret(Float32, 0x7fc00001)NaN32julia> NaN32p1 === NaN32, isequal(NaN32p1, NaN32), isnan(NaN32p1)(false, true, true)
source
Base.NaN64Constant
NaN, NaN64

A not-a-number value of typeFloat64.

See also:isnan,missing,NaN32,Inf.

Examples

julia> 0/0NaNjulia> Inf - InfNaNjulia> NaN == NaN, isequal(NaN, NaN), isnan(NaN)(false, true, true)
Note

Always useisnan orisequal for checking forNaN. Usingx === NaN may give unexpected results:

julia> reinterpret(UInt32, NaN32)0x7fc00000julia> NaN32p1 = reinterpret(Float32, 0x7fc00001)NaN32julia> NaN32p1 === NaN32, isequal(NaN32p1, NaN32), isnan(NaN32p1)(false, true, true)
source
Base.NaN32Constant
NaN32

A not-a-number value of typeFloat32.

See also:NaN.

source
Base.NaN16Constant
NaN16

A not-a-number value of typeFloat16.

See also:NaN.

source
Base.issubnormalFunction
issubnormal(f) -> Bool

Test whether a floating point number is subnormal.

An IEEE floating point number issubnormal when its exponent bits are zero and its significand is not zero.

Examples

julia> floatmin(Float32)1.1754944f-38julia> issubnormal(1.0f-37)falsejulia> issubnormal(1.0f-38)true
source
Base.isfiniteFunction
isfinite(f) -> Bool

Test whether a number is finite.

Examples

julia> isfinite(5)truejulia> isfinite(NaN32)false
source
Base.isinfFunction
isinf(f) -> Bool

Test whether a number is infinite.

See also:Inf,iszero,isfinite,isnan.

source
Base.isnanFunction
isnan(f) -> Bool

Test whether a number value is a NaN, an indeterminate value which is neither an infinity nor a finite number ("not a number").

See also:iszero,isone,isinf,ismissing.

source
Base.iszeroFunction
iszero(x)

Returntrue ifx == zero(x); ifx is an array, this checks whether all of the elements ofx are zero.

See also:isone,isinteger,isfinite,isnan.

Examples

julia> iszero(0.0)truejulia> iszero([1, 9, 0])falsejulia> iszero([false, 0, 0])true
source
Base.isoneFunction
isone(x)

Returntrue ifx == one(x); ifx is an array, this checks whetherx is an identity matrix.

Examples

julia> isone(1.0)truejulia> isone([1 0; 0 2])falsejulia> isone([1 0; 0 true])true
source
Base.nextfloatFunction
nextfloat(x::AbstractFloat, n::Integer)

The result ofn iterative applications ofnextfloat tox ifn >= 0, or-n applications ofprevfloat ifn < 0.

source
nextfloat(x::AbstractFloat)

Return the smallest floating point numbery of the same type asx suchx < y. If no suchy exists (e.g. ifx isInf orNaN), then returnx.

See also:prevfloat,eps,issubnormal.

source
Base.prevfloatFunction
prevfloat(x::AbstractFloat, n::Integer)

The result ofn iterative applications ofprevfloat tox ifn >= 0, or-n applications ofnextfloat ifn < 0.

source
prevfloat(x::AbstractFloat)

Return the largest floating point numbery of the same type asx suchy < x. If no suchy exists (e.g. ifx is-Inf orNaN), then returnx.

source
Base.isintegerFunction
isinteger(x) -> Bool

Test whetherx is numerically equal to some integer.

Examples

julia> isinteger(4.0)true
source
Base.isrealFunction
isreal(x) -> Bool

Test whetherx or all its elements are numerically equal to some real number including infinities and NaNs.isreal(x) is true ifisequal(x, real(x)) is true.

Examples

julia> isreal(5.)truejulia> isreal(1 - 3im)falsejulia> isreal(Inf + 0im)truejulia> isreal([4.; complex(0,1)])false
source
Core.Float32Method
Float32(x [, mode::RoundingMode])

Create aFloat32 fromx. Ifx is not exactly representable thenmode determines howx is rounded.

Examples

julia> Float32(1/3, RoundDown)0.3333333f0julia> Float32(1/3, RoundUp)0.33333334f0

SeeRoundingMode for available rounding modes.

source
Core.Float64Method
Float64(x [, mode::RoundingMode])

Create aFloat64 fromx. Ifx is not exactly representable thenmode determines howx is rounded.

Examples

julia> Float64(pi, RoundDown)3.141592653589793julia> Float64(pi, RoundUp)3.1415926535897936

SeeRoundingMode for available rounding modes.

source
Base.Rounding.roundingFunction
rounding(T)

Get the current floating point rounding mode for typeT, controlling the rounding of basic arithmetic functions (+,-,*,/ andsqrt) and type conversion.

SeeRoundingMode for available modes.

source
Base.Rounding.setroundingMethod
setrounding(T, mode)

Set the rounding mode of floating point typeT, controlling the rounding of basic arithmetic functions (+,-,*,/ andsqrt) and type conversion. Other numerical functions may give incorrect or invalid values when using rounding modes other than the defaultRoundNearest.

Note that this is currently only supported forT == BigFloat.

Warning

This function is not thread-safe. It will affect code running on all threads, but its behavior is undefined if called concurrently with computations that use the setting.

source
Base.Rounding.setroundingMethod
setrounding(f::Function, T, mode)

Change the rounding mode of floating point typeT for the duration off. It is logically equivalent to:

old = rounding(T)setrounding(T, mode)f()setrounding(T, old)

SeeRoundingMode for available rounding modes.

source
Base.Rounding.get_zero_subnormalsFunction
get_zero_subnormals() -> Bool

Returnfalse if operations on subnormal floating-point values ("denormals") obey rules for IEEE arithmetic, andtrue if they might be converted to zeros.

Warning

This function only affects the current thread.

source
Base.Rounding.set_zero_subnormalsFunction
set_zero_subnormals(yes::Bool) -> Bool

Ifyes isfalse, 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. Returnstrue unlessyes==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).

Warning

This function only affects the current thread.

source

Integers

Base.count_onesFunction
count_ones(x::Integer) -> Integer

Number of ones in the binary representation ofx.

Examples

julia> count_ones(7)3julia> count_ones(Int32(-1))32
source
Base.count_zerosFunction
count_zeros(x::Integer) -> Integer

Number of zeros in the binary representation ofx.

Examples

julia> count_zeros(Int32(2 ^ 16 - 1))16julia> count_zeros(-1)0
source
Base.leading_zerosFunction
leading_zeros(x::Integer) -> Integer

Number of zeros leading the binary representation ofx.

Examples

julia> leading_zeros(Int32(1))31
source
Base.leading_onesFunction
leading_ones(x::Integer) -> Integer

Number of ones leading the binary representation ofx.

Examples

julia> leading_ones(UInt32(2 ^ 32 - 2))31
source
Base.trailing_zerosFunction
trailing_zeros(x::Integer) -> Integer

Number of zeros trailing the binary representation ofx.

Examples

julia> trailing_zeros(2)1
source
Base.trailing_onesFunction
trailing_ones(x::Integer) -> Integer

Number of ones trailing the binary representation ofx.

Examples

julia> trailing_ones(3)2
source
Base.isoddFunction
isodd(x::Number) -> Bool

Returntrue ifx is an odd integer (that is, an integer not divisible by 2), andfalse otherwise.

Julia 1.7

Non-Integer arguments require Julia 1.7 or later.

Examples

julia> isodd(9)truejulia> isodd(10)false
source
Base.isevenFunction
iseven(x::Number) -> Bool

Returntrue ifx is an even integer (that is, an integer divisible by 2), andfalse otherwise.

Julia 1.7

Non-Integer arguments require Julia 1.7 or later.

Examples

julia> iseven(9)falsejulia> iseven(10)true
source
Core.@int128_strMacro
@int128_str str

Parsestr as anInt128. Throw anArgumentError if the string is not a valid integer.

Examples

julia> int128"123456789123"123456789123julia> int128"123456789123.4"ERROR: LoadError: ArgumentError: invalid base 10 digit '.' in "123456789123.4"[...]
source
Core.@uint128_strMacro
@uint128_str str

Parsestr as anUInt128. Throw anArgumentError if the string is not a valid integer.

Examples

julia> uint128"123456789123"0x00000000000000000000001cbe991a83julia> uint128"-123456789123"ERROR: LoadError: ArgumentError: invalid base 10 digit '-' in "-123456789123"[...]
source

BigFloats and BigInts

TheBigFloat andBigInt types implements arbitrary-precision floating point and integer arithmetic, respectively. ForBigFloat theGNU MPFR library is used, and forBigInt theGNU Multiple Precision Arithmetic Library (GMP) is used.

Base.MPFR.BigFloatMethod
BigFloat(x::Union{Real, AbstractString} [, rounding::RoundingMode=rounding(BigFloat)]; [precision::Integer=precision(BigFloat)])

Create an arbitrary precision floating point number fromx, with precisionprecision. Therounding 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 asconvert(BigFloat,x), except ifx itself is alreadyBigFloat, in which case it will return a value with the precision set to the current global precision;convert will always returnx.

BigFloat(x::AbstractString) is identical toparse. This is provided for convenience since decimal literals are converted toFloat64 when parsed, soBigFloat(2.1) may not yield what you expect.

See also:

Julia 1.1

precision as a keyword argument requires at least Julia 1.1. In Julia 1.0precision is the second positional argument (BigFloat(x, precision)).

Examples

julia> BigFloat(2.1) # 2.1 here is a Float642.100000000000000088817841970012523233890533447265625julia> BigFloat("2.1") # the closest BigFloat to 2.12.099999999999999999999999999999999999999999999999999999999999999999999999999986julia> BigFloat("2.1", RoundUp)2.100000000000000000000000000000000000000000000000000000000000000000000000000021julia> BigFloat("2.1", RoundUp, precision=128)2.100000000000000000000000000000000000007
source
Base.precisionFunction
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 typeT (its current default, ifT is a variable-precision type likeBigFloat).

Ifbase is specified, then it returns the maximum corresponding number of significand digits in that base.

Julia 1.8

Thebase keyword requires at least Julia 1.8.

source
Base.MPFR.setprecisionFunction
setprecision([T=BigFloat,] precision::Int; base=2)

Set the precision (in bits, by default) to be used forT arithmetic. Ifbase is specified, then the precision is the minimum required to give at leastprecision digits in the givenbase.

Warning

This function is not thread-safe. It will affect code running on all threads, but its behavior is undefined if called concurrently with computations that use the setting.

Julia 1.8

Thebase keyword requires at least Julia 1.8.

source
setprecision(f::Function, [T=BigFloat,] precision::Integer; base=2)

Change theT arithmetic precision (in the givenbase) for the duration off. It is logically equivalent to:

old = precision(BigFloat)setprecision(BigFloat, precision)f()setprecision(BigFloat, old)

Often used assetprecision(T, precision) do ... end

Note:nextfloat(),prevfloat() do not use the precision mentioned bysetprecision.

Julia 1.8

Thebase keyword requires at least Julia 1.8.

source
Base.GMP.BigIntMethod
BigInt(x)

Create an arbitrary precision integer.x may be anInt (or anything that can be converted to anInt). The usual mathematical operators are defined for this type, and results are promoted to aBigInt.

Instances can be constructed from strings viaparse, or using thebig string literal.

Examples

julia> parse(BigInt, "42")42julia> big"313"313julia> BigInt(10)^1910000000000000000000
source
Core.@big_strMacro
@big_str str

Parse a string into aBigInt orBigFloat, and throw anArgumentError if the string is not a valid number. For integers_ is allowed in the string as a separator.

Examples

julia> big"123_456"123456julia> big"7891.5"7891.5julia> big"_"ERROR: ArgumentError: invalid number format _ for BigInt or BigFloat[...]
Warning

Using@big_str for constructingBigFloat values may not result in the behavior that might be naively expected: as a macro,@big_str obeys the global precision (setprecision) and rounding mode (setrounding) settings as they are atload time. Thus, a function like() -> precision(big"0.3") returns a constant whose value depends on the value of the precision at the point when the function is defined,not at the precision at the time when the function is called.

source

Settings


This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.


[8]ページ先頭

©2009-2025 Movatter.jp