std::pow produce different result in 32 bit and 64 bit application

后端 未结 4 1133
旧巷少年郎
旧巷少年郎 2020-12-11 18:20

I have found the mismatch in the result of some complex calculations. When i thoroughly observed the intermediate results, its the std::pow function which creates that misma

4条回答
  •  时光取名叫无心
    2020-12-11 18:30

    The most important thing to understand about floating-point calculations is that they are (almost always) inexact. Most numbers cannot be represented exactly as a floating-point number. And even when the result of a calculation can be represented exactly, the result actually computed may still not be exactly correct.

    The way to deal with this is to write code that does not depend on getting exact results. For example, you should almost never test floating point numbers for equality. Or if you need to test if a number is positive, your program may need to reject extremely small positive numbers (they're approximately negative) or accept extremely small negative numbers (they're approximately positive).

    Similarly, you should try to avoid numerically unstable algorithms, since these small errors blow up quickly; conversely, you should try to use numerically stable algorithms, since those are error tolerant.

    How to do numerical calculations well is an entire field of study!

提交回复
热议问题