Skip to content
ry

Functions over single numbers, integer or double, and eight constants (Num.pi, Num.tau, Num.e, Num.infinity and the integer and double limits). A checked operation returns the failure as a tag where R would return NA, NaN or Inf with a warning; its unchecked twin (Num.add beside Num.add_checked) stops with an error in that case. A NaN result, such as Inf - Inf or Num.sin(Inf), is :Err(:Undefined) from a checked operation and an error from any other. The bit operations take integers as 32-bit two’s complement.

Function Summary
Num.to_str The number as text, with up to 15 significant digits.
Num.compare :LT, :EQ or :GT, comparing first with second.
Num.min The smaller number.
Num.max The larger number.
Num.is_lt Whether first < second.
Num.is_gt Whether first > second.
Num.is_lte Whether first <= second.
Num.is_gte Whether first >= second.
Num.is_approx_eq Whether two numbers are equal within a relative tolerance rtol (default 1e-5) or an absolute tolerance atol (default 1e-8).
Num.is_zero Whether the number is zero.
Num.is_positive Whether the number is greater than zero.
Num.is_negative Whether the number is less than zero.
Num.is_even Whether the number is a whole, even number.
Num.is_odd Whether the number is a whole, odd number.
Num.is_multiple_of Whether first is a whole multiple of second.
Num.is_finite Whether the number is neither infinite nor NaN.
Num.is_infinite Whether the number is Inf or -Inf.
Num.abs The absolute value.
Num.abs_diff The absolute difference of two numbers.
Num.neg The number with its sign flipped.
Num.ceiling Rounds up to a whole number, keeping the number type.
Num.floor Rounds down to a whole number, keeping the number type.
Num.round Rounds to the nearest whole number, halves away from zero, keeping the number type.
Num.pow base raised to exponent.
Num.sin The sine of an angle in radians.
Num.cos The cosine of an angle in radians.
Num.tan The tangent of an angle in radians.
Num.atan The arctangent, in radians.
Num.add_checked The sum, or :Err(:Overflow); a NaN sum such as Inf + -Inf is :Err(:Undefined).
Num.sub_checked The difference, or :Err(:Overflow).
Num.mul_checked The product, or :Err(:Overflow); a finite double that would become infinite overflows too.
Num.add_saturated Adds two numbers, clamping an integer result to the largest or smallest integer.
Num.sub_saturated Subtracts second from first, clamping an integer result.
Num.mul_saturated Multiplies two numbers, clamping an integer result.
Num.div_checked The quotient as a double, or :Err(:DivByZero).
Num.div_trunc_checked The quotient rounded toward zero, or :Err(:DivByZero); Inf divided by Inf is :Err(:Undefined).
Num.div_ceil_checked Divides and rounds up, or :Err(:DivByZero).
Num.rem_checked The remainder of the truncated division, with the sign of first, or :Err(:DivByZero).
Num.sqrt_checked The square root, or :Err(:SqrtOfNegative).
Num.log_checked The natural logarithm, or :Err(:LogNeedsPositive).
Num.to_double The number as a double.
Num.to_integer_checked The number truncated toward zero as an integer, or :Err(:OutOfBounds) when it does not fit.
Num.to_integer The number truncated toward zero as an integer.
Num.add The sum.
Num.sub second subtracted from first.
Num.mul The product.
Num.div The quotient as a double.
Num.div_trunc The quotient rounded toward zero.
Num.div_ceil The quotient rounded up.
Num.rem The remainder of the truncated division, with the sign of first.
Num.pow_int base raised to a non-negative exponent, as an integer.
Num.sqrt The square root.
Num.log The natural logarithm.
Num.asin The arcsine in radians.
Num.acos The arccosine in radians.
Num.is_nan Whether the number is NaN.
Num.bitwise_and The bits set in both integers, in 32-bit two’s complement.
Num.bitwise_or The bits set in either integer.
Num.bitwise_xor The bits set in exactly one of the integers.
Num.bitwise_not The integer with every bit flipped, which is -number - 1.
Num.shift_left_by Shifts the bits left by bits, from 0 to 31.
Num.shift_right_by Shifts the bits right by bits, copying the sign bit in, so a negative number stays negative.
Num.shift_right_zf_by Shifts the bits right by bits, filling with zeros.
Num.count_one_bits The number of set bits.
Num.count_leading_zero_bits The number of zero bits above the highest set bit, 32 for zero.
Num.count_trailing_zero_bits The number of zero bits below the lowest set bit, 32 for zero.
Num.pi The ratio of a circle’s circumference to its diameter.
Num.tau The ratio of a circle’s circumference to its radius, 2 * Num.pi.
Num.e Euler’s number, the base of the natural logarithm.
Num.max_integer The largest integer, 2147483647L.
Num.min_integer The smallest integer, -2147483647L.
Num.max_double The largest finite double.
Num.min_double The most negative finite double, the negation of Num.max_double.
Num.infinity Positive infinity, R’s Inf.
Num.to_str <- function<T: scalar numeric>(number: T) -> character

The number as text, with up to 15 significant digits.

Num.to_str(1 / 3)
#> "0.333333333333333"

Source

Num.compare <- function<T: scalar numeric>(first: T, second: T) -> :LT | :EQ | :GT

:LT, :EQ or :GT, comparing first with second.

Num.compare(2L, 1L)
#> :GT

Source

Num.min <- function<T: scalar numeric>(first: T, second: T) -> T

The smaller number.

Num.min(2L, 1L)
#> 1L

Source

Num.max <- function<T: scalar numeric>(first: T, second: T) -> T

The larger number.

Num.max(2.5, 1)
#> 2.5

Source

Num.is_lt <- function<T: scalar numeric>(first: T, second: T) -> logical

Whether first < second.

Num.is_lt(1L, 2L)
#> TRUE

Source

Num.is_gt <- function<T: scalar numeric>(first: T, second: T) -> logical

Whether first > second.

Num.is_gt(1L, 2L)
#> FALSE

Source

Num.is_lte <- function<T: scalar numeric>(first: T, second: T) -> logical

Whether first <= second.

Num.is_lte(2, 2L)
#> TRUE

Source

Num.is_gte <- function<T: scalar numeric>(first: T, second: T) -> logical

Whether first >= second.

Num.is_gte(1.5, 2)
#> FALSE

Source

#: <T: scalar numeric> fn(first: T, second: T, [rtol]: double, [atol]: double) -> logical
Num.is_approx_eq <- function(first, second, rtol, atol)

Whether two numbers are equal within a relative tolerance rtol (default 1e-5) or an absolute tolerance atol (default 1e-8).

Num.is_approx_eq(0.1 + 0.2, 0.3)
#> TRUE

Source

Num.is_zero <- function<T: scalar numeric>(number: T) -> logical

Whether the number is zero.

Num.is_zero(-0)
#> TRUE

Source

Num.is_positive <- function<T: scalar numeric>(number: T) -> logical

Whether the number is greater than zero.

Num.is_positive(0L)
#> FALSE

Source

Num.is_negative <- function<T: scalar numeric>(number: T) -> logical

Whether the number is less than zero.

Num.is_negative(-1)
#> TRUE

Source

Num.is_even <- function<T: scalar numeric>(number: T) -> logical

Whether the number is a whole, even number.

Num.is_even(4L)
#> TRUE

Source

Num.is_odd <- function<T: scalar numeric>(number: T) -> logical

Whether the number is a whole, odd number.

Num.is_odd(-3L)
#> TRUE

Source

Num.is_multiple_of <- function<T: scalar numeric>(first: T, second: T) -> logical

Whether first is a whole multiple of second. Only zero is a multiple of zero.

Num.is_multiple_of(12L, 4L)
#> TRUE

Source

Num.is_finite <- function<T: scalar numeric>(number: T) -> logical

Whether the number is neither infinite nor NaN.

Num.is_finite(1 / 0)
#> FALSE

Source

Num.is_infinite <- function<T: scalar numeric>(number: T) -> logical

