Do-s and Don't-s for floating point arithmetic

2019-03-24 17:34发布

What are some good do-s and don't-s for floating point arithmetic (IEEE754 in case there's confusion) to ensure good numerical stability and high accuracy in your results?

I know a few like don't subtract quantities of similar magnitude, but I'm curious what other good rules are out there.

7条回答
贪生不怕死
2楼-- · 2019-03-24 17:49

DO remember that because of faulty floating point arithmetic people died and billion dollars of damages occured.

查看更多
在下西门庆
3楼-- · 2019-03-24 17:50

My "main weapon" for avoiding floating-point pitfalls is to have a firm grasp on the way they work. I think Chris Hecker explains the basics pretty well.

查看更多
祖国的老花朵
4楼-- · 2019-03-24 17:52

The #1 "don't" rule with floating-point numbers is:

Don't use floating-point numbers where integers will suffice.

查看更多
神经病院院长
5楼-- · 2019-03-24 17:54

never try to do an equals compare

double da,db;

...

if (da==db) then something.

remember that C uses double by default so if you want to do single precision, be clear about it

float fa,fb;

...

fa = fb + 1.0;

will convert fb to double do a double add then convert to single and do a single equal

Instead

fa = fb + 1.0F.

all single.

If you are going to use a whole number like 1.0 dont make it a decimal in your code. you get more reliability out of your compilers/tools if you can minimize the ascii numbers. so

fa = fb + 1;

or instead of

fa = fb + 0.3333333F;

do something like this (if interested in accuracy).

fc = 1; fc = fc / 3; fa = fb + fc;

Lots and lots of others, floating point is painful, compilers and libs are not that good, fpus have bugs, and IEEE is exceptionally painful and leads to more bugs. Unfortunately that is the world we live in on most platforms.

查看更多
等我变得足够好
6楼-- · 2019-03-24 18:04

Search for, download and read "what every computer scientist should know about floating point arithmetic"

查看更多
啃猪蹄的小仙女
7楼-- · 2019-03-24 18:05

First, enter with the notion that floating point numbers do NOT necessarily follow the same rules as real numbers... once you have accepted this, you will understand most of the pitfalls.

Here's some rules/tips that I've always followed:

  • NEVER compare a floating point number to zero or anything else for that matter (IE don't do: if (myFloat == 0)
  • Associative property does not hold for floating point... meaning (a + b) + c != a + (b + c)
  • Remember that there is always rounding
  • Floating point numbers do not necessarily have a unique inverse
  • No closure with floating point numbers... never assume that the result of a floating point operation results in a valid floating point number.
  • Distributive property does not hold
  • Try to avoid using floating point comparisons at all... as round off error can cause unexpected results
查看更多
登录 后发表回答