首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::fegetround

Defined in header <cfenv>

?

?

int fesetround( int round )

(1)

(since C++11)

int fegetround()

(2)

(since C++11)

管理浮点舍入方向。

1%29尝试建立与参数相等的浮点舍入方向。round,这将是浮点四舍五入宏...

2%29返回浮点舍入宏它对应于当前的舍入方向。

参数

round

-

rounding direction, one of floating point rounding macros

返回值

1%29?0?在成功的时候,不是零,否则。

2%29浮点舍入宏描述当前的四舍五入方向或一个负值,如果无法确定该方向。

注记

当前的舍入模式,反映了最近的fesetround,也可以用FLT_ROUNDS...

见浮点四舍五入宏四舍五入的效果。

二次

代码语言:javascript
复制
#include <cmath>
#include <cfenv>
#include <iostream>
 
int main()
{
    #pragma STDC FENV_ACCESS ON
    std::fesetround(FE_DOWNWARD);
    std::cout << "rounding using FE_DOWNWARD: \n" << std::fixed
              << " 12.0 ->  " << std::nearbyint(12.0) << '\n'
              << " 12.1 ->  " << std::nearbyint(12.1) << '\n'
              << "-12.1 -> " << std::nearbyint(-12.1) << '\n'
              << " 12.5 ->  " << std::nearbyint(12.5) << '\n'
              << " 12.9 ->  " << std::nearbyint(12.9) << '\n'
              << "-12.9 -> " << std::nearbyint(-12.9) << '\n'
              << " 13.0 ->  " << std::nearbyint(13.0) << '\n';
    std::fesetround(FE_TONEAREST);
    std::cout << "rounding using FE_TONEAREST: \n"
              << " 12.0 ->  " << std::nearbyint(12.0) << '\n'
              << " 12.1 ->  " << std::nearbyint(12.1) << '\n'
              << "-12.1 -> " << std::nearbyint(-12.1) << '\n'
              << " 12.5 ->  " << std::nearbyint(12.5) << '\n'
              << " 12.9 ->  " << std::nearbyint(12.9) << '\n'
              << "-12.9 -> " << std::nearbyint(-12.9) << '\n'
              << " 13.0 ->  " << std::nearbyint(13.0) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
rounding using FE_DOWNWARD:
 12.0 ->  12.000000
 12.1 ->  12.000000
-12.1 -> -13.000000
 12.5 ->  12.000000
 12.9 ->  12.000000
-12.9 -> -13.000000
 13.0 ->  13.000000
rounding using FE_TONEAREST: 
 12.0 ->  12.000000
 12.1 ->  12.000000
-12.1 -> -12.000000
 12.5 ->  12.000000
 12.9 ->  13.000000
-12.9 -> -13.000000
 13.0 ->  13.000000

二次

另见

nearbyint (C++11)

nearest integer using current rounding mode (function)

rintlrintllrint (C++11)(C++11)(C++11)

nearest integer using current rounding mode with exception if the result differs (function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com