C ++中的Rint(),rintf(),rintl()
在这里,我们将看到三个功能。这些功能Rint(),rintf()和rintl()。这些函数用于将浮点值转换为舍入格式。
该rint()方法
此函数用于将浮点值四舍五入为整数。语法如下。如果结果超出返回类型,则可能会发生域错误。当参数为0或无穷大时,它将返回未修改的
float rint(float argument) double rint(double argument) long double rint(long double argument)
示例
#include <cmath>
#include <iostream>
using namespace std;
main() {
double a, b, x, y;
x = 53.26;
y = 53.86;
a = rint(x);
b = rint(y);
cout << "The value of a: " << a << endl;
cout << "The value of b: " << b;
}输出结果
The value of a: 53 The value of b: 54
该rintf()方法
这与rint相同,但是唯一的区别是所有参数和返回类型都是浮点类型。在此示例中,我们将使用fesetround()方法指定舍入类型。
float rintf(float argument)
示例
#include <cmath>
#include <fenv.h>
#include <iostream>
using namespace std;
main() {
double a, b, x, y;
x = 53.26;
y = 53.86;
fesetround(FE_UPWARD);
a = rintf(x);
fesetround(FE_DOWNWARD);
b = rintf(y);
cout << "The value of a: " << a << endl;
cout << "The value of b: " << b;
}输出结果
The value of a: 54 The value of b: 53
该rintl()方法
这与rint相同,但是唯一的区别是所有参数和返回类型均为longdouble类型。在此示例中,我们将使用fesetround()方法指定舍入类型。
long double rintl(long double argument)
示例
#include <cmath>
#include <fenv.h>
#include <iostream>
using namespace std;
main() {
double a, b, x, y;
x = 53547.55555555555;
y = 53547.55555555523;
fesetround(FE_UPWARD);
a = rintl(x);
fesetround(FE_DOWNWARD);
b = rintl(y);
cout << "The value of a: " << a << endl;
cout << "The value of b: " << b;
}输出结果
The value of a: 53548 The value of b: 53547
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短