C#双值精度是42bit? [英] C# double value precision is 42bit?

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

问题描述

通过我在WinXP / VS2005 / C#2.0上的测试,

我获得了42位的精度。这是用C#指定还是取决于什么?


double dLeft = 2183.23 - 695.37;

double dRight = 1487.86;

double dFraction = Math.Pow(2,-42);

if(Math.Abs​​(dLeft - dRight)== dFraction){

System.Console.WriteLine( 成功);

}

//输出:成功

With the test I''ve made on my WinXP/VS2005/C#2.0,
I get a precision of 42bit. Is this specified in C# or dependent on what?

double dLeft = 2183.23 - 695.37;
double dRight = 1487.86;
double dFraction = Math.Pow(2, -42);
if (Math.Abs(dLeft - dRight) == dFraction) {
System.Console.WriteLine("Success");
}
// Output: Success

推荐答案

开6月10日,11:50 * am,StefanG< Stef ... @ discussion.microsoft.com>

写道:
On Jun 10, 11:50*am, StefanG <Stef...@discussions.microsoft.com>
wrote:

With我在WinXP / VS2005 / C#2.0上做的测试,

我的精度为42bit。这是用C#指定还是取决于什么?


* * * double dLeft = 2183.23 - 695.37;

* * * double dRight = 1487.86;

* * * double dFraction = Math.Pow(2,-42);

* * * if(Math.Abs​​(dLeft - dRight)== * dFraction){

* * * * System.Console.WriteLine(" Success");

* * *}

* * * //输出:成功
With the test I''ve made on my WinXP/VS2005/C#2.0,
I get a precision of 42bit. Is this specified in C# or dependent on what?

* * * double dLeft = 2183.23 - 695.37;
* * * double dRight = 1487.86;
* * * double dFraction = Math.Pow(2, -42);
* * * if (Math.Abs(dLeft - dRight) == *dFraction) {
* * * * System.Console.WriteLine("Success");
* * * }
* * *// Output: Success



你想测试什么?

What are you trying to test?


" Jon Skeet [C#MVP]" < sk *** @ pobox.com在新闻中写道:e65dbd4f-cf6c-404e-
9f *************** @ f63g2000hsf.googlegroups.com
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in news:e65dbd4f-cf6c-404e-
9f***************@f63g2000hsf.googlegroups.com:

C#中的双打有一个52位的尾数和一个11位指数(和一个

符号位)。精确度取决于标准化。

参见 http://pobox.com/~skeet/csharp/floatingpoint.html 获取更多

信息。
Doubles in C# have a 52 bit mantissa and an 11 bit exponent (and a
sign bit). The exact precision of that depends on normalisation.
See http://pobox.com/~skeet/csharp/floatingpoint.html for more
information.



让我谷歌搜索,我在这里找到了另一个不错的文章:

http://www.extremeoptimization.com/r...tConceptsAndFo

rmats.aspx

Got me googling and I found another nice writeup here:

http://www.extremeoptimization.com/r...tConceptsAndFo
rmats.aspx


Jon Skeet [C#MVP]写道:
Jon Skeet [C# MVP] wrote:

6月10日下午4:50,StefanG< Stef。 .. @ discussion.microsoft.comwrote:
On Jun 10, 4:50 pm, StefanG <Stef...@discussions.microsoft.comwrote:

>我在WinXP / VS2005 / C#2.0上做了测试,
我获得42位的精度。这是用C#指定还是取决于什么?

double dLeft = 2183.23 - 695.37;
double dRight = 1487.86;
double dFraction = Math.Pow(2,-42) ;
if(Math.Abs​​(dLeft - dRight)== dFraction){
System.Console.WriteLine(" Success");
}
//输出:成功
>With the test I''ve made on my WinXP/VS2005/C#2.0,
I get a precision of 42bit. Is this specified in C# or dependent on what?

double dLeft = 2183.23 - 695.37;
double dRight = 1487.86;
double dFraction = Math.Pow(2, -42);
if (Math.Abs(dLeft - dRight) == dFraction) {
System.Console.WriteLine("Success");
}
// Output: Success



我实际上有点失望,两个

的数字并不相同。


I''m actually slightly disappointed that the two
numbers aren''t identical.



当你想到它时,如果它们总是

那么会更令人失望。当编译器执行

减法以及在运行时完成时,您希望结果相同:


double dLeft = 2183.23 - 695.37 ;

double dRight = 1487.86;


double dLeft2 = 2183.23;

dLeft2 - = 695.37;


你想让dLeft和dLeft2相等,而不是dLeft和dRight。


-

G?ran Andersson
_____
http://www.guffa.com


这篇关于C#双值精度是42bit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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