它是使用IEEE754浮点NaN的为其未设定值是一个好主意? [英] Is it a good idea to use IEEE754 floating point NaN for values which are not set?

查看:144
本文介绍了它是使用IEEE754浮点NaN的为其未设定值是一个好主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是使用IEEE754浮点NaN(非-A-号码)这是未定义非数学的原因是什么?

Is it a good idea to use IEEE754 floating point NaN (not-a-number) for values which are undefined for non-mathematical reasons?

在我们的情况下,它们还没有设置,因为值尚未从其他设备接收。上下文是使用IEC1131 REAL32值嵌入式系统。 修改的编程语言是C,所以我们将最有可能使用NAN和isnanf(X),这是由C99。虽然我们可能需要一些额外的扭曲让这些进入我们的操作系统兼容层。

In our case they are not yet set because the values have not been received from some other device. The context is an embedded system using IEC1131 REAL32 values. The programming language is C, so we would most likely use NAN and isnanf(x), which are from C99. Though we may need some extra contortions to get these into our OS compatibility layer.

在编程语言默认似乎是初始化浮点变量与正零,其内部重组presentation都是零。这并不是我们有用的,因为0是有效的值的范围。

The default in programming languages seems to be to initialize floating point variables with positive zero, whose internal representation is all zeros. That is not usable for us, because 0 is in the range of valid values.

这似乎是一个干净的解决方案使用NaN的,但也许是更多的麻烦比它的价值,我们应该选择一些其他的价值?

It seems like a clean solution to use NaN, but maybe it is more hassle than it is worth and we should pick some other value?

推荐答案

只注意到了这个问题。

这是NaN的用途之一是,IEEE 754委员会在心中(我是委员)。算术NaN的传播规则,使这个非常有吸引力的,因为如果你有从涉及一些初始化的数据计算的长序列的结果,你会不会搞错​​的结果为有效的结果。它也可以使追溯通过你的计算来找到你所使用的初始化数据更加简单。

This is one of the uses of NaNs that the IEEE 754 committee has in mind (I was a committee member). The propagation rules for NaNs in arithmetic make this very attractive, because if you have a result from a long sequence of calculations that involve some initialized data, you will not mistake the result for a valid result. It can also make tracing back through your calculations to find where you are using the initialized data much more straightforward.

这是说,有几个陷阱是外部的754委员会的控制:正如其他人所指出的,并不是所有的硬件​​支持NaN的在速度,这可能会导致性能的危险值。幸运的是,一个不经常做很多操作上的初始化数据在性能关键的设置。

That said, there are a few pitfalls that are outside of the 754 committee's control: as others have noted, not all hardware supports NaN values at speed, which can result in performance hazards. Fortunately, one does not often do a lot of operations on initialized data in a performance-critical setting.

这篇关于它是使用IEEE754浮点NaN的为其未设定值是一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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