4 ms·
Then what's the best way to handle these cases? Are there any set of rules we should use while implementing the mathematical equations dealing with limits in fl
by the-Black-Dread 3y ago
Then what's the best way to handle these cases? Are there any set of rules we should use while implementing the mathematical equations dealing with limits in floating points.
- Dwedit 3y agoFor a summation, add the smaller numbers first. Smaller as in 0.0000000000053, not like -5172365126.
- Dban1 3y agohow about small as in ²³⁷
- zokier 3y agoFor summation just go with Kahan summation or some improved variant of it https://en.wikipedia.org/wiki/Kahan_summation_algorithm https://en.wikipedia.org/wiki/Kahan_summation_algorithm
- lifthrasiir 3y agoAnd if you can't remember that, you can use a pairwise summation like (((a+b)+(c+d))+((e+f)+(g+h)))+... which gives a decent error reduction in return.
- OscarCunningham 3y agoFloating point numbers have the highest precision when they're near 0. So if you know a number is going to be near 1, like exp(x) when x is small, then it's better to calculate the offset from 1 rather than the number itself. Programming languages make 'expm1' available for this purpose, which computes exp(x)-1.
- stabbles 3y agoBasically - don't subtract almost equal numbers - don't add numbers of vastly different magnitudes
- deleted 3y ago[deleted]
- edflsafoiewq 3y agoUseful list of tricks: http://www.plunk.org/~hatch/rightway.html http://www.plunk.org/~hatch/rightway.html
- an1sotropy 3y agoI love that that page is nearly the same as it when I saw it ~20 years ago. Things things are still the right way.
- topaz0 3y agoOther commenters have pointed to tricks that are undoubtedly useful, but the real answer imo is to understand where rounding error will be introduced in your problem, and where it can become catastrophic so that you can use careful alternatives in those cases.
- unbalancedevh 3y agoI'll second this. Implementing tricks can improve some use cases but make others worse.