On Dec 20, 2004, at 6:17 AM, Ph.E wrote:
Would it be possible to add a function in SC which could convert a
decimal into a fractional?
For example, 1.125 --> 9/8
It would be very useful when dealing with harmonic ratios.
this request does not really make sense. For one thing the computer
does not represent anything with decimal. it is binary. and with
binary floating point numbers the denominator can only be a power of
two. All floating point numbers are integers times a power of two.
So you have conversions like this: 0.1 -> 13421773 / 134217728 which
is the rational number that is represented by the floating point
number with the closest approximation to the string "0.1".
f = {|value|
var bits, mantissa, exponent, denominator;
bits = value.as32Bits;
mantissa = (bits & 0x007FFFFF) | 0x00800000;
exponent = 127 + 23 - ((bits & 0x7F800000) >> 23);
denominator = 1 << exponent;
[mantissa, denominator, mantissa / denominator].postln;
};
f.value(0.1);
f.value(4/3);
f.value(5/2);
_______________________________________________
sc-users mailing list
sc-users@xxxxxxxxxxxxxxx
http://www.create.ucsb.edu/mailman/listinfo/sc-users