C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Math rint()

C++ Math rint()

该函数使用当前舍入模式将给定值舍入为最接近的整数。当前的舍入模式由fesetround()确定。
例如:
rint(7.9) = 8;
if, fesetround(FE_DOWNWARD) then,
rint(7.9) = 7;

语法

假设一个数字" x"。语法为:
return_type rint(data_type x);
注意: return_type可以是float,double或long double。

参数

x : 该值可以是float或double。

返回值

它返回x的舍入值。

示例1

让我们看一个简单的示例。
#include <iostream>
#include<math.h>
#include <cfenv>
using namespace std;
int main()
{
    float x=9.9;
    std::cout << "Value of x is :" <<x<< std::endl;
    std::cout << "Rounding to nearest,value is :" <<rint(x)<< std::endl;
   fesetround(FE_UPWARD);
    std::cout << "Rounding to upward,value is :" <<rint(x)<< std::endl;
    fesetround(FE_DOWNWARD);
    std::cout << "Rounding to downward,value is :" <<rint(x)<< std::endl;
    return 0;
}
输出:
Value of x is :9.9
Rounding to nearest,value is :10
Rounding to upward,value is :10
Rounding to downward,value is :9

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4