Timothy S. Nelson wrote:
* Should there be a way to extract information about a Rat's
numerator/denominator? What methods does a Rat have?
Rat.avoid_trap(), Rat.eatcheese(), Rat.race(), etc. Oh, wait,
*that* kind of Rat :).
Don't forget that a Rat can have at least one other good representation in terms
of integers.
While the numerator/denominator pair is good, I say that the
mantissa*radix^exponent triple is better, especially so when representing very
large or very small numbers like in the generalization of an IEEE-754 floating
point number (but, with the most common actual IEEE-754 values, the radix is
always 2).
With the triple, then typically the size of integers we are dealing with are not
larger than the size of the mantissa portion, which corresponds directly to the
amount of precision we have, and the radix/exponent would tend to be just 1-5
digits or so.
In contrast, with numerator/denominator, if you wanted to represent, say, 10^30
or 10^-30, you would use at least 30 decimal digits even if your precision is
say 5 digits, and most of your digits are effectively or actually zeros. So
math with the triple may be slightly more complicated than with the pair, but
not by much, and it scales a lot better.
In the interest of TIMTOADY, I suggest that Rat be a role and at least 2
implementations exist, one using the pair internally, and one using the triple
internally (both can exactly represent every rational), since each would perform
better for different scenarios, and you could get silent conversions between
them by operators when that makes sense; in any event they would both support
exactly the same accessors, so users could extract either the pair or the triple
from either. I think you do not want the pair being the only representation. I
think it also goes without saying that using === on 2 values of different said
representations will mutually normalize and compare on the actual rational
numeric values.
Or maybe this isn't really the domain of role exposure and the internals of pair
vs triple are really just banished to the implementation details that aren't
exposed in the language. Same as with silently using machine ints vs big ints
behind Int as the situation warrants. And then there's no worrying about === or
whatever not just working. So then the main thing for the language to care
about is just that information about a Rat in terms of Ints can be extracted in
either format.
-- Darren Duncan