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

Examples:

(Num inc 41) => 42

(Num dec n)

Subtract one from a number.

Parameters:

Returns: NUMBER — n - 1

Examples:

(Num dec 43) => 42

(Num negate n)

Return the negation of a number.

Parameters:

Returns: NUMBER — The additive inverse of n

Examples:

(Num negate 5) => -5

(Num abs n)

Return the absolute value of a number.

Parameters:

Returns: NUMBER — Absolute value of n

Examples:

(Num abs -7) => 7

(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. A zero divisor raises.

Parameters:

Returns: INTEGER — 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. A zero divisor raises.

Parameters:

Returns: INTEGER — 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. A zero divisor raises.

Parameters:

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

Examples:

(Num modulo -7 3) => 2

(Num divmod a b)

Truncating quotient and remainder together. A zero divisor raises.

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: INTEGER — 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

Examples:

(Num min-by (fn (_ n) (* n n)) -3 2) => 2

(Num max-by f a b)

Return the value with the larger result under f.

Parameters:

Returns: ANY — The value whose projection is larger

Examples:

(Num max-by (fn (_ n) (* n n)) -3 2) => -3

(Num zero? n)

Test whether a number is zero.

Parameters:

Returns: BOOL — True if n is zero

Examples:

(list (Num zero? 0) (Num zero? 3)) => (#t #f)

(Num positive? n)

Test whether a number is positive.

Parameters:

Returns: BOOL — True if n is positive

Examples:

(list (Num positive? 3) (Num positive? 0)) => (#t #f)

(Num negative? n)

Test whether a number is negative.

Parameters:

Returns: BOOL — True if n is negative

Examples:

(list (Num negative? -3) (Num negative? 0)) => (#t #f)

(Num even? n)

Test whether an integer is even.

Parameters:

Returns: BOOL — True if n is even

Examples:

(list (Num even? 4) (Num even? 7)) => (#t #f)

(Num odd? n)

Test whether an integer is odd.

Parameters:

Returns: BOOL — True if n is odd

Examples:

(list (Num odd? 7) (Num odd? 4)) => (#t #f)

(Num gcd . args)

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

Returns: NUMBER — Greatest common divisor of all arguments

Examples:

(Num gcd 12 18 30) => 6

(Num lcm . args)

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

Returns: NUMBER — Least common multiple of all arguments

Examples:

(Num lcm 4 6) => 12

(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