多长时间 [英] how long is double

查看:60
本文介绍了多长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个


双倍金额,a,b,c;

sum = a + b + c;

printf("%。20f =%。20f,%。20f,%。20f",sum,a,b,c);


我发现调试版本和发布相同代码的版本

给我不同的结果。我使用的是VC ++ 6.0。


在调试版中,打印输出为:

-12.25938471938358500000 = -11.43596388399630500000,

- 0.07591666113607631300,-0.74750417425120252000,

在发布版本中,打印输出为:

-12.25938471938358300000 = -11.43596388399630500000,

- 0.07591666113607631300,-0.74750417425120252000,

上述sum = a + b + c只是我计算的一部分。我发现

我的整个计算在调试版本中被粉碎了,因为有些

数字变为零而另一个数字除以这个数字。但这确实发生了

发布版本没有发生。


为什么?


谢谢,


ff


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

解决方案

" f" < FF **** @ yahoo.com>在消息中写道

新闻:8f ************************* @ posting.google.co m ... < blockquote class =post_quotes>我有这个

双和,a,b,c;
sum = a + b + c;


您使用了a,b和c而没有初始化它们。这是你的意图吗?

printf("%。20f =%。20f,%。20f,%。20f",sum,a,b,c);
我发现相同代码的调试版本和发布版本给了我不同的结果。我使用的是VC ++ 6.0。

在调试版中,打印出来的是:
-12.25938471938358500000 = -11.43596388399630500000,
-0.07591666113607631300,-0.74750417425120252000,
在发布版本中,打印输出为:
-12.25938471938358300000 = -11.43596388399630500000,-0.07591666113607631300,-0.74750417425120252000,


看起来和我一样。那个尾随的逗号来自哪里?

上面的sum = a + b + c只是我计算的一部分。我发现我的整个计算都在调试版中被粉碎了,因为一些
数字变为零而另一个数字除以这个数字。但这确实没有在发布版本中发生。


也许你可以哄骗Great Carsoni退休。或发布一些代码。

为什么?

谢谢,

ff

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。审核。第一次海报:做到这一点! ]




你的大量交叉标记是非常糟糕的网络风格。


-

Cy
http://home.rochester.rr.com/cyhome/




" f" < FF **** @ yahoo.com>在消息中写道

新闻:8f ************************* @ posting.google.co m ... < blockquote class =post_quotes>我有这个

double sum,a,b,c;
sum = a + b + c;
printf("%。20f =%。20f,%。20f,%。20f",sum,a,b,c);

我发现调试版和同一代码的发布版本给了我不同的结果。我使用的是VC ++ 6.0。

在调试版中,打印出来的是:
-12.25938471938358500000 = -11.43596388399630500000,
-0.07591666113607631300,-0.74750417425120252000,
在发布版中,打印出来的是:
-12.25938471938358300000 = -11.43596388399630500000,-0.07591666113607631300,-0.74750417425120252000,

以上sum = a + b + c只是我计算的一部分。我发现我的整个计算都在调试版中被粉碎了,因为一些
数字变为零而另一个数字除以这个数字。但这确实没有在发布版本中发生。

为什么?




显然差异在-12.25938471938358500000之间
和-12.25938471938358300000,在第17个重要的

数字中差异为2。


我在猜 - 这只是猜测 - 在生产模式下,

编译器告诉机器使用其扩展精度

中间寄存器计算+ b + c(80位,如果我记得正确),并且在调试模式下

它实际上将中间结果截断为64位。那可能很好

给出了这个学位的差异。我必须看一下这些数字的二进制

表示,而且我现在太懒了:-) -

但是如果你的计算是如此脆弱,以至于这么小的偏差崩溃了,你可能想重新考虑它。

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]


2004年1月3日21:26:11 -0500, ff *** *@yahoo.com (f)写在

comp.lang.c ++:

