浮点运算。 [英] Floating point arithmetic.

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

问题描述

你好。

我也在comp.lang.c上发布这个:对不起。

我面临的问题如下:

例如:

double a = 0.15;

double b = 2.4;

const double VERYTINY = 1。 e-10;

我知道b / a = 16因此余数为零;但我不是

能够在c中找到任何合适的东西来编码。
例如
(fmod(b,a)> VERYTINY)返回true! br />
现在对于这个特定的实例,(fmodf(b,a)> VERYTINY)

确实返回false。

但现在如果

a = 0.15;

b = 4.5;

然后fmodf和fmod都不帮助......


对此有什么建议吗?

我指的是关于浮点运算的参考,他们在那里谈论
的ulps等,但有一个小函数或修复处理这个问题

可以在哪里找到?


谢谢,

amit。

Hi there.
I am cross posting this on comp.lang.c as well: sorry for same.
The problem I am facing is as follows:
For example:
double a= 0.15;
double b=2.4;
const double VERYTINY =1.e-10;
I know b/a = 16 and hence the remainder is zero; but I am not
able to find any suitable thing to encode it into in c.
for example (fmod(b,a)>VERYTINY) returns true!
Now for this particular instance, (fmodf(b,a)>VERYTINY)
does return false.
But now if
a=0.15;
b=4.5;
then fmodf and fmod both don''t help...

any suggestions on this?
I was pointed to a reference on floating point arithmetic, where they talk
of ulps etc, but is there a small function or fix to deal with this problem
available somewhere?

thanks,
amit.

推荐答案



" Amit Bhatia" < BH ***** @ nospam.com>写道:

"Amit Bhatia" <bh*****@nospam.com> wrote:
我面临的问题如下:
例如:


什么样的例子?在给出

的例子之前,说明你要举例说明的事情更有意义。

double a = 0.15 ;
双b = 2.4;
const double VERYTINY = 1.e-10;
我知道b / a = 16因此余数为零;
但我不是能够在c中找到任何合适的东西来编码。


定义编码。


定义它。


如果你想在C中用b / a编码,代码是:


b / a

例如


示例什么?请给出一些线索

你试图给出一个例子,然后给出

例子!

(fmod( b,a)> VERYTINY)返回true!


它在我的系统上返回false。但是,在我看来,

你将double类型的粒度推到了最大值。

即使你每1e10减去一个部分,那又怎么样?

这是每百亿的一部分。谁在乎呢?


但是如果你真的在乎,请尝试获取一个编译器,

支持类型long double。或者,或许,使用计算机

与64位处理器和编译器定义双
为64位。即使这些位中有4个左右用于符号和指数,你仍然可以使用每个元素(1e18)大约1个部分的b / b
。比你需要的更精确。

现在对于这个特定的例子,(fmodf(b,a)> VERYTINY)
会返回false 。


什么是fmodf?这不是C或C ++ std。

库的一部分。我也没有在我的编译器(DJGPP)上拥有它。

但是现在如果
a = 0.15;
b = 4.5;
那么fmodf和fmod两个都没有帮助...


我得到b / a = 30和(fmod(b,a)> VERYTINY)= false。

你得到什么?

对此有何建议?


关于什么的建议,确切地说?

我指的是关于浮点算术的参考,他们谈到了ulps等,


什么是'ulps"?

是否有一个小功能或修复来处理这个问题
可以在某个地方使用?
The problem I am facing is as follows:
For example:
Example of what? It makes more sense to state the
thing you want to give an example of, before giving
examples of it.
double a= 0.15;
double b=2.4;
const double VERYTINY =1.e-10;
I know b/a = 16 and hence the remainder is zero;
but I am not able to find any suitable thing to
encode it into in c.
Define "encode".

Define "it".

If you want to code b/a in C, the code is:

b/a
for example
Example of what? Please give some clue as to the thing
you''re trying to give an example OF, before giving
examples!
(fmod(b,a)>VERYTINY) returns true!
It returns false on my system. However, it seems to me
you''re pushing the granularity of type double to the max.
Even if you WERE off by one part per 1e10, so what?
That''s one part per ten billion. Who cares?

