📜  C++ fesetround()

📅  最后修改于: 2020-09-25 07:17:02             🧑  作者: Mango

C++中的fesetround() 函数尝试设置指定的浮点舍入方向,该方向应该是浮点舍入宏之一。

fesetround() 函数在头文件中定义。

fesetround()原型

int fesetround( int round );

此函数采用单个参数round ,它是浮点舍入宏之一。然后,它试图设置round作为浮点舍入方向。

fesetround()参数

fesetround()返回值

示例:fesetround() 函数如何工作?

#include 
#include 
#include 
#pragma STDC FENV_ACCESS ON
using namespace std;

void print_current_rounding_direction()
{
    cout << "Current rounding method: ";
    switch (fegetround()) {
        case FE_TONEAREST:
            cout << "FE_TONEAREST";
            break;
        case FE_DOWNWARD:
            cout << "FE_DOWNWARD";
            break;
        case FE_UPWARD:
            cout << "FE_UPWARD";
            break;
        case FE_TOWARDZERO:
            cout << "FE_TOWARDZERO";
            break;
        default:
            cout << "unknown";
    };
    cout << endl;
}

int main()
{
    print_current_rounding_direction();
    cout << "6.2 -> " << rint(6.2) << endl;
    cout << "18.7 -> " << rint(18.7) << endl;

    fesetround(FE_UPWARD);
    print_current_rounding_direction();
    cout << "6.2 -> " << rint(6.2) << endl;
    cout << "19.7 -> " << rint(19.7) << endl;
    
    fesetround(FE_DOWNWARD);
    print_current_rounding_direction();
    cout << "6.2 -> " << rint(6.2) << endl;
    cout << "19.7 -> " << rint(19.7) << endl;
    
    return 0;
}

运行该程序时,输出为:

Current rounding method: FE_TONEAREST
6.2 -> 6
18.7 -> 19
Current rounding method: FE_UPWARD
6.2 -> 7
19.7 -> 20
Current rounding method: FE_DOWNWARD
6.2 -> 6
19.7 -> 19