比较浮点数 [英] Comparing floating point numbers

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

问题描述




我想比较给定错误中的2个浮点数。我不会使用绝对错误,而是使用一个与两个浮点数之间可以表示的

值的数量相关的错误。我一直在读b $ b: http://www.cygnus-software.com/paper...ringfloats.htm

其中提供以下功能:


bool AlmostEqual2sComplement(float A,float B,int maxUlps){

//确保maxUlps非负且足够小以至于

//默认NAN不会比较任何东西。

断言(maxUlps 0&& maxUlps< 4 * 1024 * 1024);

int aInt = *( int *)& A;

//按字母顺序排列作为二进制补码的aInt

if(aInt< 0)

aInt = 0x80000000 - aInt;

//按字母顺序排列bnnt作为二进制补码int

int bInt = *(int *)& B;

if(bInt< 0)

bInt = 0x80000000 - bInt;

int intDiff = abs(aInt - bInt);

if(intDiff< = maxUlps)

返回true;

返回false;

}


但是,正如文章所述,这依赖于关于一些编译器

的具体功能,比如int的大小(我猜浮点数)。它

也依赖于使用IEEE表示的浮点数(我猜所有

编译器都使用它,但它是否在标准中?)。


所以我的问题是这个。是否有一个好的编译器独立方法

用于比较浮点数和相对误差?

Hi,

I''d like to compare 2 floating point numbers within a given error. I''d
rather not use a absolute error but one related to the number of
values that can be represented between the two floats. I''ve been
reading: http://www.cygnus-software.com/paper...ringfloats.htm
where the following function is provided to do this:

bool AlmostEqual2sComplement(float A, float B, int maxUlps) {
// Make sure maxUlps is non-negative and small enough that the
// default NAN won''t compare as equal to anything.
assert(maxUlps 0 && maxUlps < 4 * 1024 * 1024);
int aInt = *(int*)&A;
// Make aInt lexicographically ordered as a twos-complement int
if (aInt < 0)
aInt = 0x80000000 - aInt;
// Make bInt lexicographically ordered as a twos-complement int
int bInt = *(int*)&B;
if (bInt < 0)
bInt = 0x80000000 - bInt;
int intDiff = abs(aInt - bInt);
if (intDiff <= maxUlps)
return true;
return false;
}

However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).

So my question is this. Is there a good compiler independent method
for comparing floating point numbers with a relative error?

推荐答案

nw写道:
nw wrote:

我想比较给定错误中的2个浮点数。 [...]


但是,正如文章所述,这依赖于一些编译器特定的功能,例如int的大小(和我想漂浮)。它

也依赖于使用IEEE代表的浮点数(我猜所有的

编译器都使用它,但它是否在标准中?)。
I''d like to compare 2 floating point numbers within a given error. [...]

However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).



正确。

Right.


所以我的问题是这个。是否有一个好的编译器独立方法

用于比较浮点数和相对误差?
So my question is this. Is there a good compiler independent method
for comparing floating point numbers with a relative error?



加倍a,b;

...

if(fabs(ab)< myepsilon * max(fabs(a),fabs(b)))

//它们是相等


选择你认为合适的myepsilon。这是你的相对错误。


V

-

请删除大写''A'' s通过电子邮件回复

我没有回复最热门的回复,请不要问

double a, b;
...
if ( fabs(a-b) < myepsilon * max(fabs(a),fabs(b)) )
// they are "equal"

Pick myepsilon as you deem fit. That''s your "relative error".

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


double a,b ;
double a, b;

...

if(fabs(ab)< myepsilon * max(fabs(a),fabs(b)))

//他们是相等


选择你认为合适的myepsilon。这是你的相对错误。
...
if ( fabs(a-b) < myepsilon * max(fabs(a),fabs(b)) )
// they are "equal"

Pick myepsilon as you deem fit. That''s your "relative error".



好​​吧,如果我正确读到这将意味着大数字

允许比较小的数字更大的距离?这不是我想要的b $ b。我之前发布的函数允许我说这两个数字是最接近可能的浮点值这是两个数字>

是否可以在独立于编译器中执行此操作方式?

ok, if I''m reading this correctly this will mean that large numbers
are allowed a bigger distance than smaller ones? This isn''t exactly
what I want. The function I previously posted allows me to say "are
these two numbers with the X nearest possible floating point values"
is it possible to do that in a compiler independent way?


nw写道:
nw wrote:

>双a,b;
......
if(fabs(ab)< myepsilon * max(fabs(a),fabs(b)))
//它们是相等的 ;

选择你认为合适的myepsilon。这是你的相对错误。
> double a, b;
...
if ( fabs(a-b) < myepsilon * max(fabs(a),fabs(b)) )
// they are "equal"

Pick myepsilon as you deem fit. That''s your "relative error".



好​​吧,如果我正确读到这个,这意味着大号

的允许距离比较小?


ok, if I''m reading this correctly this will mean that large numbers
are allowed a bigger distance than smaller ones?



更大的绝对距离,但相对距离相同。

Bigger absolute distance, but the same relative distance.


这不是完全

我想要什么。
This isn''t exactly
what I want.



但这就是相对的含义。意味着。

But that''s what "relative" means.


我之前发布的函数允许我说这两个数字是最近可能的浮点值。
The function I previously posted allows me to say "are
these two numbers with the X nearest possible floating point values"



嗯?请重新阅读双引号内的语句并尝试用数学符号表示它。

Huh? Please re-read the statement inside double quotes and try
expressing it in mathematical notation.


是否可以在编译器中执行此操作独立的方式?
is it possible to do that in a compiler independent way?



一旦我知道你想要的确切(或相对),我会

尝试提供帮助。 />

V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

As soon as I know what exactly (or relatively) it is you want, I''ll
try to help.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


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

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