But if you really DO care, try acquiring a compiler that
supports type "long double". Or, perhaps, use a computer
with a 64-bit processor and compiler that defines "double"
to be 64 bits. Even if 4 or so of those bits were used
for sign and exponent, you''d still have a granularity of
about 1 part per quintillion (1e18). More precision than
you''ll EVER need.
Now for this particular instance, (fmodf(b,a)>VERYTINY)
does return false.
What is fmodf? That''s not a part of the C or C++ std.
libraries. I don''t have it on my compiler (DJGPP) either.
But now if
a=0.15;
b=4.5;
then fmodf and fmod both don''t help...
I get b/a = 30 and (fmod(b,a)>VERYTINY) = false.

What do YOU get?
any suggestions on this?
Suggestions on what, precisely?
I was pointed to a reference on floating point
arithmetic, where they talk of ulps etc,
What''s "ulps"?
is there a small function or fix to deal with this problem
available somewhere?




你想解决什么问题?


-

干杯,

Robbie Hatley

美国加利福尼亚州塔斯廷
lo **** *****@pacbell.net
http:/ /home.pacbell.net/earnur/



What problem are you trying to fix?

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lo**********@pacbell.net
http://home.pacbell.net/earnur/


On Sun,2004年7月11日01:17:09 -0700,Robbie Hatley<来自pacbell的lonewolfintj

dot net>写道:
On Sun, 11 Jul 2004 01:17:09 -0700, Robbie Hatley <lonewolfintj at pacbell
dot net> wrote:
我指的是关于浮点算术的参考,他们谈论了ulps等,
I was pointed to a reference on floating point
arithmetic, where they talk of ulps etc,



什么是ulps?



What''s "ulps"?




ULP代表最后一个单位。它是一种舍入误差的度量和

a浮点运算中常用的术语。


john



ULP stands for unit in the last place. Its a measure of rounding error and
a common enough term in floating point arithmetic.

john


2004年7月10日星期六23:08:01 -0500,Amit Bhatia< bh ***** @ nospam.com>写道:
On Sat, 10 Jul 2004 23:08:01 -0500, Amit Bhatia <bh*****@nospam.com> wrote:
你好。
我也在comp.lang.c上发布这个:对不起。
我面临的问题是如下:
例如:
double a = 0.15;
double b = 2.4;
const double VERYTINY = 1.e-10;
我知道b / a = 16,因此余数为零;但是我不能找到任何合适的东西来编码成c。
例如(fmod(b,a)> VERYTINY)返回true!
现在对于这个特定的实例,(fmodf(b,a)> VERYTINY)
确实返回false。
但是现在如果
a = 0.15;
b = 4.5;
那么fmodf和fmod都不帮忙......

有什么建议吗?


也许为VERYTINY使用更大的值。

也许使用积分或理性的aritmetic。

也许使用定点算术。

我指的是关于浮点运算的参考,在那里他们谈论ulps等,但是有一个小函数或修复来处理这个问题
可以在哪里使用?
Hi there.
I am cross posting this on comp.lang.c as well: sorry for same.
The problem I am facing is as follows:
For example:
double a= 0.15;
double b=2.4;
const double VERYTINY =1.e-10;
I know b/a = 16 and hence the remainder is zero; but I am not
able to find any suitable thing to encode it into in c.
for example (fmod(b,a)>VERYTINY) returns true!
Now for this particular instance, (fmodf(b,a)>VERYTINY)
does return false.
But now if
a=0.15;
b=4.5;
then fmodf and fmod both don''t help...

any suggestions on this?
Maybe use a bigger value for VERYTINY.
Maybe use integral or rational aritmetic.
Maybe use fixed point arithmetic.
I was pointed to a reference on floating point arithmetic, where they
talk
of ulps etc, but is there a small function or fix to deal with this
problem
available somewhere?




浮点舍入错误的问题并不容易。

最简单的事情不是编写依赖于任何特定的浮点运算精度的代码。显然这并不总是可能的,但有可能比人们想象的更频繁。


所以问题的真正取决于你的问题

实际上是试图解决。


john



There is not easy answer to the problem of floating point rounding errors.
The simplest thing is not to write code that depends on any particular
exactness of floating point arithmetic. Obviously this is not always
possible but it is possible more often than people think.

So the asnwer to your question really depends on what kind of problem you
are actually trying to solve.

john


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

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