.1 + .2 = 0.30000000000000004 [英] .1 + .2 = 0.30000000000000004

查看:49
本文介绍了.1 + .2 = 0.30000000000000004的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,mOnePlusTwo的计算结果为0.30000000000000004(尽管它的b $ b显示为.3)。因此,将该值与mThree(.3)进行比较会导致

为假。


发生了什么?还有什么我应该做的(除了使用小数类型切换到

)?


double mOne = .1;

double mTwo = .2;

double mThree = .3;

double mOnePlusTwo = mOne + mTwo;


bool mEqual = false;

if(mOnePlusTwo == mThree)

{

mEqual = true;

}


Console.WriteLine(" mOne =" + mOne);

Console.WriteLine(" mTwo =" + mTwo);

Console.WriteLine(" mThree =" + mThree);

Console.WriteLine(" mOnePlusTwo =" + mOnePlusTwo);

控制台。 WriteLine(" mThree == mOnePlusTwo?=" + mEqual);

In the below code, mOnePlusTwo evaluates to 0.30000000000000004 (although it
displays as ".3"). Consequently, comparing that value to mThree (.3) results
in false.

What''s going on? Is there something else I should do (other than switch to
using decimal types)?

double mOne = .1;
double mTwo = .2;
double mThree = .3;
double mOnePlusTwo = mOne + mTwo;

bool mEqual = false;
if (mOnePlusTwo == mThree)
{
mEqual = true;
}

Console.WriteLine("mOne = " + mOne);
Console.WriteLine("mTwo = " + mTwo);
Console.WriteLine("mThree = " + mThree);
Console.WriteLine("mOnePlusTwo = " + mOnePlusTwo);
Console.WriteLine("mThree == mOnePlusTwo? = " + mEqual);

推荐答案

Craig写道:
在在代码下面,mOnePlusTwo的计算结果为0.30000000000000004
(尽管它显示为.3)。因此,将该值与mThree(.3)进行比较会导致错误。

发生了什么?还有什么我应该做的(除了
切换到使用小数类型)?
In the below code, mOnePlusTwo evaluates to 0.30000000000000004
(although it displays as ".3"). Consequently, comparing that value
to mThree (.3) results in false.

What''s going on? Is there something else I should do (other than
switch to using decimal types)?




切换到小数并阅读这篇文章:
<每个计算机科学家应该知道的关于浮点运算的内容
http://docs.sun.com/source/806-3568/ncg_goldberg.html


-

Reginald Blue

我一直希望我的电脑和我的电话一样容易使用。我的愿望成真了。我不再知道如何使用我的

电话。

- Bjarne Stroustrup(C ++的创始人)[引自2003年

国际智能用户界面会议]



Switch to decimal and read this article:

What Every Computer Scientist Should Know About Floating-Point Arithmetic
http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]


Double或float数字的精度始终高达某些数字。在

中,有一个舍入错误。你永远不应该比较

等于双打。比较双打:

double epsilon = .0000000001; //你最喜欢的精度可以加倍。

if(Math.Abs​​(mOnePlusTwo - mThree)< epsilon)

true part

else

false part

Double or float number always has a precision up to certain digits. In
other word, there is a rounding error. You should never compare to
doubles for equal. To compare doubles:
double epsilon = .0000000001; // a precision you like up to double
can have.
if (Math.Abs(mOnePlusTwo - mThree) < epsilon)
true part
else
false part


这是完全正确的。


在基数10中,有理数1除以有理数3产生a

重复小数值0.333333333333333。表示不准确



在基数2中,相同的数字(1/3)不是重复的基数值。准确表示




在基数2中,有理数1除以有理数10(1/10)

表示重复的基数值。它没有准确表示。


由于0.1无法用二进制精确表示,我们不会尝试。我们使用

十进制表示法,它是数字的表示,而不是数字。


如果要进行统计计算,请使用double。对于货币或

值,其中1/10的倍数很重要,请使用小数。


-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:本论坛中发表的意见均为我自己的意见,而不是

代表我的雇主。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-

" Craig" <铬*** @ discussions.microsoft.com>在消息中写道

news:19 ********************************** @ microsof t.com ...
This is entirely correct.

In base 10, the rational number 1 divided by the rational number 3 yeilds a
repeating decimal value 0.333333333333333. It is not accurately
represented.
In base 2, the same number (1/3) is not a repeating radix value. It is
accurately represented.

In base 2, the rational number 1 divided by the rational number 10 (1/10)
yeilds a repeating radix value. It is not accurately represented.

Since 0.1 cannot be accurately represented in binary, we don''t try. We use
decimal notation which is a representation of the digits, not the number.

If you are making statistical calcuations, use double. For currency or
values where multiples of 1/10 are important, use decimal.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--
"Craig" <Cr***@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
在下面的代码中,mOnePlusTwo的计算结果为0.30000000000000004(尽管
它显示为.3)。因此,将该值与mThree(.3)
进行比较结果是错误的。

发生了什么?还有什么我应该做的(除了使用十进制类型切换
)?

double mOne = .1;
double mTwo = .2;
double mThree = .3;
double mOnePlusTwo = mOne + mTwo;

bool mEqual = false;
if(mOnePlusTwo == mThree)
{
mEqual = true;


Console.WriteLine(" mOne =" + mOne);
Console.WriteLine(" mTwo =" + mTwo );;
Console.WriteLine(" mThree =" + mThree);
Console.WriteLine(" mOnePlusTwo =" + mOnePlusTwo);
Console.WriteLine(" mThree = = mOnePlusTwo?=" + mEqual);
In the below code, mOnePlusTwo evaluates to 0.30000000000000004 (although
it
displays as ".3"). Consequently, comparing that value to mThree (.3)
results
in false.

What''s going on? Is there something else I should do (other than switch
to
using decimal types)?

double mOne = .1;
double mTwo = .2;
double mThree = .3;
double mOnePlusTwo = mOne + mTwo;

bool mEqual = false;
if (mOnePlusTwo == mThree)
{
mEqual = true;
}

Console.WriteLine("mOne = " + mOne);
Console.WriteLine("mTwo = " + mTwo);
Console.WriteLine("mThree = " + mThree);
Console.WriteLine("mOnePlusTwo = " + mOnePlusTwo);
Console.WriteLine("mThree == mOnePlusTwo? = " + mEqual);



这篇关于.1 + .2 = 0.30000000000000004的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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