I wish people were more willing to use rational numbers in programs. Rather than leaving something as 1/24, people almost always perform the division and wind up with a float.
I've never seen rationals in a codebase. And that's a shame, because they do some things elegantly.
I think the problem is that the numerator and denominator in reduced form can easily get pretty big, even if the magnitude of the number itself isn't all that big. E.g. in the rational number
1 + 1/2 + 1/3 + ... + 1/24,
the denominator already can't fit in a 32-bit int.
You can reasonably efficiently find the nearest rational number whose numerator and denominator fit in a given sized type. This means your rationals are imprecise, just like floats; the sparse subset of numbers they can represent exactly is different from floats' (and possibly more useful).
10 comments
[ 3.9 ms ] story [ 36.9 ms ] threadI agree on the overboard marketing spiel but the idea is actually rather clever.
I've never seen rationals in a codebase. And that's a shame, because they do some things elegantly.
You could think of fixed point as a type of rational. ;)
In computer graphics we sometimes use rational curves. It's not the same as rational numbers, but related.
1 + 1/2 + 1/3 + ... + 1/24,
the denominator already can't fit in a 32-bit int.