by taeric · 1y ago · 0 comments
Signed ints behave like the integers in some tiny subset of representable values. Maybe it's something like the interval (-sqrt(INT_MAX), sqrt(INT_MAX)).
(Alas, most languages don't expose a convenient multiplicative inverse for their integer types, and it's a PITA to write a good implementation of the extended Euclidean algorithm every time.)
You can make the argument that "proper" integers are also bounded in practice by limitations of our universe :)
Instead it's more accurate to think of them as being in scientific notation like 1.23E-1.
In this notation it's clearer that they're sparsely populated because some of the 32 bits encode the exponent, which grows and shrinks very quickly.
But yes rationals are reals. It's clear that you can't represent, say, all digits of pi in 32 bits, so the parent comment was not saying that 32 bit floats are all of the reals.
It's generally accurate to consider floats an acceptable approximation of the [extended] reals, since it's possible to do operations on them that don't exist for rational numbers, like sqrt or exp.
This kinda sent me on a spin, for a moment I thought my whole life was a lie and these functions don't take rationals as inputs somehow. Then I realized you mean rather that they typically produce non-rationals, so the outputs will be approximated.
That said, I'd argue that they are neither reals nor rationals. They are scientific numbers with no syntax to indicate "repeated" tails. Would be like saying that you want someone to represent 1/3 using 6 digits with no bar notation. Best you can do is "0.33333" and that just isn't the same. Moving it so that you have 6 digits with 2 being exponent, you are stuck with "3.333e-01". Which is just different still.
The way I like to frame it, and this will almost certainly not be mathematically rigorous, is that every number a float (as in, the formats defined in IEEE-754) can hold can be sufficiently described as at most a rational, though they cannot represent all rationals, and cannot represent anything "higher" than rationals. That's why I prefer to say they're representing rationals. It's in the sense that they're all representing some rational, not any arbitrary rational.
To do that would require infinite space. E.g. for one third, you'd need infinite binary digits (or even infinite decimal ones, as you say). It's just not how IEEE-754 floats work, as you mention.
This launched me into a research on "perfect numerical accuracy" a while back, and there I did find schemes where you store the numerator and the denominator separately, freeing you from this specific problem. It's a fun topic.
I don't tend to give much thought to real versus rational. Such that I still find it not that surprising to see people treat floats as reals. Especially since most languages make it hard to represent anything else. To that end, it will take me a bit to really internalize what you are saying here. I think I understand it.
That said, my favorite for the craziness of "old is new" is that the literal "1/3" works to represent a rational in Common Lisp. Indeed, I had originally thought that they had some specific constants for common fractions defined. Nope, they just support rational literals.
And I question if you need infinite space to represent repeated decimals. Strictly, you just need a way to indicate the repeating. No?
Tom7 has a good video about this: https://www.youtube.com/watch?v=5TFDG-y-EHs