# Numbers¶

## Standard Numeric Types¶

Bool Int8 Uint8 Int16 Uint16 Int32 Uint32 Int64 Uint64 Int128 Uint128 Float16 Float32 Float64 Complex64 Complex128

## Data Formats¶

Convert an integer to a binary string, optionally specifying a number of digits to pad to.

Convert an integer to a hexadecimal string, optionally specifying a number of digits to pad to.

Convert an integer to a decimal string, optionally specifying a number of digits to pad to.

Convert an integer to an octal string, optionally specifying a number of digits to pad to.

Convert an integer to a string in the given base, optionally specifying a number of digits to pad to. The base can be specified as either an integer, or as a Uint8 array of character values to use as digit symbols.

Returns an array of the digits of n in the given base, optionally padded with zeros to a specified size. More significant digits are at higher indexes, such that n == sum([digits[k]*base^(k-1) for k=1:length(digits)]).

bits(n)

A string giving the literal bit representation of a number.

parseint([type, ]str[, base])

Parse a string as an integer in the given base (default 10), yielding a number of the specified type (default Int).

parsefloat([type, ]str)

Parse a string as a decimal floating point number, yielding a number of the specified type.

big(x)

Convert a number to a maximum precision representation (typically BigInt or BigFloat). See BigFloat for information about some pitfalls with floating-point numbers.

bool(x)

Convert a number or numeric array to boolean

int(x)

Convert a number or array to the default integer type on your platform. Alternatively, x can be a string, which is parsed as an integer.

uint(x)

Convert a number or array to the default unsigned integer type on your platform. Alternatively, x can be a string, which is parsed as an unsigned integer.

integer(x)

Convert a number or array to integer type. If x is already of integer type it is unchanged, otherwise it converts it to the default integer type on your platform.

signed(x)

Convert a number to a signed integer

unsigned(x) → Unsigned

Convert a number to an unsigned integer

int8(x)

Convert a number or array to Int8 data type

int16(x)

Convert a number or array to Int16 data type

int32(x)

Convert a number or array to Int32 data type

int64(x)

Convert a number or array to Int64 data type

int128(x)

Convert a number or array to Int128 data type

uint8(x)

Convert a number or array to Uint8 data type

uint16(x)

Convert a number or array to Uint16 data type

uint32(x)

Convert a number or array to Uint32 data type

uint64(x)

Convert a number or array to Uint64 data type

uint128(x)

Convert a number or array to Uint128 data type

float16(x)

Convert a number or array to Float16 data type

float32(x)

Convert a number or array to Float32 data type

float64(x)

Convert a number or array to Float64 data type

float32_isvalid(x, out::Vector{Float32}) → Bool

Convert a number or array to Float32 data type, returning true if successful. The result of the conversion is stored in out[1].

float64_isvalid(x, out::Vector{Float64}) → Bool

Convert a number or array to Float64 data type, returning true if successful. The result of the conversion is stored in out[1].

float(x)

Convert a number, array, or string to a FloatingPoint data type. For numeric data, the smallest suitable FloatingPoint type is used. Converts strings to Float64.

This function is not recommended for arrays. It is better to use a more specific function such as float32 or float64.

significand(x)

Extract the significand(s) (a.k.a. mantissa), in binary representation, of a floating-point number or array.

julia> significand(15.2)/15.2
0.125

julia> significand(15.2)*8
15.2

exponent(x) → Int

Get the exponent of a normalized floating-point number.

complex64(r[, i])

Convert to r + i*im represented as a Complex64 data type. i defaults to zero.

complex128(r[, i])

Convert to r + i*im represented as a Complex128 data type. i defaults to zero.

complex(r[, i])

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

char(x)

Convert a number or array to Char data type

bswap(n)

Byte-swap an integer

num2hex(f)

Get a hexadecimal string of the binary representation of a floating point number

hex2num(str)

Convert a hexadecimal string to the floating point number it represents

hex2bytes(s::ASCIIString)

Convert an arbitrarily long hexadecimal string to its binary representation. Returns an Array{Uint8, 1}, i.e. an array of bytes.

bytes2hex(bin_arr::Array{Uint8, 1})

Convert an array of bytes to its hexadecimal representation. All characters are in lower-case. Returns an ASCIIString.

## Numbers¶

one(x)

Get the multiplicative identity element for the type of x (x can also specify the type itself). For matrices, returns an identity matrix of the appropriate size and type.

zero(x)

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

pi
π

The constant pi

im

The imaginary unit

e

The constant e

catalan

Catalan’s constant

γ

Euler’s constant

φ

The golden ratio

Inf

Positive infinity of type Float64

Inf32

Positive infinity of type Float32

Inf16

Positive infinity of type Float16

NaN

A not-a-number value of type Float64

NaN32

A not-a-number value of type Float32

NaN16

A not-a-number value of type Float16

issubnormal(f) → Bool

Test whether a floating point number is subnormal

isfinite(f) → Bool

Test whether a number is finite

isinf(f) → Bool

Test whether a number is infinite

isnan(f) → Bool

Test whether a floating point number is not a number (NaN)

inf(f)

Returns positive infinity of the floating point type f or of the same floating point type as f

nan(f)

Returns NaN (not-a-number) of the floating point type f or of the same floating point type as f

