为什么会double类型的变量有意想不到的结果呢? [英] Why would a variable of type double have an unexpected result?

查看:283
本文介绍了为什么会double类型的变量有意想不到的结果呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的完整性检查失败,因为一个双变量不包含预期的结果,这真是匪夷所思。

My sanity check fails because a double variable does not contain the expected result, it's really bizarre.

double a = 1117.54 + 8561.64 + 13197.37;
double b = 22876.55;
Console.WriteLine("{0} == {1}: {2}", a, b, a == b);

为我们提供了这样的输出:

Gives us this output:

22876.55 == 22876.55: False

进一步检查告诉我们,变量,实际上包含值22876.550000000003。

Further inspection shows us that variable a, in fact, contains the value 22876.550000000003.

这是可重复的在vb.net中也是如此。上午的我松潘县的?这是怎么回事?

This is reproducible in vb.net as well. Am I sane? What is going on?

推荐答案

浮点类型并不总是能够准确地重新presenting其确切的十进制值。这是不是一个已知的错误或设计,这取决于你的观点。无论哪种方式,这是一个结果的浮点型的内部重新presentation,以及错误的常见原因。

Floating point types are not always capable of accurately representing their exact decimal values. It's either a "known bug" or "by design", depending on your perspective. Either way, it's a consequence of the internal representation of floating point types, and a common source of bugs.

这个问题几乎是不可避免的,也是如此,短期写一个复杂的计算机代数系统,重新presents值的的象征的,而不是数字类型。打开Windows计算器,确定4平方根,再减去2从该值。你会得到令人难以置信的接近0一些无厘头的浮点数,但不是的完全的0。您的平方根计算的结果没有被存储为的完全的2,所以当你从它究竟是2减,你会得到一个意外的结果。意想不到的是,那是,除非你知道基地2个运算的肮脏的小秘密。

The problem is nearly unavoidable, too, short of writing a complex computer algebra system that represents values symbolically, rather than as numeric types. Open up Windows Calculator, determine the square root of 4, and then subtract 2 from that value. You'll get some nonsensical floating point number that is incredibly close to 0, but not exactly 0. The result of your square root computation wasn't stored as exactly 2, so when you subtract exactly 2 from it, you get an "unexpected" result. Unexpected, that is, unless you know the dirty little secret about base 2 arithmetic.

如果你好奇,还有你可能会去了解为什么是这种情况的更多信息几个地方。乔恩斯基特写道文章在.NET框架的背景下解释二进制浮点运算。如果你有时间,你也应该仔细阅读恰当地命名发布, 什么每台计算机科学家应该知道关于浮点运算

If you're curious, there are several places you might go to find out more information about why this is the case. Jon Skeet wrote an article explaining binary floating point operations in the context of the .NET Framework. If you have the time, you should also peruse the aptly-named publication, What Every Computer Scientist Should Know About Floating-Point Arithmetic.

但底线是,你不应该期望能够以一个浮点运算的结果比较浮点文字。在这种特殊情况下,你可以尝试使用十进制类型,而不是。这不是一个真正的解决方案(见其他答案对于那些吓人的数学概念,如epsilons),但结果往往更predictable,因为十进制键入是在准确最好重新presenting基座10数字(如那些在货币和金融计算中使用)。

But the bottom line is that you shouldn't expect to be able to compare the result of a floating point operation to a floating point literal. In this specific case, you might try using the decimal type, instead. It's not really a "solution" (see the other answers for those, scary mathematical concepts like epsilons), but the results are often more predictable, as the decimal type is better at accurately representing base 10 numbers (such as those used in currency and financial calculations).

这篇关于为什么会double类型的变量有意想不到的结果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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