浮点比较的要求 [英] Requirements of floating point comparisons

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

问题描述

我在模仿浮点数时可以依赖的确切模糊

比较。我知道计算值可能不同,例如我不是

保证1. + 2. == 3.


如何与文字进行比较?


float f = 3 .;

int v = f == 3 .;


保证是一个?那些涉及转换的情况怎么样?浮点数和双打数之间的价格是多少?


int v = 3.f == 3 .;


从字符串计算的值怎么样?


int v = strtod(" 3.0",NULL)== 3.。


我应该知道的任何其他情况吗?


感谢您的任何建议,

-Peter


-

拉出一个分裂来回复。

解决方案

" Peter Ammon" < GE ****** @ splintermac.com>在留言中写道

新闻:J6 ***************** @ newssvr25.news.prodigy.co m ...

在做浮点比较时,我对我可以依赖的确切模糊。我知道计算值可能不同,例如我不能保证1. + 2. == 3.


对。虽然在这种情况下,你可能*会*得到一个匹配。

如何与文字进行比较?


没有区别。为什么你认为应该有?

浮点数f = 3.;
int v = f == 3 .;

保证是一个?


在这种特殊情况下,非常有可能。一般情况下,没有。

浮动和双打之间涉及转换的情况怎么样?


同样的事情。

int v = 3.f == 3。;


这是一个错字吗?

从字符串计算的值怎么样?

int v = strtod(" 3.0" ;,NULL)== 3。;


紫红色不同的颜色相同。

我应该知道的任何其他情况?




在下列情况下,您可能会发现a和b可能无法比较:


浮动a = 1.0 / 10.0,b = 0.1;


Peter


Peter Pichler写道:


Peter Ammon < GE ****** @ splintermac.com>在消息中写道
新闻:J6 ***************** @ newssvr25.news.prodigy.co m ...

我'' m模糊在做浮点数比较时我可以依赖的东西。我知道计算值可能不同,例如我不能保证1. + 2. == 3.



对。虽然在这种情况下,你可能会*得到一个匹配。

与文字比较怎么样?



没有区别。为什么你认为应该有?

float f = 3 .;
int v = f == 3 .;

是v保证是一个吗?



在这种特殊情况下,非常有可能。一般来说,没有。

那些涉及花车和双打之间转换的情况怎么样?



同样的事情。

int v = 3.f == 3.。



这是一个错字吗?

怎么样对于从字符串计算的值?

int v = strtod(" 3.0",NULL)== 3。;



同样在不同的阴影下紫红色。

我应该知道的任何其他情况?



您可能会发现a和b在以下情况下可能无法比较:

浮动a = 1.0 / 10.0,b = 0.1;




但是,OP似乎对任务操作更感兴趣

而不是算术运算。


我会假设

double f = 0.1;



(f == 0.1)

为真,

无论0.1是否为r完全呈现为双倍。

我的理由是,即使0.1不能完全表示,

然后无论它的代表是什么,

对于常量和对象都是相同的。


-

pete

在文章< 40 *********** @ mindspring.com> pf*****@mindspring.com 写道:

....

我认为对于
double f = 0.1;

(f == 0.1)
是真的,
无论0.1是否可以完全表示为双。
我的理由是,即使0.1不能完全表示,
然后无论它的表示是什么,
对于两者都是相同的常数和对象。




也许吧。常数0.1可以保持扩展精度。

-

dik t。冬天,cwi,kruislaan 413,1098 sj amsterdam,nederland,+ 31205924131

home:bovenover 215,1025 jn amsterdam,nederland; http://www.cwi.nl/~dik/


I''m fuzzy on exactly what I can depend on when doing floating point
comparisons. I know that calculated values can differ, e.g. I''m not
guaranteed that 1. + 2. == 3.

What about comparisons with literals?

float f=3.;
int v = f==3.;

is v guaranteed to be one? What about situations involving conversions
between floats and doubles?

int v = 3.f == 3.;

How about for values calculated from strings?

int v = strtod("3.0", NULL)==3.;

Any other situations I should know about?

Thanks for any advice,
-Peter

--
Pull out a splinter to reply.

解决方案

"Peter Ammon" <ge******@splintermac.com> wrote in message
news:J6*****************@newssvr25.news.prodigy.co m...

I''m fuzzy on exactly what I can depend on when doing floating point
comparisons. I know that calculated values can differ, e.g. I''m not
guaranteed that 1. + 2. == 3.
Right. Although in this case, you probably *will* get a match.
What about comparisons with literals?
No difference. Why do you think there should be any?
float f=3.;
int v = f==3.;

is v guaranteed to be one?
In this particular case, very likely. In general, no.
What about situations involving conversions
between floats and doubles?
Same thing.
int v = 3.f == 3.;
Is this a typo?
How about for values calculated from strings?

int v = strtod("3.0", NULL)==3.;
The same in a different shade of mauve.
Any other situations I should know about?



You may find that a and b may not compare equal in the following case:

float a = 1.0/10.0, b = 0.1;

Peter


Peter Pichler wrote:


"Peter Ammon" <ge******@splintermac.com> wrote in message
news:J6*****************@newssvr25.news.prodigy.co m...

I''m fuzzy on exactly what I can depend on when doing floating point
comparisons. I know that calculated values can differ, e.g. I''m not
guaranteed that 1. + 2. == 3.



Right. Although in this case, you probably *will* get a match.

What about comparisons with literals?



No difference. Why do you think there should be any?

float f=3.;
int v = f==3.;

is v guaranteed to be one?



In this particular case, very likely. In general, no.

What about situations involving conversions
between floats and doubles?



Same thing.

int v = 3.f == 3.;



Is this a typo?

How about for values calculated from strings?

int v = strtod("3.0", NULL)==3.;



The same in a different shade of mauve.

Any other situations I should know about?



You may find that a and b may not compare equal in the following case:

float a = 1.0/10.0, b = 0.1;



But, the OP seemed more interested in assignment operations
rather than arithmetic operations.

I would assume that for
double f = 0.1;
that
(f == 0.1)
was true,
regardless of whether 0.1 could be represented exactly as a double.
My reasoning is that even if 0.1 can''t be represented exactly,
then whatever it''s representation is,
will be the same for both the constant and the object.

--
pete


In article <40***********@mindspring.com> pf*****@mindspring.com writes:
....

I would assume that for
double f = 0.1;
that
(f == 0.1)
was true,
regardless of whether 0.1 could be represented exactly as a double.
My reasoning is that even if 0.1 can''t be represented exactly,
then whatever it''s representation is,
will be the same for both the constant and the object.



Perhaps. The constant 0.1 can be kept in extended precision.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/


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

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