Whether the number is Inf or -Inf.

Num.is_infinite(-1 / 0)
#> TRUE

Source

Num.abs <- function<T: scalar numeric>(number: T) -> T

The absolute value.

Num.abs(-3L)
#> 3L

Source

Num.abs_diff <- function<T: scalar numeric>(first: T, second: T) -> T

The absolute difference of two numbers. An integer result that overflows is an error.

Num.abs_diff(3L, 10L)
#> 7L

Source

Num.neg <- function<T: scalar numeric>(number: T) -> T

The number with its sign flipped.

Num.neg(2.5)
#> -2.5

Source

Num.ceiling <- function<T: scalar numeric>(number: T) -> T

Rounds up to a whole number, keeping the number type.

Num.ceiling(1.2)
#> 2

Source

Num.floor <- function<T: scalar numeric>(number: T) -> T

Rounds down to a whole number, keeping the number type.

Num.floor(-1.2)
#> -2

Source

Num.round <- function<T: scalar numeric>(number: T) -> T

Rounds to the nearest whole number, halves away from zero, keeping the number type.

Num.round(2.5)
#> 3

Source

Num.pow <- function<T: scalar numeric>(base: T, exponent: T) -> double

base raised to exponent. A result that is not a real number, as for Num.pow(-1, 0.5), is an error.

Num.pow(2L, 10L)
#> 1024

Source

Num.sin <- function<T: scalar numeric>(number: T) -> double

The sine of an angle in radians. An infinite angle is an error, because its sine is undefined; the same holds for Num.cos and Num.tan.

Num.sin(0)
#> 0

Source

Num.cos <- function<T: scalar numeric>(number: T) -> double

The cosine of an angle in radians.

Num.cos(0)
#> 1

Source

Num.tan <- function<T: scalar numeric>(number: T) -> double

The tangent of an angle in radians.

Num.tan(0)
#> 0

Source

Num.atan <- function<T: scalar numeric>(number: T) -> double

The arctangent, in radians.

Num.atan(1) * 4
#> 3.14159265358979

Source

Num.add_checked <- function<T: scalar numeric>(first: T, second: T) -> Result<T, :Overflow | :Undefined>

The sum, or :Err(:Overflow); a NaN sum such as Inf + -Inf is :Err(:Undefined).

Num.add_checked(.Machine$integer.max, 1L)
#> :Err(:Overflow)

Source

Num.sub_checked <- function<T: scalar numeric>(first: T, second: T) -> Result<T, :Overflow | :Undefined>

The difference, or :Err(:Overflow).

Num.sub_checked(10L, 3L)
#> :Ok(7L)

Source

Num.mul_checked <- function<T: scalar numeric>(first: T, second: T) -> Result<T, :Overflow | :Undefined>

The product, or :Err(:Overflow); a finite double that would become infinite overflows too.

Num.mul_checked(1e308, 10)
#> :Err(:Overflow)

Source

Num.add_saturated <- function<T: scalar numeric>(first: T, second: T) -> T

Adds two numbers, clamping an integer result to the largest or smallest integer.

Num.add_saturated(2147483647L, 1L)
#> 2147483647L

Source

Num.sub_saturated <- function<T: scalar numeric>(first: T, second: T) -> T

Subtracts second from first, clamping an integer result.

Num.sub_saturated(-2147483647L, 1L)
#> -2147483647L

Source

Num.mul_saturated <- function<T: scalar numeric>(first: T, second: T) -> T

Multiplies two numbers, clamping an integer result.

Num.mul_saturated(100000L, 100000L)
#> 2147483647L

Source

Num.div_checked <- function<T: scalar numeric>(first: T, second: T) -> Result<double, :DivByZero | :Undefined>

The quotient as a double, or :Err(:DivByZero).

Num.div_checked(7L, 2L)
#> :Ok(3.5)

Source

Num.div_trunc_checked <- function<T: scalar numeric>(first: T, second: T) -> Result<T, :DivByZero | :Undefined>

