c语言 - Windows / msvc上的clang:为什么在FE_UPWARD printf( "%.1fn" , 0.0)下打印0.1而不是0.0?



示例代码(t928.c):

#include <stdio.h>
#include <fenv.h>
#if _MSC_VER && ! __clang__
#pragma fenv_access     (on)
#else
#pragma STDC    FENV_ACCESS ON
#endif
int main(void)
{
int i = fesetround( FE_UPWARD );
if ( ! i )
{
printf( "%.1fn", 0.0 );
}
return 0;
}

调用:

$ clang t928.c -Wall -Wextra -std=c11 -ffp-model=strict -pedantic && ./a.exe
0.1
$ cl t928.c /std:c11 /Za /fp:strict && ./t928.exe
0.1

版本:

$ clang --version
clang version 12.0.0
$ cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29913 for x64

UPD20210824。用户chux -恢复Monica假定:

当用户代码中没有FP活动时,一些编译器在正确地包括FP支持方面存在问题。试着添加一些

int main(void)
{
float f1 = 0.0f;
float f2 = 0.0f;
float f3 = 0.0f;
int i = fesetround(FE_UPWARD);
if ( ! i )
{
printf("%.1fn", 0.0);
}
f3 = f1 + f2;
printf("%.1fn", f3);
return 0;
}

调用:

$ gcc t928.c -Wall -Wextra -std=c11 -pedantic && ./a.exe
t928.c:6: warning: ignoring '#pragma STDC FENV_ACCESS' [-Wunknown-pragmas]
6 | #pragma STDC    FENV_ACCESS     ON
|
0.0
0.0
$ cl t928.c /std:c11 /Za /fp:strict && ./t928.exe
0.1
0.1
$ clang12 t928.c -Wall -Wextra -std=c11 -ffp-model=strict -pedantic && ./a.exe
0.1
0.1

UPD20210831。微软的回答:

这是通用CRT中fe_向上和fe_向下舍入模式中的一个问题,其中一些数字将四舍五入,好像它们后面有额外的非零数字。这将在通用CRT中修复,并将包含在Windows操作系统和Windows SDK的未来版本中。

因为这是通用CRT的一个bug。

最新更新