Numeric Procedures

Look up arguments, exactness, and errors for arithmetic, comparison, integer division, rounding, gcd/lcm, powers, and integer square roots.

Use this reference to choose a numeric procedure and check its arguments. The examples show how to collect two results and how negative integer division works.

Get an integer square root and remainder

Run this complete example in the Playground.

LISPEX
(call-with-values (lambda () (exact-integer-sqrt 20)) list)

Result

OUTPUT
(4 4)

The procedure returns two values. The first is the largest integer whose square is at most 20. The second is the remainder, so 20 = 4 × 4 + 4. The call-with-values procedure passes both values to list so you can see them together.

Choose how to divide negative integers

LISPEX
(list (call-with-values (lambda () (floor/ -7 3)) list)
      (call-with-values (lambda () (truncate/ -7 3)) list))

Result

OUTPUT
((-3 2) (-2 -1))

Both procedures return a quotient and a remainder. The floor/ procedure rounds the quotient down, giving -3. The truncate/ procedure rounds toward zero, giving -2. In each case, the dividend equals the divisor times the quotient plus the remainder.

Check arguments and results

In the table, ... means you can supply more arguments of the same kind.

CallArguments and resultErrors
(+ number ...), (* number ...)Zero or more numbers. (+) returns 0; (*) returns 1E312 wrong type; E314 non-finite result
(- number ...), (/ number ...)One or more numbers. (- 5) returns -5; (/ 4) returns 1/4E302 no arguments; E312 wrong type; E313 zero divisor; E314 non-finite result
(= number number ...) and < > <= >=At least two numbers, compared exactly even with mixed exactnessE302 too few arguments; E312 wrong type
(floor/ integer integer), (truncate/ integer integer)Two integers, returning quotient and remainder as two valuesE312 non-integer; E313 zero divisor; E314 inexact result too large
(expt number exact-integer)A base and an exact integer exponent. The result has the base's exactnessE312 wrong type; E313 exact zero to a negative power; E314 result cannot be represented
(exact-integer-sqrt exact-nonnegative-integer)One exact integer at least zero. Returns an exact integer root and remainderE312 input is not an exact nonnegative integer

The +, -, *, and / procedures return exact results when all inputs are exact. Division can return an integer, as in (/ 10 5) → 2, or a fraction. Any inexact input makes their result inexact.

Integer division accepts integer-valued inputs such as 7.0, but rejects 7/2. Its result is inexact when any input is inexact. By contrast, expt requires an exact exponent, so (expt 2 3.0) produces E312.

Other numeric operations

Use floor, ceiling, round, and truncate to round a number. These preserve exactness, and round sends halfway cases to the nearest even integer. The gcd and lcm procedures compute the greatest common divisor and least common multiple.

Lispex uses finite real numbers. A calculation that would produce infinity or NaN reports E314; general transcendental functions such as sin and log are not supported.

Exact and inexact numbers explains mixed arithmetic and printed results. The procedure index lists the other procedure families.