Numbers
Standard numeric types
The type tree for all subtypes of `Number' in `Base' is shown below. Abstract types are marked, other types are concrete.
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 numeric types
#
Core.AbstractFloat
— Type
AbstractFloat <: Real
An abstract supertype for all floating-point numbers.
#
Core.Unsigned
— Type
Unsigned <: Integer
An abstract supertype for all unsigned integers.
Embedded unsigned integers are output in hexadecimal format with the prefix 0x
and can be entered in the same way.
Examples
julia> typemax(UInt8) 0xff julia> Int(0x00d) 13 julia> unsigned(true) 0x0000000000000001
#
Base.AbstractIrrational
— Type
AbstractIrrational <: Real
A numeric type representing an exact irrational value that is automatically rounded to the correct precision in arithmetic operations with other numerical values.
The subtypes MyIrrational <: AbstractIrrational
must 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 can be rational (for example, a square root type representing √n
for integers n
yields a rational result if n
is a complete square), then it must also implement isinteger
, iszero
, isone
and ==
with Real
values (since all these default values are false
for the abstractirational
types), and define hash
is equal to the value of the corresponding Rational
.
Specific numeric types
#
Core.Float16
— Type
Float16 <: AbstractFloat <: Real
16-bit floating point number type (IEEE 754 standard). Binary format: 1 bit of the sign, 5 bits of the exponent, 10 bits of the fractional part.
#
Core.Float32
— Type
Float32 <: AbstractFloat <: Real
32-bit floating point number type (IEEE 754 standard). Binary format: 1 bit of the sign, 8 bits of the exponent, 23 bits of the fractional part.
The exponent for the exponential representation should be entered using the lowercase letter f
, that is, 2f3 === 2.0f0* 10^3 === Float32(2_000)
. For array literals and inclusions, the element type can be specified before square brackets: Float32[1,4,9] == Float32[i^2 for i in 1:3]
.
#
Core.Float64
— Type
Float64 <: AbstractFloat <: Real
64-bit floating point number type (IEEE 754 standard). Binary format: 1 bit of the sign, 11 bits of the exponent, 52 bits of the fractional part. To access the various bits, use bitstring
, signbit
, exponent
, frexp
and significand
.
It is used by default for floating-point literals, 1.0 isa Float64
, and for many operations such as 1/2, 2pi, log(2), range(0.90,length=4)
. Unlike integers, this default value does not change according to `Sys.WORD_SIZE'.
The exponent for the exponential representation can be entered using e
or E
, that is, 2e3 === 2.0E3 === 2.0 * 10^3
. This is much preferable to 10^n
because integers overflow, so 2.0 * 10^19 < 0
, but `2e19 > 0'.
#
Base.MPFR.BigFloat
— Type
BigFloat <: AbstractFloat
A type of floating-point number with arbitrary precision.
#
Core.Bool
— Type
Bool <: Integer
A boolean type containing the values true
and `false'.
'Bool` is a type of number: false
is numerically equal to 0
, and true
is 1
. In addition, false
acts as a multiplicative "strong zero" for NaN
and Inf
.
julia> [true, false] == [1, 0]
true
julia> 42.0 + true
43.0
julia> 0 .* (NaN, Inf, -Inf)
(NaN, NaN, NaN)
julia> false .* (NaN, Inf, -Inf)
(0.0, 0.0, -0.0)
For branching by if
and other conditional statements are accepted only by `Bool'. There are no "true" values in Julia.
Comparisons usually return Bool
, and broadcast comparisons can return BitArray
instead of Array{Bool}
.
julia> [1 2 3 4 5] .< pi
1×5 BitMatrix:
1 1 1 0 0
julia> map(>(pi), [1 2 3 4 5])
1×5 Matrix{Bool}:
0 0 0 1 1
#
Core.UInt8
— Type
UInt8 <: Unsigned <: Integer
An unsigned 8-bit integer type.
It is output in hexadecimal format, i.e. 0x07 == 7.
#
Core.UInt16
— Type
UInt16 <: Unsigned <: Integer
An unsigned 16-bit integer type.
It is output in hexadecimal format, i.e. 0x000f == 15.
#
Core.UInt32
— Type
UInt32 <: Unsigned <: Integer
An unsigned 32-bit integer type.
It is output in hexadecimal format, i.e. 0x0000001f == 31.
#
Core.UInt64
— Type
UInt64 <: Unsigned <: Integer
An unsigned 64-bit integer type.
It is output in hexadecimal format, i.e. 0x000000000000003f == 63.
#
Core.UInt128
— Type
UInt128 <: Unsigned <: Integer
An unsigned 128-bit integer type.
It is output in hexadecimal format, i.e. 0x0000000000000000000000000000007f == 127.
#
Core.Int
— Type
Int
Signed integer type with the size of Sys.WORD_SIZE digits, Int <: Signed <: Integer <: Real
.
This is the default type for most integer literals and an alias for Int32
or Int64
depending on Sys.WORD_SIZE'. This is the type returned by functions such as `length
, and the standard type for indexing arrays.
Note that integers overflow without warning, so typemax(Int) + 1 < 0
and 10^19 < 0
. Overflow can be avoided by using BigInt
. For very large integer literals, a wider type will be used, for example 10_000_000_000_000_000_000 isa Int128
.
#
Core.UInt
— Type
UInt
An unsigned integer type with the size of Sys.WORD_SIZE digits, UInt <: Unsigned <: Integer
.
As in the case of Int
, the alias UInt' can mean either `UInt32
or UInt64
depending on the value of Sys.WORD_SIZE
on this computer.
It is output and analyzed in hexadecimal format: `UInt(15) === 0x000000000000000f'.
#
Base.Rational
— Type
Rational{T<:Integer} <: Real
A type of rational number with a numerator and denominator of type `T'. Rational numbers are checked for overflow.
#
Base.Irrational
— Type
Irrational{sym} <: AbstractIrrational
A numeric type representing an exact irrational value, denoted by the symbol sym
, such as π
, ℯ
and γ
.
See also the description AbstractIrrational
.
Data formats
#
Base.digits
— Function
digits([T<:Integer], n::Integer; base::T = 10, pad::Integer = 1)
Returns an array with the element type T' (by default `Int
) containing the digits n
in the specified number system, optionally filled with zeros up to the specified size. The digits of a higher order are located at higher indexes, so that n == sum(digits[k]*base^(k-1) for k in each index(digits))
.
See also the description ndigits
, digits!
, and for base 2 — 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))
true
#
Base.digits!
— Function
digits!(array, n::Integer; base::Integer = 10)
Fills in the array of digits n
in the specified number system. The numbers of a higher order are at higher indexes. If the length of the array is insufficient, the digits of the lower order are filled in until the length of the array is reached. If the array is too long, the excess part 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
0
#
Base.bitstring
— Function
bitstring(n)
A string with a literal bit representation of a primitive type.
See also the description count_ones
, count_zeros
and digits
.
Examples
julia> bitstring(Int32(4))
"00000000000000000000000000000100"
julia> bitstring(2.2)
"0100000000000001100110011001100110011001100110011001100110011010"
#
Base.parse
— Function
parse(::Type{SimpleColor}, rgb::String)
An analog of tryparse(SimpleColor, rgb::String)
, which returns an error instead of returning nothing
.
parse(::Type{Platform}, triplet::AbstractString)
Analyzes the platform string triplet back into the 'Platform` object.
parse(type, str; base)
Analyzes a string as a number. The base can be set for the Integer
types (the default value is 10). For floating-point types, the string is parsed as a decimal floating-point number. The Complex
types are analyzed from decimal strings of the form "R±Iim"
as Complex(R,I)
of the requested type; "i"
or "j"
can also be used instead of "im"
. In addition, "R"
or "Iim"
is allowed. If the string does not contain a valid number, an error occurs.
Compatibility: Julia 1.1
For |
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.5im
#
Base.tryparse
— Function
tryparse(::Type{SimpleColor}, rgb::String)
Tries to analyze rgb
as SimpleColor'. If the `rgb
value starts with #
and has a length of 7, it is converted to SimpleColor
based on RGBTuple'. If the value of `rgb
starts with a
--z
, rgb
is interpreted as the name of the color and converted to SimpleColor
based on the `Symbol'.
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)
#
Base.signed
— Function
signed(T::Integer)
Converts an integer bit type to a signed type of the same size.
Examples
julia> signed(UInt16)
Int16
julia> signed(UInt64)
Int64
signed(x)
Converts a number to a signed integer. If the argument is unsigned, it is reinterpreted as a signed argument without checking for overflow.
#
Base.unsigned
— Function
unsigned(T::Integer)
Converts an integer bit type to an unsigned type of the same size.
Examples
julia> unsigned(Int16)
UInt16
julia> unsigned(UInt64)
UInt64
#
Base.Math.significand
— Function
significand(x)
Extracts the significant digit (mantissa) floating-point numbers. If 'x` is a non-zero finite number, then the result will be a number of the same type and with the same sign (as x
), the absolute value of which is in the range . 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::Real) -> Int
Returns the largest integer y
such that 2^y ≤ abs(x)
.
Returns a DomainError
when x
is zero, infinity, or NaN
. For any other non-subnormal floating-point number x
, this corresponds to the bits of the exponent `x'.
See also the description signbit
, significand
, frexp
, issubnormal
, log2
, ldexp
.
Examples
julia> exponent(8)
3
julia> exponent(6.5)
2
julia> exponent(-1//4)
-2
julia> exponent(3.142e-4)
-12
julia> exponent(floatmin(Float32)), exponent(nextfloat(0.0f0))
(-126, -149)
julia> exponent(0.0)
ERROR: DomainError with 0.0:
Cannot be ±0.0.
[...]
#
Base.complex
— Method
complex(r, [i])
Converts real numbers or arrays to complex numbers. The i
is zero by default.
Examples
julia> complex(7)
7 + 0im
julia> complex([1, 2, 3])
3-element Vector{Complex{Int64}}:
1 + 0im
2 + 0im
3 + 0im
#
Base.bswap
— Function
bswap(n)
Reverts the byte order to n
.
(See also the function description ntoh
and `hton', which perform the conversion between the current native and reverse byte 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)
With iterated itr
ASCII codes for a sequence of hexadecimal digits, returns Vector{UInt8}
bytes corresponding to the binary representation: each subsequent pair of hexadecimal digits in itr
gives a value of one byte in the returned vector.
The length of the itr
must be exact, and the returned array is half the length of the itr'. See also `hex2bytes!
, which describes the embedded version, and `bytes2hex', which describes the inversion.
Compatibility: Julia 1.7
Calling |
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
0xef
#
Base.hex2bytes!
— Function
hex2bytes!(dest::AbstractVector{UInt8}, itr)
Converts an iterable itr
object, which contains bytes representing a hexadecimal string, to a binary representation (similarly hex2bytes'
except that the output is written to `dest in place). The length of dest
must be half the length of `itr'.
Compatibility: Julia 1.7
To call hex2bytes! with iterators producing UInt8, version 1.7 is required. In earlier versions, you can instead assemble an iterable object before calling. |
#
Base.bytes2hex
— Function
bytes2hex(itr) -> String
bytes2hex(io::IO, itr)
Converts the byte iterator itr
to its hexadecimal string representation by returning a String
using bytes2hex(itr)
or writing a string to the io
stream using bytes2hex(io, itr)
. Hexadecimal characters are given in lowercase.
Compatibility: Julia 1.7
Calling |
Examples
julia> a = string(12345, base = 16)
"3039"
julia> b = hex2bytes(a)
2-element Vector{UInt8}:
0x30
0x39
julia> bytes2hex(b)
"3039"
Common numeric functions and constants
#
Base.one
— Function
one(x)
one(T::type)
Returns a single multiplication element for x
: a value such that one(x)*x ==x*one(x) ==x
. one(T)
can also take the type T
; in this case, one
returns a single multiplication element 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, for types representing dimensional quantities (for example, a period in days), the situation may differ, since the unit element of the multiplication must be dimensionless. In this case, the call `one(x)
should return the value of a single element with the same precision (and the same shape for matrices) as `x'.
If you need a value of the same type as x
or type T
, even if x
is dimensional, use instead oneunit
.
See also the function description identity
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))
1
#
Base.oneunit
— Function
oneunit(x::T)
oneunit(T::Type)
Returns T(one(x))
, where T
is the type of the argument or (if the type is passed) the argument. This is different from one
for dimensional values: one
is dimensionless (the unit element of multiplication), although oneunit
is dimensional (the same type as x
, or belongs to the type T
).
Examples
julia> oneunit(3.7)
1.0
julia> import Dates; oneunit(Dates.Day)
1 day
#
Base.zero
— Function
zero(x)
zero(::Type)
Returns the null element by the addition operation for type x
(x
can also specify the type directly).
Examples
julia> zero(1)
0
julia> zero(big"2.0")
0.0
julia> zero(rand(2,2))
2×2 Matrix{Float64}:
0.0 0.0
0.0 0.0
#
Base.MathConstants.pi
— Constant
π
pi
The constant "pi".
The Unicode character pi' can be entered by typing `\pi
and then pressing the TAB key in REPL Julia, as well as in many other editors.
Examples
julia> pi
π = 3.1415926535897...
julia> 1/2pi
0.15915494309189535
#
Base.MathConstants.ℯ
— Constant
ℯ
e
The constant ℯ.
The Unicode character ℯ
can be entered by typing \euler
and then pressing the TAB key in REPL Julia, as well as in many other editors.
Examples
julia> ℯ
ℯ = 2.7182818284590...
julia> log(ℯ)
1
julia> ℯ^(im)π ≈ -1
true
#
Base.MathConstants.catalan
— Constant
catalan
The Catalan 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.9159466120554123
#
Base.MathConstants.eulergamma
— Constant
γ
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) * dx
0.5772078382499133
#
Base.MathConstants.golden
— Constant
φ
golden
The golden ratio.
Examples
julia> Base.MathConstants.golden
φ = 1.6180339887498...
julia> (2ans - 1)^2 ≈ 5
true
#
Base.NaN
— Constant
NaN, NaN64
The value is "not a number" of the type Float64
.
Examples
julia> 0/0
NaN
julia> Inf - Inf
NaN
julia> NaN == NaN, isequal(NaN, NaN), isnan(NaN)
(false, true, true)
julia> reinterpret(UInt32, NaN32)
0x7fc00000
julia> NaN32p1 = reinterpret(Float32, 0x7fc00001)
NaN32
julia> NaN32p1 === NaN32, isequal(NaN32p1, NaN32), isnan(NaN32p1)
(false, true, true)
#
Base.NaN64
— Constant
NaN, NaN64
The value is "not a number" of the type Float64
.
Examples
julia> 0/0
NaN
julia> Inf - Inf
NaN
julia> NaN == NaN, isequal(NaN, NaN), isnan(NaN)
(false, true, true)
julia> reinterpret(UInt32, NaN32)
0x7fc00000
julia> NaN32p1 = reinterpret(Float32, 0x7fc00001)
NaN32
julia> NaN32p1 === NaN32, isequal(NaN32p1, NaN32), isnan(NaN32p1)
(false, true, true)
#
Base.issubnormal
— Function
issubnormal(f) -> Bool
Checks whether a floating-point number is subnormal.
The IEEE floating point number is https://en.wikipedia.org/wiki/Subnormal_number [subnormal] when the bits of the exponent are zero and its significant number is not zero.
Examples
julia> floatmin(Float32)
1.1754944f-38
julia> issubnormal(1.0f-37)
false
julia> issubnormal(1.0f-38)
true
#
Base.isfinite
— Function
isfinite(f) -> Bool
Checks whether the number is finite.
Examples
julia> isfinite(5)
true
julia> isfinite(NaN32)
false
#
Base.isone
— Function
isone(x)
Returns true
if x == one(x)if `x
is an array, a check is performed to see if x
is an identity matrix.
Examples
julia> isone(1.0)
true
julia> isone([1 0; 0 2])
false
julia> isone([1 0; 0 true])
true
#
Base.nextfloat
— Function
nextfloat(x::AbstractFloat, n::Integer)
The result of n
iterative applications of nextfloat
to x
if n >= 0
, or -n
applications prevfloat
if n < 0
.
nextfloat(x::AbstractFloat)
Returns the smallest floating-point number y
of the same type as x
, for example x < y
. If no such y
exists (for example, if x
is Inf
or NaN
), x
is returned.
See also the description 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 nextfloat
if n < 0
.
prevfloat(x::AbstractFloat)
Returns the largest floating-point number y
of the same type as x
, for example y < x
. If no such y
exists (for example, if x
is -Inf
or NaN
), x
is returned.
#
Base.isinteger
— Function
isinteger(x) -> Bool
Checks whether x
is numerically equal to an integer.
Examples
julia> isinteger(4.0)
true
#
Base.isreal
— Function
isreal(x) -> Bool
Checks whether x
or all of its elements are numerically equal to some real numbers, including infinities and "non-number" types. isreal(x)
is true if isequal(x, real(x))
is equal to true.
Examples
julia> isreal(5.)
true
julia> isreal(1 - 3im)
false
julia> isreal(Inf + 0im)
true
julia> isreal([4.; complex(0,1)])
false
#
Core.Float32
— Method
Float32(x [, mode::RoundingMode])
Creates a Float32
based on x'. If 'x
is not exactly representable, then mode
defines the way 'x` is rounded.
Examples
julia> Float32(1/3, RoundDown)
0.3333333f0
julia> Float32(1/3, RoundUp)
0.33333334f0
For the available rounding modes, see the description RoundingMode
.
#
Core.Float64
— Method
Float64(x [, mode::RoundingMode])
Creates a Float64' based on `x'. If 'x
is not exactly representable, then mode
defines the way 'x` is rounded.
Examples
julia> Float64(pi, RoundDown)
3.141592653589793
julia> Float64(pi, RoundUp)
3.1415926535897936
For the available rounding modes, see the description RoundingMode
.
#
Base.Rounding.setrounding
— Method
setrounding(T, mode)
Specifies the rounding mode of the floating-point type T
, which controls the rounding of basic arithmetic functions (+
, -
, *
, /
and sqrt
) and type conversion. Other numeric functions may give incorrect or invalid values if rounding modes other than the default mode are used. RoundNearest
.
Please note that this function is supported only for `T == BigFloat'.
This function is not thread-safe. It affects the code executed in all threads, but its behavior is undefined if it is called simultaneously with calculations using the parameter. |
#
Base.Rounding.setrounding
— Method
setrounding(f::Function, T, mode)
Changes the rounding mode of the floating-point type T
for the duration of the function f
. Logically equivalent to the following:
old = rounding(T) setrounding(T, mode) f() setrounding(T, old)
For the available rounding modes, see the description RoundingMode
.
#
Base.Rounding.get_zero_subnormals
— Function
get_zero_subnormals() -> Bool
Returns false' if operations with subnormal ("denormalized") floating-point values follow IEEE arithmetic rules, or `true
if they can be converted to zeros.
This function only affects the current flow. |
#
Base.Rounding.set_zero_subnormals
— Function
set_zero_subnormals(yes::Bool) -> Bool
If yes
is equal to false', sequential floating-point operations follow the rules of IEEE arithmetic regarding subnormal ("denormalized") values. Otherwise, floating-point operations are allowed (but not required) to convert subnormal input or output values to zeros. Returns `true
if the condition `yes==true' is not met, but the hardware does not support zeroing of subnormal numbers.
'set_zero_subnormals(true)` can speed up calculations on some hardware. However, this function may violate identities such as (x-y==0) ==(x==y)
.
This function only affects the current flow. |
Integers
#
Base.count_ones
— Function
count_ones(x::Integer) -> Integer
The number of units in the binary representation of `x'.
Examples
julia> count_ones(7)
3
julia> count_ones(Int32(-1))
32
#
Base.count_zeros
— Function
count_zeros(x::Integer) -> Integer
The number of zeros in the binary representation of `x'.
Examples
julia> count_zeros(Int32(2 ^ 16 - 1))
16
julia> count_zeros(-1)
0
#
Base.leading_zeros
— Function
leading_zeros(x::Integer) -> Integer
The number of leading zeros in the binary representation of `x'.
Examples
julia> leading_zeros(Int32(1))
31
#
Base.leading_ones
— Function
leading_ones(x::Integer) -> Integer
The number of initial units in the binary representation of `x'.
Examples
julia> leading_ones(UInt32(2 ^ 32 - 2))
31
#
Base.trailing_zeros
— Function
trailing_zeros(x::Integer) -> Integer
The number of trailing zeros in the binary representation of `x'.
Examples
julia> trailing_zeros(2)
1
#
Base.trailing_ones
— Function
trailing_ones(x::Integer) -> Integer
The number of finite units in the binary representation of `x'.
Examples
julia> trailing_ones(3)
2
#
Base.isodd
— Function
isodd(x::Number) -> Bool
Returns true' if `x
is an odd integer (i.e. an integer that is not divisible by 2), or false
otherwise.
Compatibility: Julia 1.7
Arguments other than |
Examples
julia> isodd(9)
true
julia> isodd(10)
false
#
Base.iseven
— Function
iseven(x::Number) -> Bool
Returns true
if x
is an even integer (i.e. an integer that is divisible by 2), or false
otherwise.
Compatibility: Julia 1.7
Arguments other than |
Examples
julia> iseven(9)
false
julia> iseven(10)
true
#
Core.@uint128_str
— Macro
@uint128_str str
Analyzes str
as UInt128
. Returns 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" [...]
Numeric types of BigFloats and BigInts
Types BigFloat
and 'BigInt` implements arbitrary precision floating-point arithmetic and integer arithmetic, respectively. For BigFloat
is in use https://www.mpfr.org /[GNU MPFR library], and for BigInt
— https://gmplib.org [GNU Multiple Precision Arithmetic Library (GMP)].
#
Base.MPFR.BigFloat
— Method
BigFloat(x::Union{Real, AbstractString} [, rounding::RoundingMode=rounding(BigFloat)]; [precision::Integer=precision(BigFloat)])
Creates an arbitrary precision floating-point number based on x
with precision
precision. The rounding
argument indicates the rounding direction of the result if the conversion cannot be performed accurately. If it is not set, the current global values apply.
BigFloat(x::Real)
— is the same as convert(BigFloat,x)
, except when x
is already a BigFloat'. In this case, a value with an accuracy equal to the current global precision is returned; `convert
always returns `x'.
BigFloat(x::AbstractString)
is identical parse
. This function is provided for convenience, as decimal literals are converted to Float64
during analysis, so BigFloat(2.1)
may not produce the expected result.
See also the description
-
rounding
andsetrounding
Compatibility: Julia 1.1
The named argument |
Examples
julia> BigFloat(2.1) # 2.1 здесь является Float64
2.100000000000000088817841970012523233890533447265625
julia> BigFloat("2.1") # самый близкий BigFloat для 2.1
2.099999999999999999999999999999999999999999999999999999999999999999999999999986
julia> BigFloat("2.1", RoundUp)
2.100000000000000000000000000000000000000000000000000000000000000000000000000021
julia> BigFloat("2.1", RoundUp, precision=128)
2.100000000000000000000000000000000000007
#
Base.precision
— Function
precision(num::AbstractFloat; base::Integer=2)
precision(T::Type; base::Integer=2)
Gets the precision for a floating-point number, determined by the number of bits in the significant part of the number, or the precision of the floating-point type T
(the current default value for it if T
is a variable-precision type, for example BigFloat
).
If base
is set, the maximum corresponding number of digits in the significant part of the number for this base is returned.
Compatibility: Julia 1.8
The named |
#
Base.MPFR.setprecision
— Function
setprecision([T=BigFloat,] precision::Int; base=2)
Specifies the precision (in bits, by default) used for arithmetic operations with T
. If base
is set, the accuracy will be the minimum required to output at least precision
digits for this base
.
This function is not thread-safe. It affects the code executed in all threads, but its behavior is undefined if it is called simultaneously with calculations using the parameter. |
Compatibility: Julia 1.8
The named |
setprecision(f::Function, [T=BigFloat,] precision::Integer; base=2)
Modifies the arithmetic precision of 'T' (in this case base
) for the duration of the function f
. Logically equivalent to the following:
old = precision(BigFloat) setprecision(BigFloat, precision) f() setprecision(BigFloat, old)
It is often used as setprecision(T, precision) do ... end
Note. nextfloat()', `prevfloat()
do not use the precision specified in `setprecision'.
Compatibility: Julia 1.8
The named |
#
Base.GMP.BigInt
— Method
BigInt(x)
Creates an integer with arbitrary precision. 'x` can be Int
(or anything that can be converted to Int
). The usual mathematical operators are defined for this type, and the results progress to BigInt
.
Instances can be created based on strings by parse
or using the string literal big
.
Examples
julia> parse(BigInt, "42")
42
julia> big"313"
313
julia> BigInt(10)^19
10000000000000000000
#
Core.@big_str
— Macro
@big_str str
Analyzes the string and converts it to BigInt
or 'BigFloat`; if the string is not a valid integer, it issues an ArgumentError'. The separator `_
is allowed for integers in a string.
Examples
julia> big"123_456"
123456
julia> big"7891.5"
7891.5
julia> big"_"
ERROR: ArgumentError: invalid number format _ for BigInt or BigFloat
[...]
When using |