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

后端 未结 7 1918
臣服心动
臣服心动 2021-02-13 16:32

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?

相关标签:
7条回答
  • 2021-02-13 16:43

    DO understand how floating point behave.

    DON'T believe that simple rules will be enough to use them correctly.

    For instance, at least two answers proposed that comparing floating point for equality should be prohibited. First there are cases where comparing them for equality is what is needed. Then when doing range check is what is needed, you also need to be aware that it has its pitfall, for example it isn't transitive which is a property most people will assume for equality test.

    0 讨论(0)
  • 2021-02-13 16:45

    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.

    0 讨论(0)
  • 2021-02-13 16:47

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

    0 讨论(0)
  • 2021-02-13 16:48

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

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

    0 讨论(0)
  • 2021-02-13 16:54

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

    0 讨论(0)
  • 2021-02-13 16:56

    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.

    0 讨论(0)
提交回复
热议问题