The quotient rounded toward zero, or :Err(:DivByZero); Inf divided by Inf is :Err(:Undefined).

Num.div_trunc_checked(-7L, 2L)
#> :Ok(-3L)
Num.div_trunc_checked(Inf, Inf)
#> :Err(:Undefined)

Source

Num.div_ceil_checked <- function<T: scalar numeric>(first: T, second: T) -> Result<T, :DivByZero | :Undefined>

Divides and rounds up, or :Err(:DivByZero).

Num.div_ceil_checked(7L, 2L)
#> :Ok(4L)

Source

Num.rem_checked <- function<T: scalar numeric>(first: T, second: T) -> Result<T, :DivByZero | :Undefined>

The remainder of the truncated division, with the sign of first, or :Err(:DivByZero).

Num.rem_checked(-7L, 2L)
#> :Ok(-1L)

Source

Num.sqrt_checked <- function<T: scalar numeric>(number: T) -> Result<double, :SqrtOfNegative>

The square root, or :Err(:SqrtOfNegative).

Num.sqrt_checked(-1)
#> :Err(:SqrtOfNegative)

Source

Num.log_checked <- function<T: scalar numeric>(number: T) -> Result<double, :LogNeedsPositive>

The natural logarithm, or :Err(:LogNeedsPositive).

Num.log_checked(exp(2))
#> :Ok(2)

Source

Num.to_double <- function<T: scalar numeric>(number: T) -> double

The number as a double.

Num.to_double(3L)
#> 3

Source

Num.to_integer_checked <- function<T: scalar numeric>(number: T) -> Result<integer, :OutOfBounds>

The number truncated toward zero as an integer, or :Err(:OutOfBounds) when it does not fit.

Num.to_integer_checked(-3.7)
#> :Ok(-3L)

Source

Num.to_integer <- function<T: scalar numeric>(number: T) -> integer

The number truncated toward zero as an integer. A number that does not fit an integer is an error; Num.to_integer_checked returns :Err(:OutOfBounds) instead.

Num.to_integer(-3.7)
#> -3L

Source

Num.add <- function<T: scalar numeric>(first: T, second: T) -> T

The sum. An integer sum that overflows stops with an error, so use Num.add_checked when overflow is possible. Unlike +, it can be passed by name.

List.walk(list(1L, 2L, 3L), 0L, Num.add)
#> 6L

Source

Num.sub <- function<T: scalar numeric>(first: T, second: T) -> T

second subtracted from first. An integer result that overflows is an error, and so is a NaN result such as Inf - Inf.

Num.sub(10L, 4L)
#> 6L

Source

Num.mul <- function<T: scalar numeric>(first: T, second: T) -> T

The product. An integer product that overflows is an error.

Num.mul(6L, 7L)
#> 42L

Source

Num.div <- function<T: scalar numeric>(first: T, second: T) -> double

The quotient as a double. A zero divisor is an error; Num.div_checked returns :Err(:DivByZero) instead.

Num.div(7L, 2L)
#> 3.5

Source

Num.div_trunc <- function<T: scalar numeric>(first: T, second: T) -> T

The quotient rounded toward zero. A zero divisor is an error.

Num.div_trunc(-7L, 2L)
#> -3L

Source

Num.div_ceil <- function<T: scalar numeric>(first: T, second: T) -> T

The quotient rounded up. A zero divisor is an error.

Num.div_ceil(7L, 2L)
#> 4L

Source

Num.rem <- function<T: scalar numeric>(first: T, second: T) -> T

The remainder of the truncated division, with the sign of first. A zero divisor is an error.

Num.rem(-7L, 2L)
#> -1L

Source

Num.pow_int <- function(base: integer, exponent: integer) -> integer

base raised to a non-negative exponent, as an integer. A result that overflows is an error; Num.pow returns a double instead.

Num.pow_int(3L, 4L)
#> 81L

Source

Num.sqrt <- function<T: scalar numeric>(number: T) -> double

The square root. A negative number is an error; Num.sqrt_checked returns :Err(:SqrtOfNegative) instead.

Num.sqrt(16L)
#> 4

Source

Num.log <- function<T: scalar numeric>(number: T) -> double

The natural logarithm. A number that is not positive is an error; Num.log_checked returns :Err(:LogNeedsPositive) instead.

Num.log(Num.e)
#> 1

Source

Num.asin <- function<T: scalar numeric>(number: T) -> double

The arcsine in radians. A number outside -1 to 1 is an error.

Num.asin(1)
#> 1.5707963267949

Source

Num.acos <- function<T: scalar numeric>(number: T) -> double

The arccosine in radians. A number outside -1 to 1 is an error.

Num.acos(1)
#> 0

Source

Num.is_nan <- function<T: scalar numeric>(number: T) -> logical

Whether the number is NaN. It is the one library function that accepts NaN; NA is still an error.

Num.is_nan(0 / 0)
#> TRUE

Source

Num.bitwise_and <- function(first: integer, second: integer) -> integer

The bits set in both integers, in 32-bit two’s complement.

Num.bitwise_and(12L, 10L)
#> 8L

Source

Num.bitwise_or <- function(first: integer, second: integer) -> integer

The bits set in either integer.

Num.bitwise_or(12L, 10L)
#> 14L

Source

Num.bitwise_xor <- function(first: integer, second: integer) -> integer

The bits set in exactly one of the integers. A result whose bit pattern is -2147483648 is an error, because R uses that value for NA; the same holds for every bit operation.

Num.bitwise_xor(12L, 10L)
#> 6L

Source

Num.bitwise_not <- function(number: integer) -> integer

The integer with every bit flipped, which is -number - 1.

Num.bitwise_not(5L)
#> -6L

Source

Num.shift_left_by <- function(number: integer, bits: integer) -> integer

Shifts the bits left by bits, from 0 to 31. Bits shifted past the top are lost, as in Roc.

Num.shift_left_by(1L, 4L)
#> 16L

Source

Num.shift_right_by <- function(number: integer, bits: integer) -> integer

Shifts the bits right by bits, copying the sign bit in, so a negative number stays negative.

Num.shift_right_by(-16L, 2L)
#> -4L

Source

Num.shift_right_zf_by <- function(number: integer, bits: integer) -> integer

Shifts the bits right by bits, filling with zeros.

Num.shift_right_zf_by(-16L, 28L)
#> 15L

Source

Num.count_one_bits <- function(number: integer) -> integer

The number of set bits.

Num.count_one_bits(255L)
#> 8L

Source

Num.count_leading_zero_bits <- function(number: integer) -> integer

The number of zero bits above the highest set bit, 32 for zero.

Num.count_leading_zero_bits(1L)
#> 31L

Source

Num.count_trailing_zero_bits <- function(number: integer) -> integer

The number of zero bits below the lowest set bit, 32 for zero.

Num.count_trailing_zero_bits(8L)
#> 3L

Source

#: double
Num.pi

The ratio of a circle’s circumference to its diameter. It is a value, not a function.

Num.pi
#> 3.14159265358979

Source

#: double
Num.tau

The ratio of a circle’s circumference to its radius, 2 * Num.pi.

Num.tau
#> 6.28318530717959

Source

#: double
Num.e

Euler’s number, the base of the natural logarithm.

Num.e
#> 2.71828182845905

Source

#: integer
Num.max_integer

The largest integer, 2147483647L. It is a value, not a function.

Num.max_integer
#> 2147483647L

Source

#: integer
Num.min_integer

The smallest integer, -2147483647L. R stores NA in the one value below it, so that value is not an integer here.

Num.min_integer
#> -2147483647L

Source

#: double
Num.max_double

The largest finite double.

Num.max_double
#> 1.79769313486232e+308

Source

#: double
Num.min_double

The most negative finite double, the negation of Num.max_double.

Num.min_double
#> -1.79769313486232e+308

Source

#: double
Num.infinity

Positive infinity, R’s Inf.

c(Num.infinity, -Num.infinity)
#> c(Inf, -Inf)

Source