Rational


An adapter (wrapper) for a floating point number which, within a given precision, will attempt to display as a fraction of two integers. Normal math operators may be used on rationals, and the result will be a rational. If a rational contains an irrational number, the precision test will fail and the number will display as floating point.


Useful for working out just intonation or other ratio-based tuning systems.


Creation:


Rational(1.5)

1.5.asRational

3 /% 2


All the above display "3/2."


Math:


a = 3 /% 2;

b = 5 /% 4;

a+b // == 11/4

a-b // == 1/4

a*b // == 15/8

a/b // == 6/5


a + 2.sqrt // == 2.9142135623731

a + 2.sqrt - 2.sqrt // == 3/2