我有这个
double sum,a,b,c;
sum = a + b + c;
printf("%。20f =%。20f,%。20f,%。20f",sum,a ,b,c);

我发现相同代码的调试版本和发行版本给了我不同的结果。我使用的是VC ++ 6.0。

在调试版中,打印出来的是:
-12.25938471938358500000 = -11.43596388399630500000,
-0.07591666113607631300,-0.74750417425120252000,
在发布版中,打印出来的是:
-12.25938471938358300000 = -11.43596388399630500000,-0.07591666113607631300,-0.74750417425120252000,

以上sum = a + b + c只是我计算的一部分。我发现我的整个计算都在调试版中被粉碎了,因为一些
数字变为零而另一个数字除以这个数字。但这确实没有在发布版本中发生。

为什么?

谢谢,

ff



如果您重视浮点计算的准确性,请不要在任何情况下使用

Visual C ++。当Microsoft从16位更改为32位操作系统和编译器时,为了提供兼容性,他们将浮点代码更改为


他们将提供Windows所有不同处理器的


NT,其中大部分从未发生过。


特别是,他们做了这两个更改:


1.在他们的16位编译器中,long double类型使用了英特尔浮点硬件的全部80%b $ b位扩展精度。在他们的b $ b 32位编译器中,long double与double相同,并使用浮点硬件的64位双b精度模式。在Visual C ++中根本没有使用更高精度模式构建

到硬件中的方式。


2。在大多数情况下,他们的数学代码设置浮点硬件

控制位以将精度限制为64位,而不是使用FPU内部通常使用的80

位格式。这意味着你完全在FPU内计算时会损失
的精度,而不是只有当你将值存储到RAM时



微软多年前做出的决定是,程序员并不值得信赖,他们自己决定用英特尔FPU提供的最高精度是否会更好,或者他们应该

牺牲兼容浮点的性能和准确性

导致其他没有人真正购买Windows NT的处理器。

他们为你做出了决定,并且取消了你对结果的精确度的控制。


如果你想要英特尔FPU的最高精度和准确度

能够提供,你必须放弃Visual C ++并切换到

另一个编译器,如Borland或GNU,它可以让你延长

的精度加倍并且不会截断浮点控制

位s。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C- faq / top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu .edu / ~a ... FAQ-acllc.html


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++ .moderated。第一次海报:做到这一点! ]


I have this

double sum, a, b, c;
sum = a + b + c;
printf("%.20f = %.20f, %.20f, %.20f", sum, a, b, c);

I found that the debug version and release version of the same code
give me different result. I am using VC++ 6.0.

In debug version, the print out is:
-12.25938471938358500000 = -11.43596388399630500000,
-0.07591666113607631300, -0.74750417425120252000,

In the release version, the print out is:
-12.25938471938358300000 = -11.43596388399630500000,
-0.07591666113607631300, -0.74750417425120252000,

The above sum = a + b + c is just a part of my computation. I found
that my whole computation crushed in the debug version because some
number became zero and another number divide this number. But this did
not happened in the release version.

Why?

Thanks,

ff

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

解决方案

"f" <ff****@yahoo.com> wrote in message
news:8f*************************@posting.google.co m...

I have this

double sum, a, b, c;
sum = a + b + c;
You used a, b, and c without initializing them. Was this your intent?
printf("%.20f = %.20f, %.20f, %.20f", sum, a, b, c);

I found that the debug version and release version of the same code
give me different result. I am using VC++ 6.0.

In debug version, the print out is:
-12.25938471938358500000 = -11.43596388399630500000,
-0.07591666113607631300, -0.74750417425120252000,

In the release version, the print out is:
-12.25938471938358300000 = -11.43596388399630500000,
-0.07591666113607631300, -0.74750417425120252000,
Looks the same to me. And where is that trailing comma coming from?

The above sum = a + b + c is just a part of my computation. I found
that my whole computation crushed in the debug version because some
number became zero and another number divide this number. But this did
not happened in the release version.
Perhaps you can coax The Great Carsoni out of retirement. Or post some code.

