为什么在特定的Visual Studio 2008项目中双击添加不正确? [英] Why are doubles added incorrectly in a specific Visual Studio 2008 project?

查看:154
本文介绍了为什么在特定的Visual Studio 2008项目中双击添加不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将java代码移植到C ++我已经偶然发现了一些奇怪的行为。我不能得到双倍的工作(即使编译器选项/ fp:strict这意味着正确的浮点数学在Visual Studio 2008中设置)。

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" floating point math is set in Visual Studio 2008).

double a = 0.4;
/* a: 0.40000000000000002, correct */

double b = 0.0 + 0.4;
/* b: 0.40000000596046448, incorrect
(0 + 0.4 is the same). It's not even close to correct. */

double c = 0;  
float f = 0.4f;  
c += f;
/* c: 0.40000000596046448 too */

在另一个测试项目中工作正常(/ fp:strict根据IEEE754)。

In a different test project I set up it works fine (/fp:strict behaves according to IEEE754).

使用没有优化和FP:strict的Visual Studio 2008(标准)。

Using Visual Studio 2008 (standard) with No optimization and FP: strict.

任何想法?是真的截尾到浮动吗?这个项目在java和C ++端都需要相同的行为。我通过从VC ++中的调试窗口读取所有的值。

Any ideas? Is it really truncating to floats? This project really needs same behaviour on both java and C++ side. I got all values by reading from debug window in VC++.

解决方案: _fpreset // Barry Kelly的想法解决了。库已将FP精度设置为低。

Solution: _fpreset(); // Barry Kelly's idea solved it. A library was setting the FP precision to low.

推荐答案

我唯一能想到的就是你链接库或DLL,控制字。

The only thing I can think of is perhaps you are linking against a library or DLL which has modified the CPU precision via the control word.

您尝试从 float.h 调用 _fpreset() code>之前有问题的计算?

Have you tried calling _fpreset() from float.h before the problematic computation?

这篇关于为什么在特定的Visual Studio 2008项目中双击添加不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