简体   繁体   English

在 Windows / msvc 上 clang:为什么在 FE_UPWARD printf("%.1f\\n", 0.0) 下打印 0.1 而不是 0.0?

[英]clang on Windows / msvc: why under FE_UPWARD printf("%.1f\n", 0.0) prints 0.1 instead of 0.0?

Sample code (t928.c):示例代码(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( "%.1f\n", 0.0 );
        }
        return 0;
}

Invocations:调用:

$ 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

Versions:版本:

$ clang --version
clang version 12.0.0

$ cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29913 for x64

UPD20210824. UPD20210824。 User chux - Reinstate Monica supposed:用户 chux - 恢复莫妮卡假设:

Some compilers have trouble properly including FP support when there is no FP activity in user code.当用户代码中没有 FP 活动时,某些编译器无法正确包含 FP 支持。 Try adding some.尝试添加一些。

Here is it:就这个:

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

Invocations:调用:

$ 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. UPD20210831。 Answer from Microsoft:来自微软的回答:

This is an issue in the Universal CRT with our FE_UPWARD and FE_DOWNWARD rounding modes where some numbers will round up as though they had additional non-zero digits after them.这是我们的 FE_UPWARD 和 FE_DOWNWARD 舍入模式中通用 CRT 中的一个问题,其中一些数字会舍入,就好像它们后面有额外的非零数字一样。 This will be fixed in the Universal CRT and will be included in a future release of the Windows OS and Windows SDK.这将在通用 CRT 中修复,并将包含在 Windows 操作系统和 Windows SDK 的未来版本中。

因为它是通用 CRT 中的一个错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM