C++
数字 | Numerics

std::nearbyint

STD::近字节

Defined in header
float nearbyint( float arg (1)(since C++11)
double nearbyint( double arg (2)(since C++11)
long double nearbyint( long double arg (3)(since C++11)
double nearbyint( Integral arg (4)(since C++11)

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

4%29一组过载或接受任意参数的函数模板积分型等效于%282%29%28的参数转换为double29%。

参数

arg-floating point value

返回值

最近的整数值arg,根据电流舍入模式,被归还。

错误处理

中指定的任何错误都不受此函数的影响。数学[医]错误处理...

如果实现支持ieee浮点算法%28IEC 60559%29,

  • FE_INEXACT永远不会被提起

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

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

  • 如果arg是南,南回来了

注记

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

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

如果当前舍入模式是FE_TONEAREST,此函数在一半情况下会转到%28类似的情况。rint,但不像round29%。

二次

#include <iostream> #include <cmath> #include <cfenv> int main() { #pragma STDC FENV_ACCESS ON std::fesetround(FE_TONEAREST std::cout << "rounding to nearest: \n" << "nearbyint(+2.3) = " << std::nearbyint(2.3) << " nearbyint(+2.5) = " << std::nearbyint(2.5) << " nearbyint(+3.5) = " << std::nearbyint(3.5) << '\n' << "nearbyint(-2.3) = " << std::nearbyint(-2.3) << " nearbyint(-2.5) = " << std::nearbyint(-2.5) << " nearbyint(-3.5) = " << std::nearbyint(-3.5) << '\n'; std::fesetround(FE_DOWNWARD std::cout << "rounding down:\n" << "nearbyint(+2.3) = " << std::nearbyint(2.3) << " nearbyint(+2.5) = " << std::nearbyint(2.5) << " nearbyint(+3.5) = " << std::nearbyint(3.5) << '\n' << "nearbyint(-2.3) = " << std::nearbyint(-2.3) << " nearbyint(-2.5) = " << std::nearbyint(-2.5) << " nearbyint(-3.5) = " << std::nearbyint(-3.5) << '\n'; std::cout << "nearbyint(-0.0) = " << std::nearbyint(-0.0) << '\n' << "nearbyint(-Inf) = " << std::nearbyint(-INFINITY) << '\n'; }

二次

产出:

二次

rounding to nearest: nearbyint(+2.3) = 2 nearbyint(+2.5) = 2 nearbyint(+3.5) = 4 nearbyint(-2.3) = -2 nearbyint(-2.5) = -2 nearbyint(-3.5) = -4 rounding down: nearbyint(+2.3) = 2 nearbyint(+2.5) = 2 nearbyint(+3.5) = 3 nearbyint(-2.3) = -3 nearbyint(-2.5) = -3 nearbyint(-3.5) = -4 nearbyint(-0.0) = -0 nearbyint(-Inf) = -inf

二次

另见

rintlrintllrint (C++11)(C++11)(C++11)nearest integer using current rounding mode with exception if the result differs (function)
roundlroundllround (C++11)(C++11)(C++11)nearest integer, rounding away from zero in halfway cases (function)
fegetroundfesetround (C++11)(C++11)gets or sets rounding direction (function)

c近字节文件

© cppreference.com

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

http://en.cppreference.com/w/cpp/数值/maht/linebyint