3 ms·
The thing that made floating point easy to understand for me was the concept of a dyadic rational [1]. Those are just the rational numbers whose denominator is
by pcwalton 7y ago
The thing that made floating point easy to understand for me was the concept of a dyadic rational [1]. Those are just the rational numbers whose denominator is a power of two (e.g. 1/2, 3/16, 61/32). Fundamentally, binary floating point represents dyadic rationals (not counting special stuff like NaN, infinity, etc.) To represent such a number in floating point form, just write it as a/2^b. Then the mantissa is a, and the exponent is -b.
This intuitively makes it obvious why, for example, 1/3 can't be represented. I find the rational form like a/b easier to understand than the "scientific notation" form like a×2^b.
[1]: https://en.wikipedia.org/wiki/Dyadic_rational https://en.wikipedia.org/wiki/Dyadic_rational
- jacobolus 7y ago> a/2^b. Then the mantissa is a, and the exponent is -b Why not just write a * 2^b. Your a here, if supposed to represent the mantissa, is not an integer. This form doesn’t really clarify the relationship to the concept of a dyadic rational. To write the floating point number as a dyadic rational treating a as an integer you would have something like (2^(53) + a) / 2^(53 - b). But arbitrary dydadic rationals cannot be represented by a single floating point number. If you want arbitrary dyadic rationals you need the sum of a list of floats. * * * I think what you might be getting at is starting by teaching people about fixed-point binary numbers? Or just binary fractions in general, analogous to the usual decimal fractions? That would be clearer to say than anything about “dyadic rationals”. After going through a typical primary school, people have the concept of “terminating decimals”, rather than a concept of “ten-adic rationals”.
- twic 7y agoI think of a float as an integer with a shift. Take the mantissa, stick on the implicit leading 1, put it in the middle of an infinitely big string of zeroes with a decimal (well, binary) point at the right-hand end, then shift it by the exponent (which can be positive or negative). If you're comfortable thinking about shifting integers, then perhaps this is a simple way to think about floats. For me, it's easier than thinking about them as fractions.
- deleted 7y ago[deleted]
- emmelaich 7y agoYep, this is how the Unix `bc` tool works; all numbers are internally simple decimal but have a `scale` and `length`. Scale is the number of dec digits after the decimal point and length is the number of significant figures. See `man bc`
- pcwalton 7y ago> To represent such a number in floating point form, just write it as a/2^b. Then the mantissa is a, and the exponent is -b. (As a follow-up, since I can't edit: This isn't exactly correct, because of the leading "1.", but it's close enough to aid understanding.)