Why?

Thanks,

ff

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]



Your massive crossposting is very bad usenet manners.

--
Cy
http://home.rochester.rr.com/cyhome/



"f" <ff****@yahoo.com> wrote in message
news:8f*************************@posting.google.co m...

I have this

double sum, a, b, c;
sum = a + b + c;
printf("%.20f = %.20f, %.20f, %.20f", sum, a, b, c);

I found that the debug version and release version of the same code
give me different result. I am using VC++ 6.0.

In debug version, the print out is:
-12.25938471938358500000 = -11.43596388399630500000,
-0.07591666113607631300, -0.74750417425120252000,

In the release version, the print out is:
-12.25938471938358300000 = -11.43596388399630500000,
-0.07591666113607631300, -0.74750417425120252000,

The above sum = a + b + c is just a part of my computation. I found
that my whole computation crushed in the debug version because some
number became zero and another number divide this number. But this did
not happened in the release version.

Why?



Apparently the discrepancy is between -12.25938471938358500000
and -12.25938471938358300000, a difference of 2 in the 17th significant
digit.

I''m guessing--and this is only a guess--that in production mode, the
compiler tells the machine to compute a+b+c by using its extended-precision
intermediate register (80 bits, if I remember correctly), and in debug mode
it actually truncates the intermediate result to 64 bits. That might well
give a discrepancy of about this degree. I''d have to look at the binary
representations of the numbers to be sure, and I''m too lazy right now :-) --
but if your computation is so brittle that such a small deviation crashes
it, you may want to rethink it.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


On 3 Jan 2004 21:26:11 -0500, ff****@yahoo.com (f) wrote in
comp.lang.c++:

I have this

double sum, a, b, c;
sum = a + b + c;
printf("%.20f = %.20f, %.20f, %.20f", sum, a, b, c);

I found that the debug version and release version of the same code
give me different result. I am using VC++ 6.0.

In debug version, the print out is:
-12.25938471938358500000 = -11.43596388399630500000,
-0.07591666113607631300, -0.74750417425120252000,

In the release version, the print out is:
-12.25938471938358300000 = -11.43596388399630500000,
-0.07591666113607631300, -0.74750417425120252000,

The above sum = a + b + c is just a part of my computation. I found
that my whole computation crushed in the debug version because some
number became zero and another number divide this number. But this did
not happened in the release version.

Why?

Thanks,

ff



If you value accuracy in floating point calculations, do not use
Visual C++ under any circumstances. When Microsoft changed from
16-bit to 32-bit operating systems and compilers, they made changes to
their floating point code for the purpose of providing compatibility
on all the different processors for which they would provide Windows
NT, most of which never happened.

In particular, they made these two changes:

1. In their 16-bit compilers, the long double type used the full 80
bit extended precision of the Intel floating point hardware. In their
32-bit compilers, long double is the same as double and uses the 64
bit double precision mode of the floating point hardware. There is no
way in Visual C++ at all to utilize the higher precision mode build
into the hardware.

2. In most cases, their math code sets the floating point hardware
control bits to limit precision to 64 bits, instead of using the 80
bit format normally used internally by the FPU. That means that you
lose precision on calculations entirely inside the FPU, and not just
when you store values to RAM.

Microsoft made the decision years ago that programmers were not
trustworthy to decide for themselves whether they were better off with
the highest precision the Intel FPU can provide, or they should
sacrifice performance and accuracy for compatible floating point
results on other processors that nobody actually bought Windows NT on.
They made the decision for you, and took away your control over the
precision of your results.

If you want the maximum precision and accuracy that the Intel FPU is
capable of providing, you have to give up on Visual C++ and switch to
another compiler, such as Borland or GNU, that gives you extended
precision long double and doesn''t truncate the floating point control
bits.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于多长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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