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

后端 未结 7 1919
臣服心动
臣服心动 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 17:07

    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

提交回复
热议问题