The Rational class stores a floating point number, but when it is displayed it tries to find a fractional equivalent. Doing math on Rationals tends to convert regular floating point numbers into Rationals.
Rational is included in dewdrop_lib; if you are using this library, you don't need to download the class here.
Useful for working with harmonic or other ratios and just intonation.
Note: There is a performance cost (math on Rationals is slower than math on Floats). Not recommended for use in performance situations. Use Rationals to generate arrays of ratios, then convert them to floats for performance routines.
To report bugs: send email to hjh
Examples:
3 /% 2 // create a rational 3/2 (3/2).asRational // same thing 3/2 c = (0..15) /% 12 // make an array of rationals [ 0, 1/12, 1/6, 1/4, 1/3, 5/12, 1/2, 7/12, 2/3, 3/4, 5/6, 11/12, 1, 13/12, 7/6, 5/4 ] c.asFloat [ 0, 0.083333333333333, 0.16666666666667, 0.25, 0.33333333333333, 0.41666666666667, 0.5, 0.58333333333333, 0.66666666666667, 0.75, 0.83333333333333, 0.91666666666667, 1, 1.0833333333333, 1.1666666666667, 1.25 ] a = 1 /% 2; b = 1 /% 3; a + b 5/6 a - b 1/6 a * b 1/6 a / b 3/2 a + 0.8 // add a float, result is still Rational 13/10 0.8 + a // commutative 13/10 a + ({ rand(15) * 0.125 } ! 10) // add Rational to array of floats--result is Rational [ 3/2, 15/8, 3/4, 2, 15/8, 1, 5/4, 7/4, 3/4, 5/4 ] c = a + 2.sqrt // 2.sqrt does not easily resolve to a rational fraction, so it displays as float 1.9142135623731 c + 0.25 - 2.sqrt // but internally it's still Rational and displays so whenever possible 3/4 c.round(0.001) // similarly (chopping off part of it) 957/500