4 ms·
> haven't read deeply into IEEE 754-2008 for decimal floating points, but it seems like it should be pretty fast (relative to system calls) to convert binary t
by kstenerud 6y ago
> haven't read deeply into IEEE 754-2008 for decimal floating points, but it seems like it should be pretty fast (relative to system calls) to convert binary to decimal because 10 has a factor of 2.
It could be fast, but legacy has doomed us all to a "canonical" conversion of sorts, where any other conversion algorithm will likely yield off-by-a-tiny-amount differences in the binary format (like 1.200000000031 instead of 1.2). There are in fact fairly simple conversions that can be done, but they yield results that are incompatible with printf.
> Is there any reason big float implementations should use decimal rather than binary?
At the end of the day, we work in decimal. So every binary result we calculate has to be converted to its decimal approximation (the meaning of which is subject to convention). Rounding is also an issue, because you want to keep your number of significant digits within reason to avoid false precision errors. Doing all of this in a different base that can't be 1:1 converted adds a whole slew of bug opportunities and corner cases.
- thethirdone 6y ago> It could be fast, but legacy has doomed us all to a "canonical" conversion of sorts, where any other conversion algorithm will likely yield off-by-a-tiny-amount differences in the binary format (like 1.200000000031 instead of 1.2). There are in fact fairly simple conversions that can be done, but they yield results that are incompatible with printf. I would argue that the issue here is not the binary -> decimal conversion. People expect when they write "1.2" they get that exact value, but that is not representable with binary floats. So the weird value you get is the closest value that is representable. I definitely agree that there aren't good options to make a round trip fast and intuitive. > At the end of the day, we work in decimal. So every binary result we calculate has to be converted to its decimal approximation (the meaning of which is subject to convention). Rounding also is an issue, because you want to keep your number of significant digits within reason to avoid false precision errors. Doing all of this in a different base that can't be 1:1 converted adds a whole slew of bug opportunities and corner cases. If you are keeping track of significant digits, you can definitely work in binary and render the binary value to the significant decimal digits. In particular for scientific work, if you cannot handle the issues with binary floating point, you probably are not handling uncertainty well enough. The corner cases you would hit would already have been bugs, but you just wouldn't have noticed. One particular setup I am a fan of is keeping track of an upper and lower bound for all of your numbers which allows your computations to introduce a small amount of error , but you will still have objectively true statements when converting back to decimal to read. For example "1.2" would be converted to the range "1.001-1.010" in binary with 4 significant figs which when converted back would be the range "1.125-1.25". Its not perfect, but it steps around a lot of typical floating point problems.
- enriquto 6y ago> At the end of the day, we work in decimal. What? No. I have spent much of my life working with floating point numbers and never ever had to resort to anything decimal for serious purposes (that is, except for some occasional printing of a number for debugging).
- smabie 6y agoAre you telling me that you do math with a pen and paper in binary? everything is decimal. Computers aren't, but almost everyone tries to paper over that fact as much as possible.
- enriquto 6y ago> Computers aren't, but almost everyone tries to paper over that fact as much as possible. Do you realize that many entire fields of scientific computing (i.e., computer vision, fluid mechanics, solid mechanics, signal processing, numerical partial differential equations, computational chemistry, climate modeling, and dozens of other things) couldn't care less whether the inner representation of floating point numbers is decimal or binary? If binary can be made just a little faster, so be it.
- smabie 6y agoI'm not saying anyone thinks we should have computers process numbers as decimal. I'm just saying that no one thinks (like the brains internal representation) in decimal binary, that's it.
- enriquto 6y agoI don't think about real numbers as decimals either (for one, the decimal representation is not unique). For many people real numbers are points on a straight line.
- smabie 6y agoI'm not sure why I'm replying, but let's say someone asks you to square 1.2 in your head. You think about points on a line?