86 points · by arunc · 7y ago · 98 comments
Even with a floating point processor you need to be careful when switching into and out of interrupts! The floating point core has it's own set of registers you need to keep track of. Doubles are even worse!
Basically it is almost always better to use integers when you can, and be aware of what types you are using (always important in C anyways).
Called cos() when you meant cosf()? Done in software.
Multiplied by 2.25 when you meant 2.25f? Done in software.
-Wdouble-promotion will help you find those performance bugs.
> Basically it is almost always better to use integers when you can
I'd say: understand the limitations of FP, but it's almost always better to use them unless you have some particular precision or performance requirement. Then again, my experience of FP is mostly in graphics, where IEEE 754 single precision is good enough for almost anything you'd want, and hardware FP is commonplace.
Maybe there are better ways to do it, I could be wrong!
$ python3 -c 'print(0.3 == 0.1 + 0.2)'
False
$ ruby -e 'puts 0.3 == 0.1 + 0.2'
false
$ perl6 -e 'say 0.3 == 0.1 + 0.2'
TrueUsing imprecise but efficient-to-calculate-with-numbers for exact literals by default is probably the most pervasive premature optimization in all of computing.
This is exactly the same situation as with floating point numbers. In both cases you have a predetermined finite set of rational numbers, with exact rational arithmetic when you stay within those numbers, and deterministic rules when your operation exits this finite set.
> Using imprecise but efficient-to-calculate-with-numbers for exact literals by default is probably the most pervasive premature optimization in all of computing.
I do not see how this is a matter of precision nor efficiency. Floating point arithmetic was deemed more useful for general purpose for good reasons: the representable numbers are mostly scale-free, so that you do not care about the absolute size of your numbers (you can compute in armstrongs or in parsecs and obtain essentially the same results). With rational arithmetic using bounded integers, you cannot represent very large or very small numbers. On the other hand you can represent small fractions like 1/3, which is arguably useful in some cases, but not really a big deal in practice. There's no reason why rational arithmetic with bounded numerator and denominator could not be efficiently implemented in hardware as fast as floating point; I do not understand your point.
I imagine whole classes of unexpected problems like timing side channel attacks, financial types expecting it to work better and missing easily overlooked corner cases...
How do I know this? I spent two years investigating exact versus inexact arithmetic in the context of computational science.
>It uses exact rational arithmetic for values that it can represent that way, preferring correctness to efficiency
Fwiw, floats do exactly this the only difference is that you've changed your base from 2 to 10 and introduced unnecessary computation for what is maybe a 10% increase in exactly computable values.
https://docs.perl6.org/language/numerics#Rational
By irrational I am not joking, these sorts of number systems are complicated and glitchy, in this case with program-destroying magic like autoconversion to floating point when you hit a 2^64 denominator, and, don't click this URL, please, just read it:
https://docs.perl6.org/language/numerics#Zero-denominator_ra...
Could you expand on why it is a totally irrational idea? Seems pretty rational to me. But then I'm not really a mathematician.
> autoconversion to floating point when you hit a 2^64 denominator
Please note that this is after normalization. And it has come from practicality: having the denominator also be an BigInt, slows down the use of the Rational number significantly. However, if you do need that type of precision, then you can by using infectious FatRats: any expression with a FatRat, will result in a FatRat (unless explicitly coerced to something else, of course).
With regards to the Zero-denominator Rats: they are just special cases just as IEEE has special cases for -Inf, Inf and NaN. So what's the problem there?
fmt.Print(0.3 == 0.1 + 0.2)
// => true
https://repl.it/repls/SubtleQuarrelsomeMaintenanceWhy does it have this result?
Go: Here's a trivial example.
If you want a thorough understanding you will want to look at representation-error, rounding-error, error-propagation, why they exist and how they interact.
The interplay between those three forms of numerical error in floating point numbers will also allow you to more easily see the world of limitations of fp beyond 0.1 + 0.2 for yourself.
> It’s not stupid, just different.
Over the past 50 years, my computer has adapted to how humans normally operate in nearly every other way. Why do they continue to use this system which produces results different from what any normal person expects?
> Computers use binary numbers because they’re faster at dealing with those
Computers are faster at dealing with all-caps ASCII, too, but we've accepted here that micro-optimization is less important than doing what people want. Most of the languages I use have even moved past fixnums. Why have we not improved real arithmetic since 1985?
Lisps and Lisp-derived languages, like Scheme, have had a proper numerical tower, including rationals, for decades now. Using reals is optional, but using rationals and everything else imposes an efficiency cost, so people make their decision. Implementing rationals in hardware would not necessarily make them more efficient; that is, if you think having rational support in hardware would help, you have to make the case. It isn't an automatic win:
https://yosefk.com/blog/its-done-in-hardware-so-its-cheap.ht...
“Efficiency” is tough to believe given all the other inefficient yet nice features that have been universally adopted, like Unicode, variable length lists, bigints, etc. In many dynamic languages, every method call is a hash table lookup, yet we’re expected to believe they don’t use Decimals by default because it would be too slow? In C++ I’d buy that excuse.
If you look at any low level hardware it's behavior will be alien and unintuitive to most end-users, your computer has not adapted, only it's ability to support sophisticated high level software that abstracts these things away from end users has improved.
FPUs are no different, calculators round output to 10sf, and as a result most users think calculators are perfect, and that's usually fine... it's not fine when you do programming, because eventually you will need to understand the fundamental limitations of the hardware and more generally finite numerical computation regardless of implementation.
Because most people actually want either fixed-point or floating-point arithmetic. Especially if you only consider the population who is willing to spend money to get better hardware to support their use cases.
The "On Using Integers" page seems very narrowly applicable because it only discusses fixed-point in the context of currency; in a lot of other applications, notably DSP and other multimedia, fixed-point is the norm.
AFAIK in DSPs you use fixed point for a completely different reason, namely because it's simpler and thus more efficient to implement in silicon.
The "absurd" result that [1+2 != 3] * (0.1) isn't even the problem with floating point. It's an even simpler misconception that "even" vs. "repeating" fractions are the same for base 10 vs. base 2 fractions.
So we can understand that 0.33+0.33+0.33 != 1 in decimal due to rounding. So when 0.1 (decimal) which is a truncated repeating "binary decimal" fraction isn't exact we loose our shit. But why is base 10 good and base 2 bad? Why not base 57? Clearly for any base, a truncated decimal-like fraction will be inexact for a lot of numbers. Rounding errors will add up, but this is really a binary vs. decimal issue.
More subtle are errors that grow simply because of issues specific to floating point. For example re-normalization when numbers of very different magnitudes are added or subtracted which eventually causes precision loss.
This is neither hard to understand, nor the only complication with floating point, nor is anything wrong, or bad, nor should anything be fixed in IEEE-754. This is just stuff that numeric programmers MUST understand, at least at the rudimentary level.
No, it is not hard to understand, but it is not obvious, and in fact counterintuitive to most people. Which is why there is a need for a website like this for people to refer to intead of the Goldberg paper which is great if you actually need to do error propagation analysis on your numeric algorithm but which does not give this basic explanation and is therefore not suitable to be cited to people who are suprised that 0.1 + 0.2 != 0.3
So please explain to me how referring to the Goldberg paper as "long article with lols of formulas" is anything but factually correct and a good reason to provide an alternative?
This article is "terrible" because it gives a single bad example and derides a very good reference implying it's too complicated. And steals it's title.
Every programmer that uses floating point should read Goldberg. That blog post is garbage. Post the real source, or an informative article, not junk.
So did I on a small website (yes, I'm the author). It's a website. Not an article. Not a blog. There is more than the title page, which seems to be the only thing you've looked at.
> This article is "terrible" because it gives a single bad example
Which example? What makes it bad?
> and derides a very good reference implying it's too complicated.
I absolutely do not "deride" the Goldberg paper, and it is too complicated for people without a CS background. That doesn't mean there's anything wrong with the paper, or with those people. There is something wrong with kneejerk-posting a link to the paper as an RTFM to basic questions about floating point behaviour. My intention was to provide a better resource for those cases.
> And steals it's title.
Don't be ridiculous. I use the format of the title, which lots of people do (it's basically become a meme now, not sure if Goldberg was the first to use it either), with a critical difference that you seem to have missed: Goldberg adresses his paper to "every Computer Scientist", I address my website to "every programmer". Different audiences.
> Every programmer that uses floating point should read Goldberg.
Maybe. But it is not a good introduction.
> That blog post is garbage. Post the real source, or an informative article, not junk.
You have still not been able to explain what is wrong with it. I am beginning to think you have serious problems with reading comprehension.