C++ 语言中的 lrint() 和 llrint()
在本节中,我们将了解 C++ 中的 lrint() 和 llrint() 函数。我们首先讨论 lrint() 函数。
lrint() 函数可根据当前舍入模式将参数中给定的分数值舍入为整数。当前模式由 fesetround() 函数确定。>=
lrint() 函数将 double 或 float 或 integer 值作为输入参数,通过将分数部分舍入为整数部分,返回 long int 值。
示例
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
main() {
int x = 40;
long int res;
fesetround(FE_DOWNWARD); // setting rounding direction to DOWNWARD as downward
res = lrint(x);
cout << "Downward rounding of " << x << " is " << res << endl;
}输出
Downward rounding of 40.0235 is 40
llrint() 函数可根据当前舍入模式将参数中给定的分数值舍入为整数。当前模式由 fesetround() 确定。
llrint() 函数将 double 或 float 或 integer 值作为输入参数,通过将分数部分舍入为整数部分,返回 long long int 值。
示例
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
main(){
double a;
long int res;
fesetround(FE_UPWARD); //set rounding direction to upward
a = 40.3;
res = llrint(a);
cout << "Upward rounding of " << a << " is " << res << endl;
fesetround(FE_DOWNWARD); //set rounding direction to downward
a = 40.88;
res = llrint(a);
cout << "Downward rounding of " << a << " is " << res << endl;
}输出
Upward rounding of 40.3 is 41 Downward rounding of 40.88 is 40
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP