x-lang

← Index

x/num/rational

Exact rational number arithmetic, homed on the Rational class.

Literal syntax: 1/3, -2/7. The generic operators dispatch rational

operands through the type ops; / promotes inexact int division.

rational

make-rational

rat-denom

rat-first-denom

rat-numer

rat-sign

Predicates

Arithmetic

rat-add

rat-sub

rat-mul

rat-div

Comparison

rat-lt

rat-mod

rat-eq

Operator Overrides

rat?

ensure-rat

rational-type

/

Divide numbers; integer division produces rationals when not exact.

Parameters:

Returns: NUMBER — Quotient

Examples:

(/ 6 3) => 2
(/ 1 3) => 1/3
(/ 3) => 1/3

Class Rational

(Rational type)

The rational type handle, for Convert and Type.

Returns: ATOM — The RATIONAL type handle

(Rational make n d)

Make the rational n/d in lowest terms; a whole result is the integer it equals.

Parameters:

Returns: RATIONAL|INTEGER — n/d reduced

(Rational rational? x)

Test whether a value is a rational number or integer.

Parameters:

Returns: BOOL — True if x is rational or integer

(Rational exact? x)

Test whether a value is an exact number.

Parameters:

Returns: BOOL — True if x is exact (rational or integer)

(Rational numerator x)

Return the numerator of a rational number.

Parameters:

Returns: INTEGER — Numerator of the rational, or the integer itself

(Rational denominator x)

Return the denominator of a rational number.

Parameters:

Returns: INTEGER — Denominator of the rational, or 1 for integers

(Rational + a b)

Add two rationals (ints promote).

Parameters:

Returns: RATIONAL|INTEGER — Sum, reduced to lowest terms

(Rational - a b)

Subtract two rationals (ints promote).

Parameters:

Returns: RATIONAL|INTEGER — Difference, reduced to lowest terms

(Rational * a b)

Multiply two rationals (ints promote).

Parameters:

Returns: RATIONAL|INTEGER — Product, reduced to lowest terms

(Rational / a b)

Divide two rationals (ints promote).

Parameters:

Returns: RATIONAL|INTEGER — Quotient, reduced to lowest terms

(Rational < a b)

Test whether a is less than b (ints promote).

Parameters:

Returns: BOOL — True if a < b

(Rational = a b)

Test whether a equals b (ints promote).

Parameters:

Returns: BOOL — True if a equals b

number?

Test whether a value is any numeric type (now including rationals).

Parameters:

Returns: BOOL — True if x is a number

Examples:

(number? 1/2) => #t