32位IEEE浮点乘法 [英] 32-bit IEEE float multiplication

查看:100
本文介绍了32位IEEE浮点乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不知道这是否是发布此内容的正确组合,但是当我将一个巨大的浮点值乘以一个真正的

小(非零)浮点值,结果得到0(零)。

这就是我写的32位定时器程序中的一个大漏洞。


问题。

1.为什么会发生这种情况?

2.我可以打电话告诉C宏/函数吗?当两个非零数字相乘且

结果为零时,我是




TIA

Andy

Hi,
I don''t know if this is the correct group to post this, but
when I multiply a huge floating point value by a really
small (non-zero) floating point value, I get 0 (zero) for the result.
This creates a big hole in a 32-bit timer routine I wrote.

Questions.
1. Why does this happen?
2. Is there C macros/functions I can call to tell me
when two non-zero numbers are multiplied and the
result will be zero?

TIA
Andy

推荐答案



2003年12月3日星期三,Andy写道:

On Wed, 3 Dec 2003, Andy wrote:

我不知道这是否是发布此内容的正确组,但是当我将一个巨大的浮点值乘以一个非常小的(非零)浮点值时,我得到0(零)的结果。
这在我写的32位计时器程序中创造了一个大洞。

问题。
1.为什么会这样?


这是在FAQ中。一般来说,计算机不是数学。计算机,

即使是快速的计算机,也专门处理固定宽度的数据。那个

意味着你只能获得有限的精确度。

一遍又一遍地将X除以2,最终X将

变得如此之小以至于它与零无法区分。在一台

计算机上,这意味着它等于零。它被称为截断

或underflow。或溢出或舍入误差或舍入误差。 (取决于

到底发生了什么),这是每个数字的b $ b程序员应该理解的东西。

你的主题行表明你理解什么是比特。是;所以

这样考虑一下。你正在使用32位数字--32位

可以容纳2 ** 32个不同的值 - 所以如果你想要的数字是
计算不是'''这些2 ** 32个特定值中的一个可用32位数字代表

,那么你将得到舍入错误。如果你的
数字非常接近于零,那么它将*变为*零,只是因为

'是计算机可以得到的最近的代表。 br />
2.是否有C宏/函数我可以打电话告诉我
当两个非零数字相乘并且
结果为零时?

I don''t know if this is the correct group to post this, but
when I multiply a huge floating point value by a really
small (non-zero) floating point value, I get 0 (zero) for the result.
This creates a big hole in a 32-bit timer routine I wrote.

Questions.
1. Why does this happen?
It''s in the FAQ. In general, Computers Are Not Math. Computers,
even big fast ones, deal exclusively in fixed-width data. That
means you only get a finite amount of precision to work with.
Keep dividing X by 2 over and over again, and eventually X will
get so small that it''s indistinguishable from zero. And on a
computer, that means it''s *equal* to zero. It''s called "truncation"
or "underflow" or "overflow" or "rounding error" (depending on
exactly what''s going on), and it''s something that every numeric
programmer should understand.
Your subject line suggests you understand what a "bit" is; so
think about it this way. You''re using 32-bit numbers -- 32 bits
can hold 2**32 different values -- so if the number you want to
compute isn''t one of those 2**32 particular values representable
by your 32-bit numbers, then you''ll get rounding error. If your
number is very close to zero, then it''ll *become* zero, just because
that''s the nearest representation the computer can get.
2. Is there C macros/functions I can call to tell me
when two non-zero numbers are multiplied and the
result will be zero?




if(a!= 0&& b!= 0&& a * b == 0)

puts(" duh") ;


-Arthur



if (a != 0 && b != 0 && a*b == 0)
puts("duh");

-Arthur


文章< ae *********** ***************@posting.google.com>,
bi ** ***@hotmail.com (Andy)写道:
In article <ae**************************@posting.google.com >,
bi*****@hotmail.com (Andy) wrote:

我不知道这是否是发布此内容的正确组合,但是当我将一个巨大的浮点值乘以一个非常小的(非零)浮点值时,我得到的结果为0(零)。
这会造成一个大漏洞我写的一个32位计时器例程。

问tions。
1.为什么会发生这种情况?
2.是否有C宏/函数我可以打电话告诉我
当两个非零数字相乘并且
结果将是零?
Hi,
I don''t know if this is the correct group to post this, but
when I multiply a huge floating point value by a really
small (non-zero) floating point value, I get 0 (zero) for the result.
This creates a big hole in a 32-bit timer routine I wrote.

Questions.
1. Why does this happen?
2. Is there C macros/functions I can call to tell me
when two non-zero numbers are multiplied and the
result will be zero?




这是不寻常的。你可以发布使用哪个编译器,并发布源代码来证明这个问题的
代码吗?你的意思是


双结果= 1e300 * 1e-300;





This is unusual. Could you post which compiler is used, and post source
code that demonstrates the problem? You mean something like

double result = 1e300 * 1e-300;

?


Andy写道:
Andy wrote:

我不知道这是否是发布此内容的正确组合,但
当我繁殖时浮点值通过一个非常小的(非零)浮点值,我得到0(零)的结果。
这在我写的32位定时器例程中创建了一个大漏洞。问题。
1.为什么会发生这种情况?
2.是否有C宏/函数我可以打电话告诉我
当两个非零数字相乘时并且结果将为零?

TIA
Andy
Hi,
I don''t know if this is the correct group to post this, but
when I multiply a huge floating point value by a really
small (non-zero) floating point value, I get 0 (zero) for the result.
This creates a big hole in a 32-bit timer routine I wrote.

Questions.
1. Why does this happen?
2. Is there C macros/functions I can call to tell me
when two non-zero numbers are multiplied and the
result will be zero?

TIA
Andy




这不是真正的话题,但是我不知道它会在哪里,

所以让我们试一试。


假设32位IEEE浮点数(来自你的主题),

他们的产品应该是代表最接近的电子数字

到最近的可表示数字,可以四舍五入。引用IEEE 754-1985第4节的第一段




Rounding将数字视为无限精确,如果

必要,修改它以适应目的地的格式,而

表示不精确的异常(7.5)。除了二进制< --->十进制

转换(其中较弱的条件在5.6中指定),第5节中规定的每个

操作都应该像首先生成的那样执行

中间结果正确到无限精度并且

无界范围,然后根据本节中的一个

模式舍入结果。


(当然,乘法在第5节中规定)。


因此,如果您使用符合IEEE 754标准的工作浮点数,这个

可能就是这种情况。要解决这个问题,请显示两个数字相乘的确切(十六进制)

表示和结果(

结果是单精度或双精度数字)?


嗯,这将有助于了解你运行它的硬件或软件。


最后,你可以做的最小程序生产以显示问题

将有助于理解这一点。

祝你好运,


Sidney Cadot



This is not really on-topic here, but I wouldn''t know where it would be,
so let''s give it a go.

Assuming 32-bit IEEE floating point numbers (from your subject line),
their product should be the representation of the number that is closest
to the nearest representable number, subject to rounding. To quote the
first paragraph of Section 4 of IEEE 754-1985:

"Rounding takes a number regarded as infinitely precise and, if
necessary, modifies it to fit in the destination?s format while
signaling the inexact exception (7.5). Except for binary <---> decimal
conversion (whose weaker conditions are specified in 5.6), every
operation specified in Section 5 shall be performed as if it first
produced an intermediate result correct to infinite precision and with
unbounded range, and then rounded that result according to one of the
modes in this section. "

(and, of course, multiplication is specified in Section 5).

So if you are working using IEEE 754-compliant floating point, this
could be the case. To work this out, please show the exact (hex)
representation of the two numbers being multiplied and the result (is
the result a single- or double-precision number)?

Furher, it would help to know the hardware or software your running this on.

And lastly, the minimal program you can produce to show the problem
would be of assistance in understanding this.
Best regards,

Sidney Cadot


这篇关于32位IEEE浮点乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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