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

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

问题描述

对于因非数学原因未定义的值使用 IEEE754 浮点 NaN(非数字)是否是个好主意?

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,所以我们很可能会使用来自 C99 的 NAN 和 isnanf(x).虽然我们可能需要一些额外的扭曲才能将它们纳入我们的操作系统兼容层.

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.

编程语言的默认设置似乎是用正零初始化浮点变量,其内部表示全为零.这对我们来说是不可用的,因为 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?

推荐答案

刚注意到这个问题.

这是 IEEE 754 委员会考虑的 NaN 用途之一(我是委员会成员).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天全站免登陆