English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C++中的rint()函数使用当前的舍入模式将参数舍入为整数值。当前的舍入模式由函数fesetround()确定。
double rint(double x); float rint(float x); long double rint(long double x); double rint(T x); // 为整型
rint()函数采用单个参数,并返回double,float或long double类型的值。此函数在<cmath>头文件中定义。
rint()函数采用单个参数值取整。
rint()函数使用fegetround()指定的舍入方向将参数x舍入为整数值,并返回该值。默认情况下,舍入方向设置为“最接近”。可以使用fesetround()函数将舍入方向设置为其他值。
#include <iostream> #include <cmath> #include <cfenv> using namespace std; int main() { // 默认情况下,舍入方向为最接近的方向,即fesetround(FE_TONEAREST) double x = 11.87, result; result = rint(x); cout << "四舍五入到最近 (" << x << ") = " << result << endl; // 中间值取较高值 x = 11.5; result = rint(x); cout << "四舍五入到最近 (" << x << ") = " << result << endl; // ضع اتجاه التحجيم على DOWNWARD fesetround(FE_DOWNWARD); x = 11.87; result = rint(x); cout << "التحجيم إلى الأسفل (" << x << ") = " << result << endl; // 将舍入方向设置为UPWARD fesetround(FE_UPWARD); x = 33.32; result = rint(x); cout << "向上舍入 (" << x << ") = " << result << endl; return 0; }
عند تشغيل البرنامج، الناتج هو:
四舍五入到最近 (11.87) = 12 四舍五入到最近 (11.5) = 12 向下舍入 (11.8699) = 11 向上舍入 (33.3201) = 34
#include <iostream> #include <cmath> #include <cfenv> using namespace std; int main() { int x = 15; double result; // ضع اتجاه التحجيم على DOWNWARD fesetround(FE_DOWNWARD); result = rint(x); cout << "التحجيم إلى الأسفل (" << x << ") = " << result << endl; return 0; }
عند تشغيل البرنامج، الناتج هو:
التحجيم إلى الأسفل (15) = 15
بالنسبة للأرقام الكاملة، سيقوم funkce rint بتقديم نفس القيمة المدخلة. لذلك، عادةً لا يتم استخدامه للقيم الكاملة.