std::fegetround, std::fesetround
定义于头文件 <cfenv>
|
||
int fesetround( int round ) |
(1) | (C++11 起) |
int fegetround() |
(2) | (C++11 起) |
管理浮点舍入方向。
2) 返回对应当前舍入方向的浮点舍入宏。
参数
round | - | 舍入方向,浮点舍入宏之一 |
返回值
1) 成功时为 0 ,否则为非零。
2) 描述当前舍入方向的浮点舍入宏,或若不能确定方向则为负值
注意
当前舍入方向反映最近的 fesetround
的效果,亦能以 FLT_ROUNDS 查询。
舍入的效果见浮点舍入宏。
示例
运行此代码
#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'; }
输出:
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
参阅
(C++11)(C++11)(C++11) |
使用当前舍入模式的最接近整数 (函数) |
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11) |
使用当前舍入模式的最接近整数 若结果有别则有异常 (函数) |