使用浮动时出现意外结果 [英] Unexpected results while working with floats
问题描述
我在C ++中运行了以下简单代码并得到了意想不到的结果:
float f = 139.4;
cout<< f;
输出:
139.399994;
if(f == 139.4)
cout<< 预期结果;
其他
cout<< 意外的结果;
输出:
意外的结果
我在VB中运行了以下等效代码,但是得到了正确的结果结果。
Dim f单身
f = 139.4
打印f
如果f = 139.4那么
打印预期结果
否则
打印意外结果
结束如果
这对C ++的简历看起来不错吗?
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;
Output:
139.399994;
if( f == 139.4)
cout << "Expected result";
else
cout << "Unexpected result";
Output:
Unexpected reult
I ran the following equivalent code in VB but got the correct results.
Dim f As Single
f = 139.4
Print f
If f = 139.4 Then
Print "Expected result"
Else
Print "Unexpected result"
End If
Doesn''t this look bad on C++''s resume?
推荐答案
5月12日星期一2008 08:09:47 -0700,bintom写道:
On Mon, 12 May 2008 08:09:47 -0700, bintom wrote:
我在C ++中运行了以下简单代码并得到了意想不到的结果:
float f = 139.4;
cout<< f;
输出:
139.399994;
if(f == 139.4)
cout<< 预期结果;
其他
cout<< 意外结果;
输出:
意外reult
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;
Output:
139.399994;
if( f == 139.4)
cout << "Expected result";
else
cout << "Unexpected result";
Output:
Unexpected reult
[... ]
[...]
这对C ++的简历看起来不错吗?
Doesn''t this look bad on C++''s resume?
不,它看起来不错*你的*简历;-)
http://www.parashift.com/c++-faq-lit...html#faq-29.16
-
Lionel B
No, it looks bad on *your* resume ;-)
http://www.parashift.com/c++-faq-lit...html#faq-29.16
--
Lionel B
5月12日,8:23 * pm,Lionel B< m ... @ privacy.netwrote:
On May 12, 8:23*pm, Lionel B <m...@privacy.netwrote:
On Mon,2008年5月12日08:09:47 -0700,bintom写道:
On Mon, 12 May 2008 08:09:47 -0700, bintom wrote:
我在C ++中运行了以下简单代码并获得了意想不到的结果:
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout<< F;
float f = 139.4;
cout << f;
输出:
139.399994;
Output:
139.399994;
if(f == 139.4)
* cout<< 预期结果;
其他
* cout<< 意外结果;
if( f == 139.4)
* cout << "Expected result";
else
* cout << "Unexpected result";
输出:
意外的reult
Output:
Unexpected reult
[。 ..]
[...]
这对C ++的简历看起来不错吗?
Doesn''t this look bad on C++''s resume?
不,它看起来不错*你的*简历;-)
http://www.parashift.com/c++-faq-lit...html#faq-29.16
-
Lionel B-隐藏引用文字 -
- 显示引用文字 -
No, it looks bad on *your* resume ;-)
http://www.parashift.com/c++-faq-lit...html#faq-29.16
--
Lionel B- Hide quoted text -
- Show quoted text -
感谢Lionel指导我的链接,但我的问题仍然是
未答复。如果它是在
网站上说的计算机科学问题,VB应该产生类似(不准确)的结果。我只是
试图为我的简历辩护。
Thanks Lionel for directing me to the link, but my question remains
unanswered. Should it be a computer science issue as it says on the
site, VB should''ve produced a similar (inaccurate) result. I''m just
trying to defend my resume.
2008年5月12日星期一08:59:36 -0700,bintom写道:
On Mon, 12 May 2008 08:59:36 -0700, bintom wrote:
5月12日,8:23?* pm,Lionel B< m ... @ privacy.netwrote:
On May 12, 8:23?*pm, Lionel B <m...@privacy.netwrote:
> On Mon,2008年5月12日08:09:47 -0700,bintom写道:
>On Mon, 12 May 2008 08:09:47 -0700, bintom wrote:
我在C ++中运行以下简单代码并获得意想不到的结果:
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout<< F;
float f = 139.4;
cout << f;
输出:
139.399994;
Output:
139.399994;
if(f == 139.4)
?* cout<< 预期结果;
其他
?* cout<< 意外结果;
if( f == 139.4)
?* cout << "Expected result";
else
?* cout << "Unexpected result";
输出:
意外的reult
Output:
Unexpected reult
[...]
[...]
这对C ++的简历看起来不错吗?
Doesn''t this look bad on C++''s resume?
不,它看起来不错*你的*简历;-)
http://www.parashift.com/c++-faq-lit...html#faq-29.16
No, it looks bad on *your* resume ;-)
http://www.parashift.com/c++-faq-lit...html#faq-29.16
感谢Lionel指导我的链接,但我的问题仍然是
无人接听。如果它是在
网站上说的计算机科学问题,VB应该产生类似(不准确)的结果。我只是想要捍卫我的简历。
Thanks Lionel for directing me to the link, but my question remains
unanswered. Should it be a computer science issue as it says on the
site, VB should''ve produced a similar (inaccurate) result. I''m just
trying to defend my resume.
碰巧,在我的系统上,你的C ++代码产生正确的代码。结果。
但这是无关紧要的 - 正如常见问题所述:浮点数是近似的b
。这是不可避免的。如果你有兴趣进一步追求
问题,请看一下经典文章:每台计算机应该是什么?b $ b科学家应该知道浮点运算
http:// docs。 sun.com/source/806-3568/ncg_goldberg.html
-
Lionel B
As it happens, on my system your C++ code produces the "correct" result.
But that''s irrelevant - as the FAQ says: floating point is an
approximation. This is inescapable. If you''re interested in pursuing the
issue further, have a look at the classic article: "What Every Computer
Scientist Should Know About Floating-Point Arithmetic"
http://docs.sun.com/source/806-3568/ncg_goldberg.html
--
Lionel B
这篇关于使用浮动时出现意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!