当前位置: C语言 -- 标准库 -- <float.h> -- FLT_ROUNDS

FLT_ROUNDS宏


概要:
#include <float.h>
#define FLT_ROUNDS value	//value值由实现定义。

描述:

该宏表示标准浮点类型的舍入模式。对于标准浮点类型,浮点数加法的舍入模式由宏FLT_ROUNDS的实现定义值来表征。

对宏FLT_ROUNDS的评估能正确反映fesetround函数对舍入模式所做的任何更改。

宏FLT_ROUNDS的可能值:

-1   舍入模式不明确。
0   向0舍入。
1   向偶数方向就近舍入。
2   向+∞舍入。
3   向-∞舍入。
4   向远离0的方向就近舍入。

如果宏FLT_ROUNDS存在其它值,将表征实现定义的舍入模式。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
/*宏FLT_ROUNDS范例*/

#include <fenv.h>
#include <float.h>
#include <stdio.h>

#pragma STDC FENV_ACCESS ON

int main(void)
{
    puts("Default rounding direction of floating-point operations:");
    printf("FLT_ROUNDS = %d\n",FLT_ROUNDS);

    fesetround(FE_UPWARD);
    puts("Current rounding direction of floating-point operations:");
    printf("FLT_ROUNDS = %d\n",FLT_ROUNDS);

    return 0;
}


输出:

Default rounding direction of floating-point operations:

FLT_ROUNDS = 1

Current rounding direction of floating-point operations:

FLT_ROUNDS = 2

(注:使用fesetround函数修改舍入模式时,宏FLT_ROUNDS值应该能够反映这种变化;但各编译器支持程度不一样,Visual Studio 2017、Pelles C能够反映这种变化。)


相关内容:
FLT_EVAL_METHOD 表示评估方法的宏。


主要参考资料:

1、ISO/IEC 9899:2024

2、ISO/IEC 9899:2018