Double.Epsilon的含义 [英] Meaning of Double.Epsilon
问题描述
我正在努力解决double.Epsilon的想法。我已经写了
以下测试:
[测试]
public void FuzzyDivisionTest()
{
double a = 0.33333d;
double b = 1d / 3d;
Assert.IsFalse(a == b,内置==运算符不应该是
模糊;)
Assert.IsTrue(ab< double.Epsilon);
Assert.IsTrue(Math.Abs(ab)< double.Epsilon);
Assert.IsTrue(Math.Abs(ba)< double.Epsilon);
}
然而,有些事情很奇怪。前2个断言通过,但
最后2个失败。 Math.Abs()是否会失去我的Epsilon值?这是正确的吗?
I am grappling with the idea of double.Epsilon. I have written the
following test:
[Test]
public void FuzzyDivisionTest()
{
double a = 0.33333d;
double b = 1d / 3d;
Assert.IsFalse(a == b, "Built-in == operator should not be
fuzzy");
Assert.IsTrue( a-b < double.Epsilon);
Assert.IsTrue( Math.Abs(a-b) < double.Epsilon);
Assert.IsTrue( Math.Abs(b-a) < double.Epsilon);
}
However, something is weird. The first 2 assertions pass, however the
last 2 fail. Is Math.Abs() losing my Epsilon value? Is that correct?
推荐答案
Daniel< dh ***** @ yahoo.com>写道:
Daniel <dh*****@yahoo.com> wrote:
我正在努力解决double.Epsilon的想法。我已经写了
以下测试:
[测试]
public void FuzzyDivisionTest()
{
双a = 0.33333d;
double b = 1d / 3d;
Assert.IsFalse(a == b,内置==运算符不应该模糊);
Assert.IsTrue (ab< double.Epsilon);
Assert.IsTrue(Math.Abs(ab)< double.Epsilon);
Assert.IsTrue(Math.Abs(ba)< double.Epsilon) ;
}
然而,有些事情很奇怪。前2个断言通过,但最后2个失败。 Math.Abs()是否会失去我的Epsilon值?这是正确的吗?
I am grappling with the idea of double.Epsilon. I have written the
following test:
[Test]
public void FuzzyDivisionTest()
{
double a = 0.33333d;
double b = 1d / 3d;
Assert.IsFalse(a == b, "Built-in == operator should not be
fuzzy");
Assert.IsTrue( a-b < double.Epsilon);
Assert.IsTrue( Math.Abs(a-b) < double.Epsilon);
Assert.IsTrue( Math.Abs(b-a) < double.Epsilon);
}
However, something is weird. The first 2 assertions pass, however the
last 2 fail. Is Math.Abs() losing my Epsilon value? Is that correct?
Epsilon是双倍可以容纳的最小正非零值。
永远不会是真的(我*认为*)a!= b和
Math.Abs(ba)< double.Epsilon。
ab小于double.Epsilon因为它是负面的。
-
Jon Skeet - < sk *** @ pobox.com>
http ://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
Epsilon is the smallest positive non-zero value that a double can hold.
It will never be true (I *think*) that a != b and
Math.Abs(b-a) < double.Epsilon.
a-b is less than double.Epsilon because it''s negative.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
我对Visual Studio帮助的解释就是这个回复是
部分正确。有可能
a!= b
和
Math.Abs(b - a)< double.Epsilon
然而,你的例子中a和b之间的区别明显更大
比double.Epsilon - 它是3.3333333(经常性)e-10和double.Epsilon ==
4.94065645841247e-324
正如之前的回复所说,第二个断言通过的原因是
a - b是否定的。
" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道
新闻:MP ************************ @ msnews.microsoft.c om ...
My interpretation of the Visual Studio help on this is that this reply is
partially correct. It is possible that
a != b
and
Math.Abs(b - a) < double.Epsilon
However the difference between a and b in your example is evidently larger
than double.Epsilon - it is 3.3333333(recurring)e-10, and double.Epsilon ==
4.94065645841247e-324
As the previous reply said, the reason the second assertion passes is that
a - b is negative.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel< dh ***** @ yahoo.com>写道:
Daniel <dh*****@yahoo.com> wrote:
我正在努力解决double.Epsilon的想法。我已经写了
以下测试:
[测试]
public void FuzzyDivisionTest()
{
双a = 0.33333d;
double b = 1d / 3d;
Assert.IsFalse(a == b,内置==运算符不应该模糊);
Assert.IsTrue (ab< double.Epsilon);
Assert.IsTrue(Math.Abs(ab)< double.Epsilon);
Assert.IsTrue(Math.Abs(ba)< double.Epsilon) ;
}
然而,有些事情很奇怪。前2个断言通过,但最后2个失败。 Math.Abs()是否会失去我的Epsilon值?这是正确的吗?
I am grappling with the idea of double.Epsilon. I have written the
following test:
[Test]
public void FuzzyDivisionTest()
{
double a = 0.33333d;
double b = 1d / 3d;
Assert.IsFalse(a == b, "Built-in == operator should not be
fuzzy");
Assert.IsTrue( a-b < double.Epsilon);
Assert.IsTrue( Math.Abs(a-b) < double.Epsilon);
Assert.IsTrue( Math.Abs(b-a) < double.Epsilon);
}
However, something is weird. The first 2 assertions pass, however the
last 2 fail. Is Math.Abs() losing my Epsilon value? Is that correct?
Epsilon是双倍能够容纳的最小正非零值。
永远不会是真的(我*认为*)a!= b和
Math.Abs(ba)< double.Epsilon。
ab不到两倍.Epsilon因为它是负面的。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
Epsilon is the smallest positive non-zero value that a double can hold.
It will never be true (I *think*) that a != b and
Math.Abs(b-a) < double.Epsilon.
a-b is less than double.Epsilon because it''s negative.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
" Jon Skeet [C#MVP]" < SK *** @ pobox.com>写在
新闻:MP ************************ @ msnews.microsoft.c om ...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om...
Daniel< dh ***** @ yahoo.com>写道:
Daniel <dh*****@yahoo.com> wrote:
我正在努力解决double.Epsilon的想法。我已经写了
以下测试:
[测试]
public void FuzzyDivisionTest()
{
双a = 0.33333d;
double b = 1d / 3d;
Assert.IsFalse(a == b,内置==运算符不应该模糊);
Assert.IsTrue (ab< double.Epsilon);
Assert.IsTrue(Math.Abs(ab)< double.Epsilon);
Assert.IsTrue(Math.Abs(ba)< double.Epsilon) ;
}
然而,有些事情很奇怪。前2个断言通过,但最后2个失败。 Math.Abs()是否会失去我的Epsilon值?这是正确的吗?
I am grappling with the idea of double.Epsilon. I have written the
following test:
[Test]
public void FuzzyDivisionTest()
{
double a = 0.33333d;
double b = 1d / 3d;
Assert.IsFalse(a == b, "Built-in == operator should not be
fuzzy");
Assert.IsTrue( a-b < double.Epsilon);
Assert.IsTrue( Math.Abs(a-b) < double.Epsilon);
Assert.IsTrue( Math.Abs(b-a) < double.Epsilon);
}
However, something is weird. The first 2 assertions pass, however the
last 2 fail. Is Math.Abs() losing my Epsilon value? Is that correct?
Epsilon是双倍能够容纳的最小正非零值。
永远不会是真的(我*认为*)a!= b和
Math.Abs(ba)< double.Epsilon。
Epsilon is the smallest positive non-zero value that a double can hold.
It will never be true (I *think*) that a != b and
Math.Abs(b-a) < double.Epsilon.
是的,我认为关于Double.Epsilon的文档实际上是错误的;引用:
"而不是[将双值与==进行比较],通过比较确定
a比较的双方是否足够接近等于你的目的/>
左手和右手之间的差值的绝对值是否小于Epsilon。
我通常使用类似Math.Abs(ab)< 1e-10,但我不确定这是否是常见做法。
Niki
Yes, I think the docs about Double.Epsilon are actually wrong; Quote:
"Instead [of comparing double values with ==], determine if the two sides of
a comparison are close enough to equal for your purposes by comparing
whether the absolute value of the difference between the left and right-hand
sides is less than Epsilon."
I usually use something like "Math.Abs(a-b) < 1e-10", but I''m not sure if
this is common practice.
Niki
>
这篇关于Double.Epsilon的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!