Why are doubles added incorrectly in a specific Visual Studio 2008 project?

前端 未结 2 1882
南旧
南旧 2021-01-03 03:35

Trying to port java code to C++ I\'ve stumbled over some weird behaviour. I can\'t get double addition to work (even though compiler option /fp:strict which means \"correct\

2条回答
  •  执笔经年
    2021-01-03 03:59

    Yes, it's certainly truncating to floats. I get the same value printing float f = 0.4 as you do in the "inaccurate" case. Try:

    double b = 0.0 + (double) 0.4;
    

    The question then is why it's truncating to floats. There's no excuse in the standard for treating 0.0 + 0.4 as a single-precision expression, since floating point literals are double-precision unless they have a suffix to say otherwise.

    So something must be interfering with your settings, but I have no idea what.

提交回复
热议问题