无效的浮点值 [英] invalid floating point value

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

问题描述

好的,我知道我在这里要求的内容不太可能在C标准中有一个

答案。尽管如此,对于任何特定的平台来说都不是特定的,所以我会冒这个问题。


AC double可以通过一个数组来替换unsigned char,可以写入任何所需字节模式的
。是否有任何一种可靠的分配字节模式的方式,以便结果

将以非常高的概率_not_计为有效的双倍?
< br $>
我问,因为我正在寻找一种标记缺少

值的机制。在一系列双打中。我知道最好的 (完全

便携式)方式可能涉及设置一个辅助数组,或者

使得基本数据类型不是直的双重而是结构的

某种。但在上下文中,这将是一种皇室般的痛苦。一个

常见解决方案是标记缺失具有特定值

为-999.0。我想知道如果没有

求助于更广泛的数据类型,是否可以改进。


Allin Cottrell

唤醒森林大学

OK, I realize that what I am asking here is not likely to have a
answer within the C standard. Nonetheless, it is not specific
to any particular platform, so I''ll hazard the question anyway.

A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?

I ask because I''m in search of a mechanism for flagging "missing
values" in an array of doubles. I''m aware that the "best" (fully
portable) way probably involves setting up an ancillary array, or
making the basic data-type not a straight double but a struct of
some sort. But in context this would be a royal pain. One
common solution is to flag "missing" with a specific value such
as -999.0. I''m wondering if that can be improved upon without
resorting to a wider data type.

Allin Cottrell
Wake Forest University

推荐答案

>好的,我知道我在这里问的不太可能有
>OK, I realize that what I am asking here is not likely to have a
答案在C标准内。尽管如此,它并不特定于任何特定平台,所以无论如何我都会冒这个问题。

AC double可以通过一组unsigned char来别名,其中
可以写入任何所需的字节模式。是否有任何可移植的方式来分配字节模式,以便结果
将以非常高的概率_not_ count_作为有效的双重?
answer within the C standard. Nonetheless, it is not specific
to any particular platform, so I''ll hazard the question anyway.

A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?




NO。


但是,许多实现都使用IEEE浮点数,它具有一个名为NaN的非b $ b奇数值(或一组值)(不是一个号码)。如果

它可用于您的实现,这是一个合理的候选人

表示无效值。此外,使用NaN的算术几乎总是

导致NaN。


double x;

...放置一个值在x ...;

