断言(x> 0.0&& 1&& x == 0.0)保持 [英] assert( x > 0.0 && 1 && x == 0.0 ) holding

查看:82
本文介绍了断言(x> 0.0&& 1&& x == 0.0)保持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到浮点行为不一致的问题

导致例如


断言(x> 0.0&& putchar(' '\ n'')&& x == 0.0);


持有。 (实际上,我的问题是我得到的双重问题

失败的断言断言,起初认为应该持有b $ b,但这并不重要。)


最后是一个包含上述貌似

不一致断言的完整程序。在我的x86上使用gcc断言

当x是double类型时不会失败。


AFAIK,发生的事情是在第一个x临时在80位寄存器中存在
,其精度高于正常值64位的正常值。因此,即使在真实的情况下,x大于0.0也是大于0.0。 64位值为0.0。如果你将该值转换为长两个并且打印它,你可以看到它确实稍微大于
起初大于0.0,但随后变为0.0。


没有失败断言(x> 0.0& 1&& x == 0.0);"

可以接受?


问题的最佳解决方法是什么?一种可能性是

在需要时使用易变的中间变量,如下所示:

volatile double y = x;

assert( y> 0.0&& putchar(''\ n'')&& y == 0.0);


这是最好的解决方案吗?

Daniel Vallstrom


/ *测试奇怪的不一致浮点行为导致

类似于断言(x> 0.0&& 1&& x == 0.0);持有!

Daniel Vallstrom,041030。

编译如下:gcc -std = c99 -pedantic -Wall -O -lm fpbug.c

* /


#include< stdio.h>

#include< math.h>

#include< ; assert.h>


int main(无效)

{

double x = nextafter(0.0,-1.0)* nextafter(0.0,-1.0);


/ *下面的putchar-conjunct只是* /

/ *中的任意内容为了清除x-注册为副作用。至少* /

/ *这就是我猜的正在发生的事情。 * /

断言(x> 0.0&& putchar(''\ n'')&& x == 0.0);


返回0;

}

I''m having problems with inconsistent floating point behavior
resulting in e.g.

assert( x > 0.0 && putchar(''\n'') && x == 0.0 );

holding. (Actually, my problem is the dual one where I get
failed assertions for assertions that at first thought ought
to hold, but that''s not important.)

At the end is a full program containing the above seemingly
inconsistent assertion. On my x86 using gcc the assertion
doesn''t fail when x is of type double.

AFAIK, what is happening is that at first x temporary resides
in a 80 bit register with a higher precision than the normal
64 bits. Hence x is greater than 0.0 at first even though
the "real" 64-bit value is 0.0. If you cast the value to long
double and print it you can see that it indeed is slightly
larger than 0.0 at first, but then becomes 0.0.

Is not failing "assert( x > 0.0 && 1 && x == 0.0 );"
acceptable?

What is the best workaround to the problem? One possibility is
using volatile intermediate variables, when needed, like this:

volatile double y = x;
assert( y > 0.0 && putchar(''\n'') && y == 0.0 );

Is that the best solution?
Daniel Vallstrom

/* Tests weird inconsistent floating point behavior resulting in
something like "assert( x > 0.0 && 1 && x == 0.0 );" holding!
Daniel Vallstrom, 041030.
Compile with e.g: gcc -std=c99 -pedantic -Wall -O -lm fpbug.c
*/

#include <stdio.h>
#include <math.h>
#include <assert.h>

int main( void )
{
double x = nextafter( 0.0, -1.0 ) * nextafter( 0.0, -1.0 );

/* The putchar-conjunct below is just something arbitrary in */
/* order to clear the x-register as a side-effect. At least */
/* that''s what I guess is happening. */
assert( x > 0.0 && putchar(''\n'') && x == 0.0 );

return 0;
}

推荐答案

Daniel Vallstrom写道:
Daniel Vallstrom wrote:
我'' m具有不一致的浮点行为问题导致例如断言(x> 0.0&& putchar(''\ n'')&& x == 0.0);
I''m having problems with inconsistent floating point behavior
resulting in e.g.

assert( x > 0.0 && putchar(''\n'') && x == 0.0 );




由于舍入,截断等原因,测试浮点数是否等于零是

无限期的事情FP错误。你可以永远不确定它是否相等,尽管IEEE规范

将0定义为在某个FP范围内。


还有其他一些方法可以构建逻辑吗?


-

谢谢,

Elliott C. B?ck

--------------------------
www.elliottback.com
www.spreadIE.com


2004年10月29日16:39:04 -0700,Daniel Vallstrom

< da ************** @ gmail.com>写道:
On 29 Oct 2004 16:39:04 -0700, Daniel Vallstrom
<da**************@gmail.com> wrote:
没有失败断言(x> 0.0&& 1&& x == 0.0);
可以接受?


这完全取决于(正如已故的Joad教授曾经说过的)你的意思

浮点数相等。或者更确切地说,你的编译器是什么,b $ b和处理器认为它意味着什么。你能让你的编译器生成

汇编代码并检查吗?它可能正在寻找绝对值
下溢和一个NAN(非数字,一个特殊值表明

发生了奇怪的事情)被评估为对于平等而言,零但是

仍然有一个迹象。

这个问题的最佳解决方法是什么?一种可能性是在需要时使用易失性中间变量,如下所示:

