Namespaces
Variants
Views
Actions

std::rint, std::lrint, std::llrint

From cppreference.com
< cpp‎ | numeric‎ | math
Revision as of 21:26, 2 November 2012 by P12bot (Talk | contribs)

 
 
 
Common mathematical functions
Functions
Basic operations
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Exponential functions
(C++11)
(C++11)
(C++11)
(C++11)
Power functions
(C++11)
(C++11)
Trigonometric and hyperbolic functions
(C++11)
(C++11)
(C++11)
Error and gamma functions
(C++11)
(C++11)
(C++11)
(C++11)
Nearest integer floating point operations
(C++11)(C++11)(C++11)
(C++11)
(C++11)
rintlrintllrint
(C++11)(C++11)(C++11)
Floating point manipulation functions
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
Classification/Comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Macro constants
(C++11)(C++11)(C++11)(C++11)(C++11)
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <cmath>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
int rint( float arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
int rint( double arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
int rint( long double arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
int rint( Integral arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
long lrint( float arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
long lrint( double arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
long lrint( long double arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
long lrint( Integral arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
long long llrint( float arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
long long llrint( double arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
long long llrint( long double arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
long long llrint( Integral arg );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode. If the result differs from arg (i.e., arg was not an integer value already), the floating-point exception FE_INEXACT is raised.

Contents

Parameters

arg - floating point value

Return value

The integer result of rounding arg

Notes

The only difference between std::nearbyint and std::rint is that std::rint may raise the FE_INEXACT floating-point exception, while std::nearbyint never raises it.

Example

#include <cmath>
#include <cfenv>
#include <iostream>
int main()
{
    #pragma STDC FENV_ACCESS ON
    std::fesetround(FE_DOWNWARD);
    std::cout << "rounding using FE_DOWNWARD:\n" << std::fixed
              << " 12.0 ->  " << std::rint(12.0) << '\n'
              << " 12.1 ->  " << std::rint(12.1) << '\n'
              << "-12.1 -> " << std::rint(-12.1) << '\n'
              << " 12.5 ->  " << std::rint(12.5) << '\n'
              << " 12.9 ->  " << std::rint(12.9) << '\n'
              << "-12.9 -> " << std::rint(-12.9) << '\n'
              << " 13.0 ->  " << std::rint(13.0) << '\n';
    std::fesetround(FE_TONEAREST);
    std::cout << "rounding using FE_TONEAREST: \n"
              << " 12.0 ->  " << std::rint(12.0) << '\n'
              << " 12.1 ->  " << std::rint(12.1) << '\n'
              << "-12.1 -> " << std::rint(-12.1) << '\n'
              << " 12.5 ->  " << std::rint(12.5) << '\n'
              << " 12.9 ->  " << std::rint(12.9) << '\n'
              << "-12.9 -> " << std::rint(-12.9) << '\n'
              << " 13.0 ->  " << std::rint(13.0) << '\n';
 
    std::cout << "When rounding 12.0 ";
    std::feclearexcept(FE_ALL_EXCEPT);
    std::rint(12.0);
    if(std::fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) {
        std::cout << "inexact result reported\n";
    } else {
        std::cout << "inexact result not reported\n";
    }
 
    std::cout << "When rounding 12.1 ";
    std::rint(12.1);
    if(std::fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) {
        std::cout << "inexact result reported\n";
    } else {
        std::cout << "inexact result not reported\n";
    }
}

Output:

rounding using FE_DOWNWARD:
 12.0 ->  12.000000
 12.1 ->  12.000000
-12.1 -> -13.000000
 12.5 ->  12.000000
 12.9 ->  12.000000
-12.9 -> -13.000000
 13.0 ->  13.000000
rounding using FE_TONEAREST: 
 12.0 ->  12.000000
 12.1 ->  12.000000
-12.1 -> -12.000000
 12.5 ->  12.000000
 12.9 ->  13.000000
-12.9 -> -13.000000
 13.0 ->  13.000000
When rounding 12.0 inexact result not reported
When rounding 12.1 inexact result reported

See also

Template:cpp/numeric/math/dcl list nearbyintTemplate:cpp/numeric/math/dcl list truncTemplate:cpp/numeric/fenv/dcl list feround