cl:编译时间与运行时间:inf与-nan(ind) [英] cl: compile time vs. run time: inf vs. -nan(ind)
问题描述
示例代码( t50.c
):
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>
#include <float.h>
#include <assert.h>
const float d1 = NAN;
const float d2 = -0x0p+0;
const float d3 = NAN / -0x0p+0;
typedef union { uint32_t u; float d; } u_t;
int main(void)
{
u_t u1;
u_t u2;
u_t u3;
u1.d = *(volatile float*)&d1 / *(volatile float*)&d2;
u2.d = d3;
u3.d = d1 / d2;
if ( u1.u != u2.u || u1.u != u3.u )
{
printf("error:\n");
printf("u1 (run time) %08"PRIx32" %.*e\n", u1.u, DECIMAL_DIG, u1.d);
printf("u2 (compile time) %08"PRIx32" %.*e\n", u2.u, DECIMAL_DIG, u2.d);
printf("u3 %08"PRIx32" %.*e\n", u3.u, DECIMAL_DIG, u3.d);
}
return 0;
}
编译器调用: cl t50.c/O1/fp:precise&&t50
预期结果:< nothing>
实际结果(与 cl x86
和 cl x64
相同):
Actual result (same for cl x86
and cl x64
):
error:
u1 (run time) ffc00000 -nan(ind)
u2 (compile time) 7f800000 inf
u3 ffc00000 -nan(ind)
我指定了/fp:strict
: cl t50.c/O1/fp:strict&&t50
,但得到了:
I've specified /fp:strict
: cl t50.c /O1 /fp:strict && t50
, but got:
t50.c(8): error C2099: initializer is not a constant
t50.c(10): error C2099: initializer is not a constant
cl
版本:x86的 19.25.28611
和x64的 19.25.28611
.
cl
version: 19.25.28611 for x86
and 19.25.28611 for x64
.
与 gcc
(10.2.0)和 clang
(11.0.0)进行比较:
Compare with gcc
(10.2.0) and clang
(11.0.0):
gcc t50.c -O2 && a.exe
<nothing>
clang t50.c -O2 && a.exe
<nothing>
为什么?我在这里想念的是什么?标准(C/IEEE)怎么说?
Why? What I'm missing here? What the standard (C / IEEE) says?
UPD1:
-
if(u1.d!= u2.d || u1.d!= u3.d)的结果相同.
- 相同的结果
不包含联合
(即使用float u1,u2,u3
).
- Same result for
if ( u1.d != u2.d || u1.d != u3.d )
. - Same result
w/o unions
(i.e. usingfloat u1, u2, u3
).
UPD2:
- 编译器的NAN定义:
Windows Kits \ 10 \ Include \ 10.0.18362.0 \ ucrt \ corecrt_math.h
:#define NAN((float)(INFINITY * 0.0F))代码>.
- u1.d = NAN的输出printf("NAN%08" PRIx32%.* e \ n",u1.u,DECIMAL_DIG,u1.d); (对于
cl x86
和clx64
):NAN ffc00000 -nan(ind)
.
- compiler's NAN definition:
Windows Kits\10\Include\10.0.18362.0\ucrt\corecrt_math.h
:#define NAN ((float)(INFINITY * 0.0F))
. - Output for
u1.d = NAN; printf("NAN %08"PRIx32" %.*e\n", u1.u, DECIMAL_DIG, u1.d);
(for bothcl x86
andcl x64
):NAN ffc00000 -nan(ind)
.
推荐答案
为什么?
cl
不遵循关于 IEEE-754 > NAN
.
cl
does not follow IEEE-754 concerning NAN
.
我在这里想念什么?
What I'm missing here?
假设遵循IEEE标准的C编译器.
Assuming a compliant C compiler follows IEEE.
标准C怎么说?
C对 NAN
部门的规范不严格.我认为编译器是合规的,只是关于 NAN
的实现质量较差.
C is lax with specs in the NAN
department. I'd rate the compiler as compliant, just a weak quality of implementation concerning NAN
.
IEEE的标准是什么?
What the standard IEEE says?
不符合规定.
这篇关于cl:编译时间与运行时间:inf与-nan(ind)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!