x-lang

← Index

x/core/math

Integer arithmetic utilities and number predicates, homed on the Num class.

Class Num

(Num int? x)

Test whether x is a machine INT – the base integer type. Floats, rationals, bigints, booleans, and nil are not (N5: counts and indexes are machine INTs).

Parameters:

Returns: BOOL — #t only for machine integers

Examples:

(list (Num int? 3) (Num int? ())) => (#t #f)

(Num inc n)

Add one to a number.

Parameters:

Returns: NUMBER — n + 1

(Num dec n)

Subtract one from a number.

Parameters:

Returns: NUMBER — n - 1

(Num negate n)

Return the negation of a number.

Parameters:

Returns: NUMBER — The additive inverse of n

(Num abs n)

Return the absolute value of a number.

Parameters:

Returns: NUMBER — Absolute value of n

(Num min a . more)

Return the smallest of one or more numbers.

Parameters:

Returns: NUMBER — The smallest argument

Examples:

(Num min 3 1 2) => 1

(Num max a . more)

Return the largest of one or more numbers.

Parameters:

Returns: NUMBER — The largest argument

Examples:

(Num max 3 1 2) => 3

(Num quotient a b)

Integer division truncating toward zero – always an integer, where / on integers may promote to a rational under the tower.

Parameters:

Returns: INT — trunc(a/b)

Examples:

(Num quotient -7 2) => -3

(Num remainder a b)

Truncating remainder (the dividend’s sign) – the pair of quotient; identical to % on integers.

Parameters:

Returns: INT — a - b*trunc(a/b)

Examples:

(Num remainder -7 2) => -1

(Num modulo a b)

Floored modulo: the result takes the DIVISOR’s sign – (Num modulo -7 3) is 2 where % gives -1.

Parameters:

Returns: INT — a - b*floor(a/b)

Examples:

(Num modulo -7 3) => 2

(Num divmod a b)

Truncating quotient and remainder together.

Parameters:

Returns: LIST — (quotient remainder)

Examples:

(Num divmod 7 2) => (3 1)

(Num isqrt n)

Integer square root: the largest k with k*k <= n (Newton’s method); errors on a negative input.

Parameters:

Returns: INT — floor(sqrt(n))

Examples:

(Num isqrt 99) => 9

(Num clamp lo hi n)

Clamp a number to the range [lo, hi].

Parameters:

Returns: NUMBER — n clamped to [lo, hi]

Examples:

(Num clamp 0 10 15) => 10

(Num min-by f a b)

Return the value with the smaller result under f.

Parameters:

Returns: ANY — The value whose projection is smaller

(Num max-by f a b)

Return the value with the larger result under f.

Parameters:

Returns: ANY — The value whose projection is larger

(Num zero? n)

Test whether a number is zero.

Parameters:

Returns: BOOL — True if n is zero

(Num positive? n)

Test whether a number is positive.

Parameters:

Returns: BOOL — True if n is positive

(Num negative? n)

Test whether a number is negative.

Parameters:

Returns: BOOL — True if n is negative

(Num even? n)

Test whether an integer is even.

Parameters:

Returns: BOOL — True if n is even

(Num odd? n)

Test whether an integer is odd.

Parameters:

Returns: BOOL — True if n is odd

(Num gcd . args)

Compute the greatest common divisor. Variadic: (Num gcd a b c …) folds pairwise.

Returns: NUMBER — Greatest common divisor of all arguments

(Num lcm . args)

Compute the least common multiple. Variadic: (Num lcm a b c …) folds pairwise.

Returns: NUMBER — Least common multiple of all arguments

(Num expt base exp)

Compute base raised to a non-negative integer exponent by repeated squaring; errors on a negative exponent.

Parameters:

Returns: NUMBER — base raised to the power exp

Examples:

(Num expt 2 10) => 1024