FLT_HAS_SUBNORM为0:使用此类次法线时,手动构造的次法线是否会导致行为明确? [英] FLT_HAS_SUBNORM is 0: does manually constructed subnormal lead to well-defined behavior when using such subnormal?

查看:75
本文介绍了FLT_HAS_SUBNORM为0:使用此类次法线时,手动构造的次法线是否会导致行为明确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 FLT_HAS_SUBNORM 为0:手动构造(例如,使用 union 或使用 memcpy()键入punning)将该法线用作FP操作(例如FP比较)的输入时,该法线会导致行为良好的定义?

In case if FLT_HAS_SUBNORM is 0: does manually constructed (using, for example, type punning via union or using memcpy()) subnormal lead to well-defined behavior when such subnormal is used as an input for FP operations (for example, FP comparison)?

示例:

#include <stdio.h>
#include <float.h>
int main( void )
{
    union { unsigned int i; float f; } u = { .i = 1 }; // subnormal
    printf("%d %d %a 0x%08lx\n", FLT_HAS_SUBNORM, u.f != 0.0f, u.f, u.i);
    return 0;
}

输出:

叮当声: 1 1 0x1p-149 0x00000001

gcc: 1 1 0x1p-149 0x00000001

测试编译器: 0 1 0x0p + 0 0x00000001

注意:这里我们看到测试编译器不支持%a 的次常态(由/与 FLT_HAS_SUBNORM 为0的相关性解释).

Note: here we see that test compiler does not support %a for subnormals (which is explained by / correlates with FLT_HAS_SUBNORM is 0).

问题:

  1. 这里 u.f!= 0 是否导致行为明确?
  2. 如果是这样,那么如果 FLT_HAS_SUBNORM 为0,为什么C标准允许此类手动构造的子范式(以及具有此类手动构造的子范式的FP操作导致IEEE 754符合结果)?
  1. Is u.f != 0 here leads to well-defined behavior?
  2. If so, then why C standard permits such manually constructed subnormals (as well as FP operations with such manually constructed subnormals leading to IEEE 754 conformant results) if FLT_HAS_SUBNORM is 0?

UPD.我已经阅读了标准.我很困惑,因为它说:

UPD. I've read the standard. I'm confused because it says:

FLT_HAS_SUBNORM为0表示不存在次正规数字(类型不支持次正规数字)

但是,尽管在上面的示例中类型不支持次正规数,我们已经看到可以手动构造次正规并对其执行一些FP操作,以获得符合IEEE 754的结果.

However, despite of type does not support subnormal numbers in the example above we've seen that it is possible to manually construct subnormals and perform some FP operations on them getting IEEE 754 conformant results.

UPD.换句话说:是否将上述示例( u.f!= 0.0f )解释为违反了 FLT_HAS_SUBNORM为0 ?如果是这样,那么请证明这一点.

UPD. In other words: shall the example above (u.f != 0.0f) be interpreted as a violation of FLT_HAS_SUBNORM is 0? If so, then please prove it.

推荐答案

u!f = 0是否导致行为明确?

Is u.f != 0 here leads to well-defined behavior?

不.如果不支持非正规数,则无论如何获取,无论是通过源代码中的文字,算术还是对表示浮点对象的位进行操作,都将不受支持.

No. If subnormal numbers are not supported, then they are not supported regardless of how they are obtained, whether by literals in source code, arithmetic, or manipulation of the bits that represent floating-point objects.

如果是这样,那么为什么C标准允许这样的人工构造的法线……

If so, then why C standard permits such manually constructed subnormals…

C是一种低级语言,它允许直接访问代表任何对象的字节.您还可以构造由于奇偶校验错误(在具有此类的实现上)而无效的整数表示形式,以及不在定义的字符集中的 char 对象的表示形式.您还可以修改 FILE struct tm 的字节,并生成具有不受支持的值的对象.您可以操作字节的事实并不意味着对字节的任何操作都会产生有效或受支持的值.

C is a low-level language and allows direct access to the bytes that represent any object. You can also construct representations of integers that are invalid due to parity errors (on implementations that have such) and representations of char objects that are not in the defined character set. You can also tinker with the bytes of a FILE or a struct tm and produce objects with unsupported values. The fact you can manipulate the bytes does not mean any manipulation of the bytes results in a valid or supported value.

这篇关于FLT_HAS_SUBNORM为0:使用此类次法线时,手动构造的次法线是否会导致行为明确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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