volatile double y = x;
断言(y> 0.0&& putchar(''这是最好的解决方案吗?
Is not failing "assert( x > 0.0 && 1 && x == 0.0 );"
acceptable?
It all depends (as the late Professor Joad used to say) on what you mean
by equality of floating point numbers. Or rather, on what your compiler
and processor think it means. Can you get your compiler to generate
assembler code and check? It may be that it is looking for "absolute
value less than a very small amount" for equality, and the value is not
quite zero. Or perhaps your multiply which generated it resulted in
underflow and a NAN ("Not A Number", a special value indicating that
something odd happened) which is evaluated as ''zero'' for equality but
still has a sign.
What is the best workaround to the problem? One possibility is
using volatile intermediate variables, when needed, like this:

volatile double y = x;
assert( y > 0.0 && putchar(''\n'') && y == 0.0 );

Is that the best solution?



通过具有条件

来理解你想要实现的目标,这应该总是像这样评估为假。或者你期望y

(或x)在测试之间的短时间内改变值y> 0和y ==

0?中间的putchar()只会让事情更加混乱。这是

你在一个更大的程序中找到的东西吗? nextafter()函数调用

意味着。


尝试放入printfs跟踪x的值,高得可笑

精度:


printf("% - 30.30g \ n",x);


应显示值是否为实际上是零(没有四舍五入)

或不是。

(即使我的gcc上的x值非常小,我也不能让它失败

2.95.4 Debial GNU / Linux Duron 1200系统...)


Chris C



I don''t understand what you are trying to achieve by having a condition
which should always evaluate as false like that. Or are you expecting y
(or x) to change value in the short time between testing y > 0 and y ==
0? The putchar() in the middle just confuses things more. Is this
something you found in a larger program? The nextafter() function call
would imply that.

Try putting in printfs tracing the value of x, with ridiculously high
precision:

printf("%-30.30g\n", x);

That should show whether the value is actually zero (with no rounding)
or not.

(I can''t get it to fail even with very small values of x on my gcc
2.95.4 Debial GNU/Linux Duron 1200 system...)

Chris C


Elliott Back< el ***** @ cornell.edu>在消息新闻中写道:< Bs ********************* @ twister.nyroc.rr.com> ...
Elliott Back <el*****@cornell.edu> wrote in message news:<Bs*********************@twister.nyroc.rr.com >...
Daniel Vallstrom写道:
Daniel Vallstrom wrote:
我遇到浮点行为不一致的问题导致例如断言(x> 0.0&& putchar( ''\ n'')&& x == 0.0);
由于舍入,截断和其他FP,测试浮点数是否等于零是一个不确定的事情。错误。你可能永远无法确定它是否相等,尽管IEEE规范将定义0在一定的FP范围内。
I''m having problems with inconsistent floating point behavior
resulting in e.g.

assert( x > 0.0 && putchar(''\n'') && x == 0.0 );
Testing whether a floating point number is equal to zero is an
indefinite thing, due to roundoff, truncation, and other FP errors. You
can never really be sure if it''s equal, although the IEEE specification
will define 0 to be within a certain FP range.




我不认为这是问题所在。我不介意有限的

精度。我的问题是触发器的行为,显示为x

一开始就是一件事,然后突然发生另一件事没有发生任何事情。如果它是你反对的0.0,那么你可以用一个y!= 0.0代替它。我会在帖子末尾添加这样一个例子




还有其他一些方法可以构建逻辑吗?



I don''t think that''s the issue here. I don''t mind the limited
precision. My problem is the flip-flop behavior, showing as x
being one thing at first, then suddenly another thing without
anything happening in between. If it''s the 0.0 you object to, you
can replace that with an y!=0.0. I''ll add such an example at
the end of the post.

Is there some other way you can structure the logic?




不知道你的意思。如果你的意思是削弱

断言(在真实程序中)我永远不会这样做。

volatile解决方案似乎更好。

Daniel Vallstrom


/ *测试奇怪的不一致浮点行为导致

类似于断言(x> y&& 1&& x == y);持有!

Daniel Vallstrom,041030。

编译如下:gcc -std = c99 -pedantic -Wall -O -lm fpbug2.c

* /


#include< stdio.h>

#include< math.h>

#include< ; assert.h>


int main(无效)

{

double y = nextafter(0.0,1.0);

double x = y + 0x1.0p-2 * y;


/ *下面的putchar-conjunct只是* /
中的任意内容
/ *命令将x-register清除为副作用。至少* /

/ *这就是我猜的正在发生的事情。 * /

断言(x& y&& putchar(''\ n'')&& x == y);


返回0;

}



Don''t know what you mean by this. If you mean weakening the
assertions (in the real program) I would never do that. The
volatile solution seems better.
Daniel Vallstrom

/* Tests weird inconsistent floating point behavior resulting in
something like "assert( x > y && 1 && x == y );" holding!
Daniel Vallstrom, 041030.
Compile with e.g: gcc -std=c99 -pedantic -Wall -O -lm fpbug2.c
*/

#include <stdio.h>
#include <math.h>
#include <assert.h>

int main( void )
{
double y = nextafter( 0.0, 1.0 );
double x = y + 0x1.0p-2 * y;

/* The putchar-conjunct below is just something arbitrary in */
/* order to clear the x-register as a side-effect. At least */
/* that''s what I guess is happening. */
assert( x > y && putchar(''\n'') && x == y );

return 0;
}


这篇关于断言(x> 0.0&amp;&amp; 1&amp;&amp; x == 0.0)保持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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