C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 常用数学函数
- std::abs(int), std::labs, std::llabs, std::imaxabs
- std::div, std::ldiv, std::lldiv
- std::fmod, std::fmodf, std::fmodl
- std::remainder, std::remainderf, std::remainderl
- std::remquo, std::remquof, std::remquol
- std::hypot, std::hypotf, std::hypotl
- std::abs(float), std::fabs, std::fabsf, std::fabsl
- std::sqrt, std::sqrtf, std::sqrtl
- std::sin, std::sinf, std::sinl
- std::fma, std::fmaf, std::fmal
- std::fmax, std::fmaxf, std::fmaxl
- std::fmin, std::fminf, std::fminl
- std::fdim, std::fdimf, std::fdiml
- NAN
- std::exp, std::expf, std::expl
- std::exp2, std::exp2f, std::exp2l
- std::expm1, std::expm1f, std::expm1l
- std::log, std::logf, std::logl
- std::log10, std::log10f, std::log10l
- std::log1p, std::log1pf, std::log1pl
- std::log2, std::log2f, std::log2l
- std::cbrt, std::cbrtf, std::cbrtl
- std::pow, std::powf, std::powl
- std::cos, std::cosf, std::cosl
- std::tan, std::tanf, std::tanl
- std::asin, std::asinf, std::asinl
- std::acos, std::acosf, std::acosl
- std::atan, std::atanf, std::atanl
- std::atan2, std::atan2f, std::atan2l
- std::sinh, std::sinhf, std::sinhl
- std::cosh, std::coshf, std::coshl
- std::tanh, std::tanhf, std::tanhl
- std::asinh, std::asinhf, std::asinhl
- std::acosh, std::acoshf, std::acoshl
- std::atanh, std::atanh, std::atanhl
- std::erf, std::erff, std::erfl
- std::erfc, std::erfcf, std::erfcl
- std::lgamma, std::lgammaf, std::lgammal
- std::tgamma, std::tgammaf, std::tgammal
- std::ceil, std::ceilf, std::ceill
- std::floor, std::floorf, std::floorl
- std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl, std::llround, std::llroundf
- std::trunc, std::truncf, std::truncl
- std::nearbyint, std::nearbyintf, std::nearbyintl
- std::rint, std::rintf, std::rintl, std::lrint, std::lrintf, std::lrintl, std::llrint, std::llrintf
- std::ldexp, std::ldexpf, std::ldexpl
- std::scalbn, std::scalbnf, std::scalbnl, std::scalbln, std::scalblnf, std::scalblnl
- std::ilogb, std::ilogbf, std::ilogbl
- std::logb, std::logbf, std::logbl
- std::frexp, std::frexpf, std::frexpl
- std::modf, std::modff, std::modfl
- std::nextafter, std::nextafterf, std::nextafterl, std::nexttoward, std::nexttowardf, std::nexttowardl
- std::copysign, std::copysignf, std::copysignl
- std::fpclassify
- std::isfinite
- std::isinf
- std::isnan
- std::isnormal
- std::signbit
- std::isgreater
- std::isgreaterequal
- std::isless
- std::islessequal
- std::islessgreater
- std::isunordered
- HUGE_VALF, HUGE_VAL, HUGE_VALL
- INFINITY
- MATH_ERRNO, MATH_ERREXCEPT, math_errhandling
- FP_NORMAL, FP_SUBNORMAL, FP_ZERO, FP_INFINITE, FP_NAN
- std::midpoint
- std::lerp
- std::has_single_bit
- std::bit_ceil
- std::bit_floor
- std::bit_width
- std::rotl
- 数学特殊函数
- 伪随机数生成
- 浮点环境
- std::complex
- std::valarray
- 编译时有理数算术
- std::gcd
- std::lcm
- 数学常数
- std::bit_cast
- std::rotr
- std::countl_zero
- std::countl_one
- std::countr_zero
- std::countr_one
- std::popcount
- 注释
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
std::nearbyint, std::nearbyintf, std::nearbyintl
定义于头文件 <cmath>
|
||
float nearbyint ( float arg ); float nearbyintf( float arg ); |
(1) | (C++11 起) |
double nearbyint ( double arg ); |
(2) | (C++11 起) |
long double nearbyint ( long double arg ); long double nearbyintl( long double arg ); |
(3) | (C++11 起) |
double nearbyint ( IntegralType arg ); |
(4) | (C++11 起) |
参数
arg | - | 浮点值 |
返回值
返回 arg
按照当前舍入模式的最接近整数值。
错误处理
此函数不受制于任何指定于 math_errhandling 的错误。
若实现支持 IEEE 浮点算术( IEC 60559 ),则
- 决不引发 FE_INEXACT
- 若
arg
为 ±∞ ,则返回不修改的参数 - 若
arg
为 ±0 ,则返回不修改的参数 - 若
arg
为 NaN ,则返回 NaN
注意
std::nearbyint
和 rint 之间仅有的区别是 std::nearbyint
决不引发 FE_INEXACT 。
所有标准浮点格式中,最大可表示浮点值都是整数,故 std::nearbyint
自身决不溢出;然而存储结果于整数对象时,结果可能溢出任何整数类型(包含 std::intmax_t )。
若当前舍入模式为 FE_TONEAREST ,则此函数在中点情况向偶数舍入(同 rint ,但不同于 round )。
示例
运行此代码
#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
参阅
(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) |
获取或设置舍入方向 (函数) |