Rational:
Filter:
Rational (extension) | Math

Rational
ExtensionExtension

Rational number

Description

A rational number. Alternative syntax: x %/ y

NOTE:

Class Methods

.new

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

Instance Methods

%/

Alternative syntax for Rational(x,y)

+

Addition

-

Subtraction

*

Multiplication

/

Division

.abs

Absolute value.

.asFloat

As Float

.pow

.reciprocal

Reciprocal

.squared

Power of two

Examples

Basic operations

Comparisons

Sorting

Continued fractions

Recursive function to calculate finite continued fractions:

Stern-Brocot numbers

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 Rational and Float

Conversion between Float and Rational can have different degrees of precision:

Sound Example