std::rint, std::rintf, std::rintl, std::lrint, std::lrintf, std::lrintl, std::llrint, std::llrintf

来自cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本运算
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数函数
(C++11)
(C++11)
(C++11)
(C++11)
幂函数
(C++11)
(C++11)
三角与双曲函数
(C++11)
(C++11)
(C++11)
误差与伽马函数
(C++11)
(C++11)
(C++11)
(C++11)
临近整数的浮点运算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
rintlrintllrint
(C++11)(C++11)(C++11)
浮点操作函数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分类/比较
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
宏常量
(C++11)(C++11)(C++11)(C++11)(C++11)
 
定义于头文件 <cmath>
float       rint ( float arg );
float       rintf( float arg );
(1) (C++11 起)
double      rint ( double arg );
(2) (C++11 起)
long double rint ( long double arg );
long double rintl( long double arg );
(3) (C++11 起)
double      rint ( IntegralType arg );
(4) (C++11 起)
long lrint ( float arg );
long lrintf( float arg );
(5) (C++11 起)
long lrint ( double arg );
(6) (C++11 起)
long lrint ( long double arg );
long lrintl( long double arg );
(7) (C++11 起)
long lrint ( IntegralType arg );
(8) (C++11 起)
long long llrint ( float arg );
long long llrintf( float arg );
(9) (C++11 起)
long long llrint ( double arg );
(10) (C++11 起)
long long llrint ( long double arg );
long long llrintl( long double arg );
(11) (C++11 起)
long long llrint ( IntegralType arg );
(12) (C++11 起)
1-3)当前舍入模式,舍入浮点参数 arg 为整数值(以浮点格式)。
5-7, 9-11)当前舍入模式,舍入浮点参数 arg 为整数值。
4,8,12) 接受任何整数类型参数的重载集或函数模板。分别等价于 (2,6,10) (将参数转型为 double )。

参数

arg - 浮点值

返回值

若不出现错误,则返回 arg 按照当前舍入模式的最接近整数值。

错误处理

报告 math_errhandling 中指定的错误。

std::lrintstd::llrint 的结果在返回类型的可表示范围外,则可能出现定义域错误或值域错误。

若实现支持 IEEE 浮点算术( IEC 60559 ),则

对于 std::rint 函数:
  • arg 为 ±∞ ,则返回不修改的参数
  • arg 为 ±0 ,则返回不修改的参数
  • arg 为 NaN ,则返回 NaN
对于 std::lrintstd::llrint 函数:
  • arg 为 ±∞ ,则引发 FE_INVALID 并返回实现定义值
  • 若舍入结果在返回类型范围外,则引发 FE_INVALID 并返回实现定义值
  • arg 为 NaN ,则引发 FE_INVALID 并返回实现定义值

注意

POSIX 指定 std::lrintstd::llrint 引发 FE_INEXACT 的所有情况都是定义域错误。

math_errhandling 中指定, std::rint 在舍入非整数有限值时可以(但不在非 IEEE 浮点平台上要求)引发 FE_INEXACT

std::rintstd::nearbyint 间仅有的区别是 std::nearbyint 决不引发 FE_INEXACT

所有标准浮点格式中,最大可表示浮点值都是准确的整数,故 std::rint 自身决不上溢;然而在存储结果于整数对象时,结果可能溢出任何整数类型(包含 std::intmax_t )。

若当前舍入模式为……

示例

#include <iostream>
#include <cmath>
#include <cfenv>
#include <climits>
 
int main()
{
#pragma STDC FENV_ACCESS ON
    std::fesetround(FE_TONEAREST);
    std::cout << "rounding to nearest (halfway cases to even):\n"
              << "rint(+2.3) = " << std::rint(2.3)
              << "  rint(+2.5) = " << std::rint(2.5)
              << "  rint(+3.5) = " << std::rint(3.5) << '\n'
              << "rint(-2.3) = " << std::rint(-2.3)
              << "  rint(-2.5) = " << std::rint(-2.5)
              << "  rint(-3.5) = " << std::rint(-3.5) << '\n';
 
    std::fesetround(FE_DOWNWARD);
    std::cout << "rounding down:\n" 
              << "rint(+2.3) = " << std::rint(2.3)
              << "  rint(+2.5) = " << std::rint(2.5)
              << "  rint(+3.5) = " << std::rint(3.5) << '\n'
              << "rint(-2.3) = " << std::rint(-2.3)
              << "  rint(-2.5) = " << std::rint(-2.5)
              << "  rint(-3.5) = " << std::rint(-3.5) << '\n'
              << "rounding down with lrint\n" 
              << "lrint(+2.3) = " << std::lrint(2.3)
              << "  lrint(+2.5) = " << std::lrint(2.5)
              << "  lrint(+3.5) = " << std::lrint(3.5) << '\n'
              << "lrint(-2.3) = " << std::lrint(-2.3)
              << "  lrint(-2.5) = " << std::lrint(-2.5)
              << "  lrint(-3.5) = " << std::lrint(-3.5) << '\n';
 
    std::cout << "lrint(-0.0) = " << std::lrint(-0.0)  << '\n'
              << "lrint(-Inf) = " << std::lrint(-INFINITY) << '\n';
 
    // 错误处理
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "std::rint(0.1) = " << std::rint(.1) << '\n';
    if (std::fetestexcept(FE_INEXACT))
              std::cout << "    FE_INEXACT was raised\n";
 
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "std::lrint(LONG_MIN-2048.0) = "
              << std::lrint(LONG_MIN-2048.0) << '\n';
    if (std::fetestexcept(FE_INVALID))
              std::cout << "    FE_INVALID was raised\n";
}

可能的输出:

rounding to nearest (halfway cases to even): 
rint(+2.3) = 2  rint(+2.5) = 2  rint(+3.5) = 4
rint(-2.3) = -2  rint(-2.5) = -2  rint(-3.5) = -4
rounding down:
rint(+2.3) = 2  rint(+2.5) = 2  rint(+3.5) = 3
rint(-2.3) = -3  rint(-2.5) = -3  rint(-3.5) = -4
rounding down with lrint
lrint(+2.3) = 2  lrint(+2.5) = 2  lrint(+3.5) = 3
lrint(-2.3) = -3  lrint(-2.5) = -3  lrint(-3.5) = -4
lrint(-0.0) = 0
lrint(-Inf) = -9223372036854775808
std::rint(0.1) = 0
    FE_INEXACT was raised
std::lrint(LONG_MIN-2048.0) = -9223372036854775808
    FE_INVALID was raised

参阅

(C++11)(C++11)(C++11)
绝对值不大于给定值的最接近整数
(函数)
(C++11)(C++11)(C++11)
使用当前舍入模式的最接近整数
(函数)
(C++11)(C++11)
获取或设置舍入方向
(函数)