我是靠谱客的博主 迷你烤鸡,最近开发中收集的这篇文章主要介绍rounding mode什么是rounding moderounding mode在C99中的定义示例,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

文章目录

  • 什么是rounding mode
  • rounding mode在C99中的定义
  • 示例
    • 运行结果

什么是rounding mode

c语言中rounding mode(rounding direction)的总结(基于C99标准)。所谓rounding mode就是常说的舍入模式,例如,四舍五入、向上取整、向下取整。舍入模式决定了当浮点运算的结果不能在有效值中精确表示时应该如何处理。

rounding mode在C99中的定义

FLT_ROUNDS表示浮点运算的舍入模式。FLT_ROUNDS的值如下图(from C99-N1256)所示:

FLT_ROUNDS

toward zero表示运算后的结果向0值方向靠近;

to nearest表示运算后的结果向最近的整数方向靠近;

toward positive infinity 表示正无穷方向;

toward negative infinity 表示负无穷方向;

标准中规定,FLT_ROUNDS的值要正确的反应当前的rounding mode但是在glibc 2.1版本中,fesetround()并不能正确地改变FLT_ROUNDS的值,FLT_ROUNDS的值始终为1FLT_ROUNDS在<float.h>中定义。

标准中规定,只有fegetround()fesetround()实现后,FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO表示4种rounding mode。FE_*宏在<fenv.h>中定义。

在这里插入图片描述

FE_TOWARDZERO等价于FLT_ROUNDStoward zero;

FE_TONEAREST等价于FLT_ROUNDSto nearest;

FE_UPWARD等价于FLT_ROUNDStoward positive infinity;

FE_DOWNWARD等价于FLT_ROUNDStoward negative infinity;

示例

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

int main(int argc, char** argv)
{
    double a = 1.2; 
    double b = -1.2; 
    double c = -1.5; 
    double d = -1.51; 
    double e = 1.5; 
    double f = 1.51; 

    printf("FE_TONEAREST=%dn", FE_TONEAREST);
    printf("FE_UPWARD=%dn", FE_UPWARD);
    printf("FE_DOWNWARD=%dn", FE_DOWNWARD);
    printf("FE_TOWARDZERO=%dn", FE_TOWARDZERO);
    printf("n");

    printf("current rouding mode:%dn",fegetround());
    printf("FLT_ROUNDS=%dn", FLT_ROUNDS);

    printf("rint(%f)=%fn", a, rint(a));
    printf("rint(%f)=%fn", b, rint(b));
    printf("rint(%f)=%fn", c, rint(c));
    printf("rint(%f)=%fn", e, rint(e));
    printf("rint(%f)=%fn", f, rint(f));
    printf("rint(%f)=%fn", f, rint(f));

    printf("1-after set rounding moden");
    fesetround(FE_TOWARDZERO);
    printf("current rouding mode:%dn",fegetround());
    printf("FLT_ROUNDS=%dn", FLT_ROUNDS);
    printf("rint(%f)=%fn", a, rint(a));
    printf("rint(%f)=%fn", b, rint(b));
    printf("rint(%f)=%fn", c, rint(c));
    printf("rint(%f)=%fn", e, rint(e));
    printf("rint(%f)=%fn", f, rint(f));
    printf("rint(%f)=%fn", f, rint(f));

    printf("2-after set rounding moden");
    fesetround(FE_UPWARD);
    printf("current rouding mode:%dn",fegetround());
    printf("FLT_ROUNDS=%dn", FLT_ROUNDS);
    printf("rint(%f)=%fn", a, rint(a));
    printf("rint(%f)=%fn", b, rint(b));
    printf("rint(%f)=%fn", c, rint(c));
    printf("rint(%f)=%fn", e, rint(e));
    printf("rint(%f)=%fn", f, rint(f));
    printf("rint(%f)=%fn", f, rint(f));

    printf("3-after set rounding moden");
    fesetround(FE_DOWNWARD);
    printf("current rouding mode:%dn",fegetround());
    printf("FLT_ROUNDS=%dn", FLT_ROUNDS);
    printf("rint(%f)=%fn", a, rint(a));
    printf("rint(%f)=%fn", b, rint(b));
    printf("rint(%f)=%fn", c, rint(c));
    printf("rint(%f)=%fn", e, rint(e));
    printf("rint(%f)=%fn", f, rint(f));
    printf("rint(%f)=%fn", f, rint(f));

    return 0;
}


rint()函数的作用是 按rounding direction取整。

运行结果

$ ./a.out
FE_TONEAREST=0
FE_UPWARD=2048
FE_DOWNWARD=1024
FE_TOWARDZERO=3072

current rouding mode:0 # 说明,默认的rounding mode是to nearest
FLT_ROUNDS=1
rint(1.200000)=1.000000
rint(-1.200000)=-1.000000
rint(-1.500000)=-2.000000
rint(1.500000)=2.000000
rint(1.510000)=2.000000
rint(1.510000)=2.000000
1-after set rounding mode
current rouding mode:3072 # 设置为 toword zero
FLT_ROUNDS=1 # FLT_ROUNDS的值没有变化
rint(1.199999)=1.000000
rint(-1.199999)=-1.000000
rint(-1.500000)=-1.000000
rint(1.500000)=1.000000
rint(1.510000)=1.000000
rint(1.510000)=1.000000
2-after set rounding mode
current rouding mode:2048 # 设置为 upward
FLT_ROUNDS=1 # FLT_ROUNDS的值没有变化
rint(1.200000)=2.000000
rint(-1.199999)=-1.000000
rint(-1.500000)=-1.000000
rint(1.500000)=2.000000
rint(1.510001)=2.000000
rint(1.510001)=2.000000
3-after set rounding mode
current rouding mode:1024 # 设置为 downward
FLT_ROUNDS=1 # FLT_ROUNDS的值没有变化
rint(1.199999)=1.000000
rint(-1.200000)=-2.000000
rint(-1.500000)=-2.000000
rint(1.500000)=1.000000
rint(1.510000)=1.000000
rint(1.510000)=1.000000

最后

以上就是迷你烤鸡为你收集整理的rounding mode什么是rounding moderounding mode在C99中的定义示例的全部内容,希望文章能够帮你解决rounding mode什么是rounding moderounding mode在C99中的定义示例所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部