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

rintl函数


概要:
#include <math.h>
long double rintl(long double x);

描述:

该函数根据当前舍入模式,将参数x舍入为浮点格式的整数值。

rintl函数和nearbyintl函数区别在于:如果结果和参数x的值不同,rintl函数可能会引发FE_INEXACT浮点异常。


参数:
long double x

参数为一个long double类型的浮点数。


返回值:

函数返回浮点格式表示的舍入得到的整数值。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
/*函数rintl范例*/

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

#pragma STDC FENV_ACCESS ON

/*获取当前舍入模式。*/
void showRoundingDirection(void)
{
    switch(fegetround())
    {
    case FE_DOWNWARD:
        puts("Round downward");
        break;
    case FE_TOWARDZERO:
        puts("Round toward zero");
        break;
    case FE_UPWARD:
        puts("Round upward");
        break;
    default:
        puts("Round to nearest");
        break;
    }
}

int main(void)
{
    long double x = 2.3L, y = 2.7L;

    printf("Default rounding direction: ");
    showRoundingDirection();

    printf("rintl(%.2Lf) = %.2Lf\n", x, rintl(x));
    printf("rintl(%.2Lf) = %.2Lf\n", y, rintl(y));

    fesetround(FE_UPWARD);  //改变当前舍入模式。
    printf("Current rounding direction: ");
    showRoundingDirection();

    printf("rintl(%.2Lf) = %.2Lf\n", x, rintl(x));
    printf("rintl(%.2Lf) = %.2Lf\n", y, rintl(y));

    return 0;
}


输出:

Default rounding direction: Round to nearest

rintl(2.30) = 2.00

rintl(2.70) = 3.00

Current rounding direction: Round upward

rintl(2.30) = 3.00

rintl(2.70) = 3.00


相关内容:
rint double类型的根据当前舍入模式舍入为整数值的函数。
rintf float类型的根据当前舍入模式舍入为整数值的函数。