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.

Predicates

Arithmetic

Comparison

Operator Overrides

/

Divide numbers; integer division produces rationals when not exact.

Parameters:

Returns: NUMBER — Quotient

Class Rational

(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: INT — Numerator of the rational, or the integer itself

(Rational denominator x)

Return the denominator of a rational number.

Parameters:

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

(Rational + a b)

Add two rationals (ints coerce).

Parameters:

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

(Rational - a b)

Subtract two rationals (ints coerce).

Parameters:

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

(Rational * a b)

Multiply two rationals (ints coerce).

Parameters:

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

(Rational / a b)

Divide two rationals (ints coerce).

Parameters:

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

(Rational < a b)

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

Parameters:

Returns: BOOL — True if a < b

(Rational = a b)

Test whether a equals b (ints coerce).

Parameters:

Returns: BOOL — True if a equals b