A rational number. Alternative syntax: x %/ y
Rational can be one order of magnitude slower compared to Float.Rational internally represents numerator and denominator as 64-bit Floats. If you want to initialize it with a term outside the range -2147483648 to 2147483647 you will need to force the number to be a Float by adding a decimal zero, e.g. -2147483649.0 and 2147483648.0.An operation with an exact rational answer gives one. floor, ceil, frac, trunc, round, roundUp, mod, wrap and fold are closed over the rationals, and each returns a Rational.
Everything else in the Number vocabulary is answered from asFloat, because there is no rational answer to give — a rational has no rational square root. respondsTo agrees with this, so a caller may ask before sending.
A denominator of zero, a division by zero and the reciprocal of zero throw.
Rational also accepts Floats and Strings in its new instantiation method. This class will always reduce a rational expression to its lowest terms (e.g. 3 %/ 2 instead of 6 %/ 4).
Alternative syntax for Rational(x,y)
Addition
Subtraction
Multiplication
Division
Absolute value.
As Float
Reciprocal
Power of two
Whether the numerator is zero, whatever the denominator.
The nearest integer below and above, as a Rational.
The part above the floor, so never negative.
Quantize down, to nearest, and up. Like sclang's, these quantize toward -inf rather than toward zero, so (-7 %/ 2).trunc is -4 %/ 1.
Remainder, taking the sign of the divisor as sclang's does.
Map into a range, wrapping or reflecting. These follow the Float definitions, where hi is outside the range —
Integer:wrap and Float:wrap are different operations in sclang. 5.wrap(0,3) is 1 and 5.0.wrap(0,3) is 2.0. A Rational is a continuous quantity, so it follows the Float one.Iterate as a number, counting exactly as the equivalent Float does.
Recursive function to calculate finite continued fractions:
In number theory, the Stern–Brocot tree is an infinite complete binary tree in which the vertices correspond one-for-one to the positive rational numbers, whose values are ordered from the left to the right as in a search tree.
Conversion between Float and Rational can have different degrees of precision: