English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

دليل الأساسيات في C++

تحكم في العملية في C++

الوظائف في C++

مجموعات C++ و سلاسل

هياكل بيانات C++

فئات C++ و أوبجكتات

المراجع في C++

الوراثة في C++

دليل STL في C++

دليل مرجع C++

استخدام وظيفة lrint() في C++ و مثال

وظائف المكتبة <cmath> في C++

The lrint() function in C++ uses the current rounding mode to round the parameter to an integer value.

The lrint() function in C++ uses the current rounding mode to round the parameter to an integer value. The current rounding mode is determined by the fesetround() function. It is similar torint(), but returns long int.

lrint() prototype [from C++11 standard]

long int lrint(double x);
long int lrint(float x);
long int lrint(long double x);
long int lrint(T x); // for integer

The lrint() function takes a single parameter and returns the value long int of type. This function is in<cmath>Defined in the header file.

lrint() parameter

The lrint() function takes a single parameter and truncates the value.

lrint() return value

The lrint() function rounds the parameter x to an integer value using the rounding direction specified by fegetround() and returns the value long int.

By default, the rounding direction is set to "nearest". You can use the fesetround() function to set the rounding direction to other values.

示例1: How does lrint() work in C++?

#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;
int main()
{
    // By default, the rounding direction is the nearest direction, i.e., fesetround(FE_TONEAREST)
    double x = 11.87;
    long int result;
    result = lrint(x);
    cout << "Rounding to the nearest (" << x << ") = " << result << endl;
    
    // The middle value is rounded up to the higher integer
    x = 11.5;
    result = lrint(x);
    cout << "Rounding to the nearest (" << x << ") = " << result << endl;
    //ضبط اتجاه التراجع إلى الأسفل
    fesetround(FE_DOWNWARD);
    x = 11.87;
    result = lrint(x);
    cout << "التراجع إلى الأسفل (" << x << ") = " << result << endl;
    
    // Set the rounding direction to UPWARD
    fesetround(FE_UPWARD);
    x = 33.32;
    result = lrint(x);
    cout << "Rounding up (" << x << ") = " << result << endl;
    
    return 0;
}

عند تشغيل البرنامج، الناتج هو:

Rounding to the nearest (11.87) = 12
Rounding to the nearest (11.5) = 12
Rounding down (11.8699) = 11
Rounding up (33.3201) = 34

示例2: integer type lrint() function

#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;
int main()
{
    int x = 15;
    long int result;
    
    //ضبط اتجاه التراجع إلى الأسفل
    fesetround(FE_DOWNWARD);
    result = lrint(x);
    cout << "التراجع إلى الأسفل (" << x << ") = " << result << endl;
    return 0;
}

عند تشغيل البرنامج، الناتج هو:

التراجع إلى الأسفل (15) = 15

بالنسبة للقيم الكاملة، يعيد وظيفة lrint القيمة نفسها التي تم إدخالها. لذلك، لا تستخدم عادة لتقديم القيم الكاملة.

وظائف المكتبة <cmath> في C++