Karl
(👑🦆)
January 22, 2023, 7:39am
3
It has come up a few times before.
Fractions are worse than both floating-point and fixed-point numbers is almost every way. Most importantly:
If you don't round results, in computations that aren't designed to work out just so , the size of the numerator and denominator is exponential in the number of terms in the computation.
If you do round results, the result will generally be significantly less accurate than what you would get with an equivalently-sized fixed- or floating-point representation, because rationals are not uniformly spaced and have redundant representations.
The are legitimately useful for some computational geometry operations where the number of terms involved is tightly bounded, but those computations can also just be rephrased in terms of integers, so you don't really need a rational type to implement them.
They're also real good at representing 1/3
exactly, which seems appealing, but has more psychological benefit than anything else.
To paraphrase Ben, rationals are the linked lists of numerics. There's a very narrow set of situations in which they're a good option, but that set of situations is vastly more limited than most people think it is. I've written about their drawbacks a few times, but to paraphrase:
If you round results, it is always strictly better to use floating-point, because it has scale invariance and fewer redundant representations, so for any fixed precision it has smaller error.
If you do not round results, the sizes of the numerator and denominator grow exponentially in general.
Because of this, any non-trivial fixed-size rational program overflows immediately.
All that said, this is generally a well-written proposal, and rational types do have some limited uses
If the use-cases really are so limited, it is probably more appropriate to encourage people to use a package (perhaps swift-numerics
, or one of the other third-party libraries you mention) rather than adding these to the standard library.
7 Likes