if(x!= x){...你在这里有NaN ......;但是,b $ b(有些编译器可能会弄错。)


获取NaN的一些方法包括0.0 / 0.0和log(0.0)。注意

表示最便携的方式不涉及用字节覆盖双$
并将字节填充到字节中。


其他可能性包括使用非常大的正面或负面

值,不可能是有效值。例如,1.0e30

可能不是电费账单余额的有效值,但是如果您错误地发送了一个电子账单余额,它可能会让您获得新闻<无论如何,
金额。 1.0e30也在ANSI C最小范围内

浮动和双倍。


Gordon L. Burditt



NO.

However, many implementations use IEEE floating point, which has a
oddball value (or set of values) called a NaN (Not A Number). If
it is available on your implementation, this is a reasonable candidate
for an invalid value. Further, arithmetic with a NaN almost always
results in a NaN.

double x;
... put a value in x ...;
if (x != x ) { ... you have a NaN here ... ; }
(some compilers might get this wrong, though.)

Some ways to get hold of a NaN include 0.0/0.0 and log(0.0). Note
that the "most portable" way does NOT involve overlaying a double
with bytes and stuffing something into the bytes.

Other possibilities include using a very large positive or negative
value, which cannot possibly be a valid value. For example, 1.0e30
is likely not a valid value for an electric bill balance, but it
might get you in the news if you mistakenly send one out for that
amount anyway. 1.0e30 is also within the ANSI C minimum range for
float and double.

Gordon L. Burditt

好的,我知道我在这里要求的内容不太可能在C标准中有答案。尽管如此,它并不特定于任何特定平台,所以无论如何我都会冒这个问题。

AC double可以通过一组unsigned char来别名,其中
可以写入任何所需的字节模式。是否有任何可移植的方式来分配字节模式,以便结果
将以非常高的概率,_not_计为有效的双倍?
NO。
但是,许多实现使用IEEE浮点,它具有称为NaN(非数字)的奇数值(或一组值)。如果
可以在您的实现中使用,那么对于无效值,这是一个合理的候选者。此外,使用NaN算术几乎总是会产生NaN。
OK, I realize that what I am asking here is not likely to have a
answer within the C standard. Nonetheless, it is not specific
to any particular platform, so I''ll hazard the question anyway.

A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?
NO.

However, many implementations use IEEE floating point, which has a
oddball value (or set of values) called a NaN (Not A Number). If
it is available on your implementation, this is a reasonable candidate
for an invalid value. Further, arithmetic with a NaN almost always
results in a NaN.




是的。进口是*差不多*;可以给你一些俗气的错误。


旁边的好故事:我的一位同事曾经遇到过这样的问题

amatrix他突然工作似乎只能拿着0.0

作为价值。经过一段时间的搜索,他找到了我,然后我们通过调试器找到了
,在某一特定点发现了

"矩阵" (仍然用0填充)用1 / inf缩放并且所有零点

进入某种NaN状态但是没有像所有其他NaN那样打印为NaN

会有...

不一样的问题,我知道,但我认为很高兴知道

这些东西仍然会发生在IEEE FPs上...


double x;
...在x中放一个值...;
if(x!= x){...你在这里有一个NaN .. 。;但是(某些编译器可能会弄错。)

获取NaN的一些方法包括0.0 / 0.0和log(0.0)。注意,最便携的是最便携的。方式不涉及用字节覆盖双层和填充字节的东西。


或者您可以使用< math.h>

中的isnan()宏(我现在不确定这是否仅存在于C99与否)


其他可能性包括使用非常大的正或负值,这可能不是有效值。例如,1.0e30
可能不是电费账单余额的有效值,但是如果您错误地发送了一个用于该金额的金额,它可能会让您获得新闻。 1.0e30也在ANSI C最小范围内,用于
浮动和双重。



Yep. Import is the *almost*; can give you some cheesy errors.

Nice story on the side: A colleague of mine once had the problem that
a "matrix" he was working with suddenly seemed only able to hold 0.0
as values. After searching for some time he came to me and we went
through with the debugger, finding that at one particular point the
"matrix" (still filled with zeros) scaled with 1/inf and all the zeros
got into a sort of NaN state but did not print as NaNs as all other NaNs
would have...
Not the same problem, I know, but I think it is good to know that
these things still can happen with IEEE FPs...

double x;
... put a value in x ...;
if (x != x ) { ... you have a NaN here ... ; }
(some compilers might get this wrong, though.)

Some ways to get hold of a NaN include 0.0/0.0 and log(0.0). Note
that the "most portable" way does NOT involve overlaying a double
with bytes and stuffing something into the bytes.
Or you can use the isnan() macro from <math.h>
(I am not sure at the moment whether this is only there in C99 or not)

Other possibilities include using a very large positive or negative
value, which cannot possibly be a valid value. For example, 1.0e30
is likely not a valid value for an electric bill balance, but it
might get you in the news if you mistakenly send one out for that
amount anyway. 1.0e30 is also within the ANSI C minimum range for
float and double.




另一个注意事项:如果你只想标记有一些

无效值,你可以使用类似errno的机制和

在每次作业后检查测试模式,甚至可以中止

使用assert(0)抛出必要的信息之后的程序。

便于携带方式。


如果你真的想用你实际的双确定你只会使用IEEE双打,产生一个NaN然后

写入尾数的32位,无论你怎么想


适合发出特定情况的信号,例如0xdeadbeef。

但是,如果

是将值加载到某个寄存器的中间步骤,则此信息可能无法按预期获得

回复。

干杯

Michael



On another note: If you only want to flag that there is some
invalid value out there, you can use an errno-like mechanism and
check in a test mode after every assignment, perhaps even aborting
the program after throwing out the necessary info using assert(0).
Easier to do in a portable way.

If you really want to use you actual double values and are fairly
sure that you will only use IEEE doubles, produce a NaN and then
write to the lower 32 Bit of the mantissa whatever you think
befitting to signal the specific condition, e.g. 0xdeadbeef.
However, this information might not get to you as expected if there
are intermediate steps where the value gets loaded into some register
and written back.
Cheers
Michael


Allin Cottrell写道:
Allin Cottrell wrote:

好的,我意识到我在这里要求的内容不太可能在C标准中有答案。尽管如此,它并不特定于任何特定平台,所以无论如何我都会冒这个问题。

AC double可以通过一组unsigned char来别名,其中
可以写入任何所需的字节模式。是否有任何可靠的方式来分配字节模式,以便结果
将以非常高的概率_not_计为有效的双倍?

我问,因为我'我正在寻找一种标记缺失值的机制。在一系列双打中。我知道最好的 (完全
便携式)方式可能涉及设置一个辅助数组,或者使基本数据类型不是直的双重而是某种形式的结构。但在上下文中,这将是一种皇室般的痛苦。一个常见的解决方案是标记缺失。具有特定值,如-999.0。我想知道是否可以在不使用更广泛的数据类型的情况下改进。

OK, I realize that what I am asking here is not likely to have a
answer within the C standard. Nonetheless, it is not specific
to any particular platform, so I''ll hazard the question anyway.

A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?

I ask because I''m in search of a mechanism for flagging "missing
values" in an array of doubles. I''m aware that the "best" (fully
portable) way probably involves setting up an ancillary array, or
making the basic data-type not a straight double but a struct of
some sort. But in context this would be a royal pain. One
common solution is to flag "missing" with a specific value such
as -999.0. I''m wondering if that can be improved upon without
resorting to a wider data type.




而不是-999.0,使用HUGE_VAL或-HUGE_VAL ,来自math.h.


-

pete



Instead of -999.0, use either HUGE_VAL or -HUGE_VAL, from math.h.

--
pete


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

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