nextfloat(f)

Get the next floating point number in lexicographic order

prevfloat(f) → FloatingPoint

Get the previous floating point number in lexicographic order

isinteger(x) → Bool

Test whether x or all its elements are numerically equal to some integer

isreal(x) → Bool

Test whether x or all its elements are numerically equal to some real number

BigInt(x)

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

BigFloat(x)

Create an arbitrary precision floating point number. x may be an Integer, a Float64, a String or a BigInt. The usual mathematical operators are defined for this type, and results are promoted to a BigFloat. Note that because floating-point numbers are not exactly-representable in decimal notation, BigFloat(2.1) may not yield what you expect. You may prefer to initialize constants using strings, e.g., BigFloat("2.1").

get_rounding(T)

Get the current floating point rounding mode for type T. Valid modes are RoundNearest, RoundToZero, RoundUp, RoundDown, and RoundFromZero (BigFloat only).

set_rounding(T, mode)

Set the rounding mode of floating point type T. Note that this may affect other types, for instance changing the rounding mode of Float64 will change the rounding mode of Float32. See get_rounding for available modes

with_rounding(f::Function, T, mode)

Change the rounding mode of floating point type T for the duration of f. It is logically equivalent to:

old = get_rounding(T)
set_rounding(T, mode)
f()
set_rounding(T, old)


See get_rounding for available rounding modes.

### Integers¶

count_ones(x::Integer) → Integer

Number of ones in the binary representation of x.

julia> count_ones(7)
3

count_zeros(x::Integer) → Integer

Number of zeros in the binary representation of x.

julia> count_zeros(int32(2 ^ 16 - 1))
16


Number of zeros leading the binary representation of x.

julia> leading_zeros(int32(1))
31


Number of ones leading the binary representation of x.

julia> leading_ones(int32(2 ^ 32 - 2))
31

trailing_zeros(x::Integer) → Integer

Number of zeros trailing the binary representation of x.

julia> trailing_zeros(2)
1

trailing_ones(x::Integer) → Integer

Number of ones trailing the binary representation of x.

julia> trailing_ones(3)
2

isprime(x::Integer) → Bool

Returns true if x is prime, and false otherwise.

julia> isprime(3)
true

primes(n)

Returns a collection of the prime numbers <= n.

isodd(x::Integer) → Bool

Returns true if x is odd (that is, not divisible by 2), and false otherwise.

julia> isodd(9)
true

julia> isodd(10)
false

iseven(x::Integer) → Bool

Returns true is x is even (that is, divisible by 2), and false otherwise.

julia> iseven(10)
true

julia> iseven(9)
false


## BigFloats¶

The BigFloat type implements arbitrary-precision floating-point aritmetic using the GNU MPFR library.

precision(num::FloatingPoint)

Get the precision of a floating point number, as defined by the effective number of bits in the mantissa.

get_bigfloat_precision()

Get the precision (in bits) currently used for BigFloat arithmetic.

set_bigfloat_precision(x::Int64)

Set the precision (in bits) to be used to BigFloat arithmetic.

with_bigfloat_precision(f::Function, precision::Integer)

Change the BigFloat arithmetic precision (in bits) for the duration of f. It is logically equivalent to:

old = get_bigfloat_precision()
set_bigfloat_precision(precision)
f()
set_bigfloat_precision(old)


## Random Numbers¶

Random number generation in Julia uses the Mersenne Twister library. Julia has a global RNG, which is used by default. Multiple RNGs can be plugged in using the AbstractRNG object, which can then be used to have multiple streams of random numbers. Currently, only MersenneTwister is supported.

srand([rng, ]seed)

Seed the RNG with a seed, which may be an unsigned integer or a vector of unsigned integers. seed can even be a filename, in which case the seed is read from a file. If the argument rng is not provided, the default global RNG is seeded.

MersenneTwister([seed])

Create a MersenneTwister RNG object. Different RNG objects can have their own seeds, which may be useful for generating different streams of random numbers.

rand() → Float64

Generate a Float64 random number uniformly in [0,1)

rand!([rng, ]A)

Populate the array A with random number generated from the specified RNG.

rand(rng::AbstractRNG[, dims...])

Generate a random Float64 number or array of the size specified by dims, using the specified RNG object. Currently, MersenneTwister is the only available Random Number Generator (RNG), which may be seeded using srand.

rand(dims or [dims...])

Generate a random Float64 array of the size specified by dims

rand(Int32|Uint32|Int64|Uint64|Int128|Uint128[, dims...])

Generate a random integer of the given type. Optionally, generate an array of random integers of the given type by specifying dims.

rand(r[, dims...])

Generate a random integer in the range r (for example, 1:n or 0:2:10). Optionally, generate a random integer array.

randbool([dims...])

Generate a random boolean value. Optionally, generate an array of random boolean values.

randbool!(A)

Fill an array with random boolean values. A may be an Array or a BitArray.

randn([rng], dims or [dims...])

Generate a normally-distributed random number with mean 0 and standard deviation 1. Optionally generate an array of normally-distributed random numbers.

randn!([rng, ]A::Array{Float64, N})

Fill the array A with normally-distributed (mean 0, standard deviation 1) random numbers. Also see the rand function.