Error due to limited precision of float and double

心已入冬 提交于 2019-12-05 21:05:01

I guess I'll make my comment an answer and expand on it. This is my hypothesis, I may be wrong.

MinGW on Windows is probably trying to preserve precision by promoting the intermediates of expressions to the full 80-bit precision of x86.

Therefore, both sides of the expression n != (n-dec) are evaluated to 64-bits of precision (80-bit FP has a 64-bit mantissa).

2^-64 ~ 10^-20

So the numbers make sense.

Visual Studio also (by default), will promote intermediates. But only up to double-precision.

Why dont you check the size of float and double in both os?

This simply shows that the different environments use different sizes for float and double.

According to the C++ specification, double has to be at least as large as float. If you want to find out just how large the types are on your system, use sizeof.

What your tests seem to indicate is that g++ uses separate sizes for float and double (32 and 64 bits respectively) while MinGW32 on your Windows system uses the same size for both. Both versions are standard conforming and neither behaviour can be relied upon in general.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!