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
.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
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: