Related Tags

c++

# How to use lrint() in C++

Ayesha Naeem

The lrint() function takes in a number and rounds it to the nearest integral value. Then, it returns the rounded value.

To use the lrint() function, the cmath header file needs to be included in the program:

#include <cmath>


Rounding is completed through the use of a current rounding mode that is specified using fesetround().

If no mode is specified, rounding is completed using the default rounding modeto-nearest. Other modes are FE_DOWNWARD and FE_UPWARD.

## Rounding modes

Mode Description
FE_UPWARD The number is rounded up to the next integral. For instance, 36.3 is rounded up to 37.
FE_DOWNWARD The number is rounded down to the previous integral. For instance, 32.8 is rounded down to 32.
to-nearest The number is rounded to the nearest integral. For instance, 32.7 is rounded up to 33, 32.5 is rounded up to 33, and 32.3 is rounded down to 32.

## Parameters

lrint() only accepts one parameter, but it can be any of the following types:

• float
• double
• long double
• T for integral types

## Return value

The lrint() function always returns a value of type long int.

## Examples

The code below shows the execution of the lrint() function using to-nearest, FE_UPWARD, and FE_DOWNWARD rounding modes.

For integral values, the lrint() function returns the same integral value as the input.

#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;

int main() {
double x= 128.8;
// the answer is rounded to nearest integral by default.
cout << x <<" rounded to nearest integral is " << answer << endl;

// Values halfway from bigger and smaller integral are rounded off to the bigger one.
x = 128.5;
cout << x <<" rounded to nearest integral is " << answer << endl;

// setting rounding mode to DOWNWARD rounds the value to the smaller integral
fesetround(FE_DOWNWARD);
x = 128.74;
cout << x <<" rounded downwards is " << answer << endl;

// setting rounding mode to UPWARD rounds th value to the bigger integral
fesetround(FE_UPWARD);
x = 33.32;
cout << x <<" rounded upwards is " << answer << endl;

return 0;
}

RELATED TAGS

c++

CONTRIBUTOR

Ayesha Naeem
{"ft_lang_label":"__label__en","ft_lang_prob":0.7943624,"math_prob":0.9785526,"size":2068,"snap":"2022-27-2022-33","text_gpt3_token_len":534,"char_repetition_ratio":0.16812016,"word_repetition_ratio":0.14245014,"special_character_ratio":0.27562863,"punctuation_ratio":0.14249364,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9947853,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-13T22:20:40Z\",\"WARC-Record-ID\":\"<urn:uuid:32461c18-c975-42be-bdac-7cbf0c7bb7d0>\",\"Content-Length\":\"415656\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:00f191a4-2f50-43b3-960f-a047ea3c789d>\",\"WARC-Concurrent-To\":\"<urn:uuid:635d583f-aff1-447b-bd0f-aa16109d81f5>\",\"WARC-IP-Address\":\"104.18.3.119\",\"WARC-Target-URI\":\"https://www.educative.io/answers/how-to-use-lrint-in-cpp\",\"WARC-Payload-Digest\":\"sha1:4ANRY3TGRGCQI3UBPJ6VSPHVX2N4I7PO\",\"WARC-Block-Digest\":\"sha1:WW3OTI4FLR5OHCLXJBBIVHYEOUGHCZDC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571987.60_warc_CC-MAIN-20220813202507-20220813232507-00628.warc.gz\"}"}