在线文档教程
C++
数字 | Numerics

std::rint

STD:Rint,STD::lrint,std::llrint

Defined in header
float rint( float arg (1)(since C++11)
double rint( double arg (2)(since C++11)
long double rint( long double arg (3)(since C++11)
double rint( Integral arg (4)(since C++11)
long lrint( float arg (5)(since C++11)
long lrint( double arg (6)(since C++11)
long lrint( long double arg (7)(since C++11)
long lrint( Integral arg (8)(since C++11)
long long llrint( float arg (9)(since C++11)
long long llrint( double arg (10)(since C++11)
long long llrint( long double arg (11)(since C++11)
long long llrint( Integral arg (12)(since C++11)

1-3%29圈浮点参数arg为浮点格式%29的整数值%28,使用电流舍入模式...

5-7,9-11%29圈浮点参数arg值的整数值,则使用电流舍入模式...

4,8,12%29一组过载或接受任意参数的函数模板积分型。等于%282,6,10%29,分别为%28double29%。

参数

arg-floating point value

返回值

如果没有发生错误,则最近的整数值为arg,根据电流舍入模式,被归还。

错误处理

错误按数学[医]错误处理...

如果...的结果...std::lrintstd::llrint在返回类型可表示的范围之外,可能会发生域错误或范围错误。

如果实现支持ieee浮点算术%28iec 60559%29,则对std::rint职能:

  • 如果arg是±∞,它被返回,未经修改

  • 如果arg是±0,它被返回,未经修改

  • 如果arg是南,南回来了

std::lrintstd::llrint职能:

  • 如果arg是±∞,FE_INVALID引发,并返回实现定义的值。

  • 如果舍入的结果超出了返回类型的范围,FE_INVALID引发,并返回实现定义的值。

  • 如果arg是南FE_INVALID引发,并返回实现定义的值。

注记

POSIX指定所有案件std::lrintstd::llrint提高FE_INEXACT是域错误。

如数学[医]错误处理,,,FE_INEXACT可能为%28,但%27T是否需要在非ieee浮点平台上由std::rint当舍入一个非整数的有限值时。

唯一的区别是std::rintstd::nearbyint那是std::nearbyint不养FE_INEXACT...

最大的可表示浮点值是所有标准浮点格式的精确整数,因此std::rint永远不要自己溢出,但是结果可能会溢出任何整数类型%28,包括std::intmax_t%29,存储在整数变量中。

如果当前舍入模式是..。

  • FE_DOWNWARD,然后std::rint等于std::floor...

  • FE_UPWARD,然后std::rint等于std::ceil...

  • FE_TOWARDZERO,然后std::rint等于std::trunc

  • FE_TONEAREST,然后std::rint不同于std::round在这中间的情况是四舍五入,而不是远离零。

二次

#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'; // error handling 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

二次

另见

trunc (C++11)nearest integer not greater in magnitude than the given value (function)
nearbyint (C++11)nearest integer using current rounding mode (function)
fegetroundfesetround (C++11)(C++11)gets or sets rounding direction (function)

c Rint文件

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/NUMERIC/MATH/Rint