c# - 添加两个 double 给出错误的结果

标签 c# .net floating-point double addition

我正在使用下面的一段代码,在一些神秘的情况下,加法的结果与预期的不一样:

double _west = 9.482935905456543;
double _off = 0.00000093248155508263153;
double _lon = _west + _off;

// check for the expected result
Debug.Assert(_lon == 9.4829368379380981);
// sometimes i get 9.48293685913086 for _lon (which is wrong)

我在我的应用程序中使用了一些 native DLL,我怀疑是某些 DLL 造成了这种“计算错误”,但我需要找出是哪个 DLL。 任何人都可以提示我如何找出问题的根源吗?

最佳答案

double 不完全准确,请尝试使用 decimal 代替

使用 double 和 float 相对于 decimal 的优势在于性能

关于c# - 添加两个 double 给出错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1193630/

相关文章:

c# - ReportViewer 在 GetDefaultPageSettings() 上锁定了很长一段时间

c# - WCF 客户端/服务器配置不匹配

c# - 如何让 TextBox 只接受数值?

java - .NET Remoting 的开源实现

database - PostgreSQL 浮点运算和类型

c# - float 中的E是什么?

c# - Unity,奇怪的调试

c# - 如何使用反射调试从 app.config 加载的对象?

.net - CruiseControl CommunicationsException 对象引用错误

python - 如何截断浮点值?