📜  C++中的lrint()和llrint()

📅  最后修改于: 2021-05-30 05:42:02             🧑  作者: Mango

lrint()函数使用当前的舍入模式将自变量中给出的分数值舍入为整数值。
在这里,当前模式由函数fesetround()确定。

注意:此函数返回long int中的最终值。
句法:

lrint(double a);
lrint(float a);

#代码1

// CPP code to illustrate
// the functionality of lrint()
#include 
#include 
#include 
using namespace std;
  
int main()
{
    int a = 15;
    long int answer;
  
    // setting rounding direction to DOWNWARD
    fesetround(FE_DOWNWARD);
    answer = lrint(a);
    cout << "Downward rounding of " << a 
         << " is " << answer << endl;
  
    return 0;
}

输出 :

Downward rounding of 15 is 15

#代码2

// CPP code to illustrate
// the functionality of lrint()
#include 
#include 
#include 
using namespace std;
  
int main()
{
    double a;
    long int answer;
  
    // By default, the rounding direction
    // is set to 'to-nearest'.
    // fesetround(FE_TONEAREST)
    a = 50.35;
    answer = lrint(a);
    cout << "Nearest rounding of " << a 
         << " is " << answer << endl;
  
    // mid values are rounded off to higher integer
    a = 50.5;
    answer = lrint(a);
    cout << "Nearest rounding of " << a 
         << " is " << answer << endl;
  
    return 0;
}

输出 :

Nearest rounding of 50.35 is 50
Nearest rounding of 50.5 is 50

#代码3

// CPP code to illustrate
// the functionality of lrint()
#include 
#include 
#include 
using namespace std;
  
int main()
{
    double a;
    long int answer;
  
    // Now, the rounding direction
    // is set to UPWARD
    fesetround(FE_UPWARD);
    a = 50.3;
    answer = lrint(a);
    cout << "Upward rounding of " << a 
         << " is " << answer << endl;
  
    // Now, the rounding direction
    //  is set to DOWNWARD
    fesetround(FE_DOWNWARD);
    a = 50.88;
    answer = lrint(a);
    cout << "Downward rounding of " << a 
         << " is " << answer << endl;
  
    return 0;
}

输出 :

Upward rounding of 50.3 is 51
Downward rounding of 50.88 is 50

C++中的llrint()

llrint()函数使用当前的舍入模式将自变量中给出的分数值舍入为整数值。
在这里,当前模式由函数fesetround()确定。

注意:因此,函数以long long int返回值
句法:

llrint(double a);
llrint(float a);

#代码1

// CPP code to illustrate
// the functionality of llrint()
#include 
#include 
#include 
using namespace std;
  
int main()
{
    int a = 15;
    long long int answer;
  
    // setting rounding direction to DOWNWARD
    fesetround(FE_DOWNWARD);
    answer = llrint(a);
    cout << "Downward rounding of " << a 
         << " is " << answer << endl;
  
    return 0;
}

输出 :

Downward rounding of 15 is 15

#代码2

// CPP code to illustrate
// the functionality of llrint()
#include 
#include 
#include 
using namespace std;
  
int main()
{
    double a;
    long long int answer;
  
    // By default, the rounding direction is 
    // set to 'to-nearest'. fesetround(FE_TONEAREST)
    a = 50.35;
    answer = llrint(a);
    cout << "Nearest rounding of " << a 
         << " is " << answer << endl;
  
    // mid values are rounded off to higher integer
    a = 50.5;
    answer = llrint(a);
    cout << "Nearest rounding of " << a 
         << " is " << answer << endl;
  
    return 0;
}

输出 :

Nearest rounding of 50.35 is 50
Nearest rounding of 50.5 is 50

#代码3

// CPP code to illustrate
// the functionality of llrint()
#include 
#include 
#include 
using namespace std;
  
int main()
{
    double a;
    long long int answer;
  
    // Now, the rounding direction
    // is set to UPWARD
    fesetround(FE_UPWARD);
    a = 50.3;
    answer = llrint(a);
    cout << "Upward rounding of " << a 
         << " is " << answer << endl;
  
    // Now, the rounding direction is set to DOWNWARD
    fesetround(FE_DOWNWARD);
    a = 50.88;
    answer = llrint(a);
    cout << "Downward rounding of " << a 
         << " is " << answer << endl;
  
    return 0;
}

输出 :

Upward rounding of 50.3 is 51
Downward rounding of 50.88 is 50
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”