C#.NET双重问题...... 6.8!= 6.8? [英] C# .Net double issue... 6.8 != 6.8?

查看:137
本文介绍了C#.NET双重问题...... 6.8!= 6.8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做的工作的一些单元测试和一个特殊的错误弹出的断言之一。需要注意的是expectedValue和actualValue都是双打。

I was doing some unit testing at work and a peculiar error popped up for one of the assertions. Note that expectedValue and actualValue are both doubles.

Assert.AreEqual(expectedValue, actualValue);

例外说,他们不相等,阐述了期望值:其中,6.8>实际值:< 6.8>

The exception stated that they were not equal, elaborating that "expected value: <6.8> actual value: <6.8>."

期望值是硬codeD 6.8和实际值是使用数据库值经过我们的分类方法(如相等的记录,或詹克斯自然间断)制定。

The expected value is a hard coded 6.8 and the actual value is formulated using database values going through our classification methods (such as Equal Records, or Jenks Natural Breaks).

我的猜测是,所不同的是可能是2个值的尾数是相似的,直到至少显著位。我更新了测试,包括小量找到,如果这两个值足够接近,但我很好奇,如果有办法迫使尾数来匹配,如果我显示的双显示值。有没有这样的尾数纠正存在吗?

My guess is that the difference is probably that the mantissas of the 2 values are similar up until the least significant bit. I updated the tests to include an epsilon to find if the two values are close enough, but I'm curious to if there is a way to force the mantissa to match with the display value of if I displayed that double. Does such a mantissa correction exist?

推荐答案

我不完全知道你所说的强制尾数相匹配的显示值的意思是......还有的的无重值这是完全0.1,例如

I'm not entirely sure what you mean by forcing the mantissa to match the display value... there are no double values which are exactly 0.1, for example.

如果你想一些code,显示的确切的双,但是,我有一个的 DoubleConverter.cs 文件,这使得它易于:

If you want some code to display the exact value of a double, however, I have a DoubleConverter.cs file which makes it easy:

 double d = 0.1;
 string x = DoubleConverter.ToExactString(d);

另一种方法是使用往返格式说明(R)将double转换字符串时 - 即保证结果有足够的信息来复制相同的精确值。换句话说,如果 X!= Y ,然后 x.ToString(R)!= y.ToString(R)

这篇关于C#.NET双重问题...... 6.8!= 6.8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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