Float比较:如何正确地完成它们 [英] Float comparisons: how to do them properly

查看:65
本文介绍了Float比较:如何正确地完成它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


浮点数比较我有问题。是的,我已阅读FAQ点

14.4ff。


假设这个小代码片段:


#include < stdio.h>


int main(int argc,char * argv []){

double a = 3;

double b = 5;


if((a / b)> 0.6)

printf("> \ n");

else

printf("< = \\\
");


返回EXIT_SUCCESS;

}


正如您可能猜到的,它有时会返回>,有时会返回< =。


现在对我来说非常重要的是,这个程序真的非常可靠地重复了。阈值可能是任意的,但我想要不惜一切代价避免
取决于我使用的编译器

选项,例如。


在我的现实世界中程序,a和b是整数,所以如果((a * 10)>(b * 6)我可以通过


来解决

问题)


但我的问题是:如果它_were_ double,那我该怎么办?如何获得一个完全确定的声明,独立于架构,

编译器和东西?


问候,


一月


-

Fire-fox你的konqueror!

解决方案

在文章< cc ********** @ sagnix.uni-muenster.de>,

< NO ****** ******@uni-muenster.de>写道:

你好,

浮点数比较我有问题。是的,我已阅读FAQ点
14.4ff。

假设这个小代码片段:

#include< stdio.h>

int main(int argc,char * argv []){
double a = 3;
double b = 5;

if((a / b)> 0.6)
printf("> \ n");

printf("< = \ n");

返回EXIT_SUCCESS ;
}

正如您可能猜到的,它有时会返回>,有时会返回< =。

现在它是对我来说非常重要的是这个程序真的,真的可重复的方式。阈值可能是任意的,但我想要不惜一切代价避免的是不同的结果,这取决于我使用的编译器选项,例如。

在我的真实中世界"程序,a和b都是整数,所以我可以通过做(

如果((a * 10)>(b * 6))来解决问题

>但我的问题是:如果它是双倍的话,我该怎么做呢?如何获得一个完全确定的声明,独立于架构,编译器和东西?




从我所看到的,你正试图对

a浮点数进行等式检查。这是一个根本上被打破的想法。


您可能想要编写一个比较两个浮点数的函数

并返回-1,0,1为案件

A绝对少于B

A大约等于B

A绝对大于B

其中近似等于如果A和B在彼此的2或3个ULP之内(您决定阈值),则为真。然后每当你想比较两个浮点数时,你就调用这个函数并检查它的返回结果

结果。


< blockquote> John Cochran< jd *@smof.fiawol.org>写道:

从我所看到的,你正在尝试


我正在考虑找出解决这些问题的正确方法是什么。

在我的程序中,我确实设法忘记了浮点数并使用了

整数,但情况可能并非总是如此 - 这就是我要问的原因。 br />
对浮点数执行相等检查。这是一个根本被打破的想法。


嗯,我知道。但有时你需要检查,例如是否你的b $ b计算的统计数据p大于例如0.95。我的问题

是:如果你想要确定性行为,你会怎么做?

您可能想要编写一个比较两个浮点数的函数
返回-1,0,1表示
A绝对小于B
A大约等于B
A绝对大于B
其中大约等于如果A和B在彼此的2或3个ULP内(您决定阈值),则为真。然后每当你想要比较两个浮点数时,你调用这个函数并检查它的返回结果。




是的,我知道,但这仍然没有给我确定性的行为。说,

我有1000个数据点的人口,我的程序运行并发现123

在统计上是显着的。现在别人接受了它,并且

发现大约两倍的统计上重要的数据点,因为

的一个...失败的比较!你如何处理这些问题 - 这个

是我的问题。


j。

-

Fire-fox你的konqueror!


< NO ************ @ uni-muenster.de>在消息中写道

news:cc ********** @ sagnix.uni-muenster.de ...

John Cochran< jd * @ smof.fiawol.org>写道:

从我所看到的,你正在尝试



我正在考虑找出解决这些问题的正确方法。<在我的程序中,我确实设法忘记了浮点数并使用了整数,但情况可能并非总是如此 - 这就是我要问的原因。

对浮点数执行相等检查。这是一个根本被打破的想法。




你想要做的是比较他们的差异的绝对值和

门槛。


#define EPSILON 0.00001


if(fabs(d2-d1)< EPSILON)


{


//他们基本上是平等的


}


否则


{


//他们不相等


}


Andre


Hello,

I have a problem with float comparisons. Yes, I have read FAQ point
14.4ff.

Assume this little code sniplet :

#include <stdio.h>

int main(int argc, char *argv[]) {
double a = 3 ;
double b = 5 ;

if( (a/b) > 0.6)
printf(">\n") ;
else
printf("<=\n") ;

return EXIT_SUCCESS ;
}

As you might guess, it sometimes returns ">", and sometimes "<=".

Now it is of much importance for me that the program in a really, really
repeatable fashion. The threshold may be arbitrary, but what I want to
avoid at all costs are different results depending on what compiler
options I use, for example.

In my "real world" program, a and b are ints, so I could solve the
problem by doing

if( (a * 10) > (b * 6) )

But my question is: if it _were_ doubles, what should I do then? How to
get a fully deterministic statement, independent of architecture,
compilers and stuff?

Regards,

January

--
Fire-fox your konqueror!

解决方案

In article <cc**********@sagnix.uni-muenster.de>,
<NO************@uni-muenster.de> wrote:

Hello,

I have a problem with float comparisons. Yes, I have read FAQ point
14.4ff.

Assume this little code sniplet :

#include <stdio.h>

int main(int argc, char *argv[]) {
double a = 3 ;
double b = 5 ;

if( (a/b) > 0.6)
printf(">\n") ;
else
printf("<=\n") ;

return EXIT_SUCCESS ;
}

As you might guess, it sometimes returns ">", and sometimes "<=".

Now it is of much importance for me that the program in a really, really
repeatable fashion. The threshold may be arbitrary, but what I want to
avoid at all costs are different results depending on what compiler
options I use, for example.

In my "real world" program, a and b are ints, so I could solve the
problem by doing

if( (a * 10) > (b * 6) )

But my question is: if it _were_ doubles, what should I do then? How to
get a fully deterministic statement, independent of architecture,
compilers and stuff?



From what I can see, you are attempting to perform an equality check against
a floating point number. That is a fundamentally broken idea.

You may want to write a function that compares two floating point numbers
and returns -1, 0, 1 for the cases of
A definitely less than B
A approximately equal to B
A definitely greater than B
where the "approximately equal to" is true if A and B are within 2 or 3 ULPs
of each other (you decide the threshold). Then whenever you want to compare
two floating point numbers, you call this function and check its return
result.


John Cochran <jd*@smof.fiawol.org> wrote:

From what I can see, you are attempting
I''m attepting to find out what the proper way of solving such problems is.
In my program, I did manage to forget the floating point numbers and use
integers, but that might not always be the case -- and that is why I ask.
to perform an equality check against
a floating point number. That is a fundamentally broken idea.
Well, I know. But somehow sometimes you need to check e.g. whether your
calculated statistics p is greater than, for example, 0.95. My question
is: what do you do then, if you want deterministic behaviour?
You may want to write a function that compares two floating point numbers
and returns -1, 0, 1 for the cases of
A definitely less than B
A approximately equal to B
A definitely greater than B
where the "approximately equal to" is true if A and B are within 2 or 3 ULPs
of each other (you decide the threshold). Then whenever you want to compare
two floating point numbers, you call this function and check its return
result.



Yes, I know, but this still does not give me deterministic behaviour. Say,
I have a population of 1000 data points, my program runs and finds that 123
of them are statistically significant. Now someone else takes it, and
finds around twice as much statistically significant data points, because
of one f...loating comparison! How do you deal with such problems -- this
is my question.

j.
--
Fire-fox your konqueror!


<NO************@uni-muenster.de> wrote in message
news:cc**********@sagnix.uni-muenster.de...

John Cochran <jd*@smof.fiawol.org> wrote:

From what I can see, you are attempting



I''m attepting to find out what the proper way of solving such problems is.
In my program, I did manage to forget the floating point numbers and use
integers, but that might not always be the case -- and that is why I ask.

to perform an equality check against
a floating point number. That is a fundamentally broken idea.



What you want to do is compare the absolute value of their difference to
some threshold.

#define EPSILON 0.00001

if (fabs(d2-d1) < EPSILON)

{

// They''re essentially equal

}

else

{

// They''re not equal

}

Andre


这篇关于Float比较:如何正确地完成它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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