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++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >æ°å¼åº >å¸¸ç¨æ°å¦å½æ° > std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl, std::llround, std::llroundf
std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl, std::llround, std::llroundf
| å®ä¹äºå¤´æä»¶ <cmath>
|
||
| float    round ( float arg ); float    roundf( float arg ); |
(1) | (C++11 èµ·) |
| double    round ( double arg ); |
(2) | (C++11 èµ·) |
| long double round ( long double arg ); long double roundl( long double arg ); |
(3) | (C++11 èµ·) |
| double    round ( IntegralType arg ); |
(4) | (C++11 èµ·) |
| long lround ( float arg ); long lroundf( float arg ); |
(5) | (C++11 èµ·) |
| long lround ( double arg ); |
(6) | (C++11 èµ·) |
| long lround ( long double arg ); long lroundl( long double arg ); |
(7) | (C++11 èµ·) |
| long lround ( IntegralType arg ); |
(8) | (C++11 èµ·) |
| long long llround ( float arg ); long long llroundf( float arg ); |
(9) | (C++11 èµ·) |
| long long llround ( double arg ); |
(10) | (C++11 èµ·) |
| long long llround ( long double arg ); long long llroundl( long double arg ); |
(11) | (C++11 èµ·) |
| long long llround ( IntegralType arg ); |
(12) | (C++11 èµ·) |
arg çææ¥è¿æ´æ°å¼ï¼ä»¥æµ®ç¹æ ¼å¼ï¼ï¼ä¸ç¹æ
åµèå
¥ä¸ºè¿ç¦»é¶ï¼æ å
³ä¹å½åèå
¥æ¨¡å¼ãarg çææ¥è¿æ´æ°å¼ï¼ä»¥æ´æ°æ ¼å¼ï¼ï¼ä¸ç¹æ
åµèå
¥ä¸ºè¿ç¦»é¶ï¼æ å
³ä¹å½åèå
¥æ¨¡å¼ãåæ°
| arg | - | æµ®ç¹å¼ |
è¿åå¼
è¥ä¸åºç°é误ï¼åè¿å arg çææ¥è¿æ´æ°å¼ï¼ä¸ç¹æ
åµä¸ºè¿ç¦»é¶è
ã
è¥åºç°å®ä¹åé误ï¼åè¿åå®ç°å®ä¹å¼ã
é误å¤ç
æ¥å math_errhandling 䏿å®çé误ã
è¥ std::lround æ std::llround çç»æå¨è¿åç±»åçå¯è¡¨ç¤ºèå´å¤ï¼åå¯è½åºç°å®ä¹åé误æå¼åé误ã
è¥å®ç°æ¯æ IEEE æµ®ç¹ç®æ¯ï¼ IEC 60559 ï¼ï¼å
- 对äº
std::round彿°ï¼
- å½åèå ¥æ¨¡å¼æ æã
- è¥
arg为 ±â ï¼åè¿åä¸ä¿®æ¹çåæ°ã - è¥
arg为 ±0 ï¼åè¿åä¸ä¿®æ¹çåæ°ã - è¥
arg为 NaN ï¼åè¿å NaN ã
- 对äº
std::lroundåstd::llround彿°ï¼
- å³ä¸å¼å FE_INEXACT ã
- å½åèå ¥æ¨¡å¼æ æã
- è¥
arg为 ±â ï¼åå¼å FE_INVALID å¹¶è¿åå®ç°å®ä¹å¼ã - è¥èå ¥ç»æå¨è¿åç±»åèå´å¤ï¼åå¼å FE_INVALID å¹¶è¿åå®ç°å®ä¹å¼ã
- è¥
arg为 NaN ï¼åå¼å FE_INVALID å¹¶è¿åå®ç°å®ä¹å¼ã
注æ
èå
¥éæ´æ°æé弿¶ std::round å¯ä»¥ï¼ä½ä¸è¦æ±ï¼å¼å FE_INEXACT ã
æææ åæµ®ç¹æ ¼å¼ä¸ï¼æå¤§å¯è¡¨ç¤ºæµ®ç¹å¼å为åç¡®çæ´æ°ï¼æ
std::round èªèº«å³ä¸ä¸æº¢ï¼ç¶èå¨åå¨äºæ´æ°å¯¹è±¡æ¶ï¼ç»æå¯è½æº¢åºä»»ä½æ´æ°ç±»åï¼å
å« std::intmax_t ï¼ã
POSIX æå® std::lround æ std::llround å¼å FE_INEXACT çæææ
åµé½æ¯å®ä¹åé误ã
std::round ç double çæ¬è¡¨ç°ä¸ºå¦åå®ç°å¦ä¸ï¼
#include <cmath> #include <cfenv> #pragma STDC FENV_ACCESS ON double round(double x) { std::fenv_t save_env; std::feholdexcept(&save_env); double result = std::rint(x); if (std::fetestexcept(FE_INEXACT)) { auto const save_round = std::fegetround(); std::fesetround(FE_TOWARDZERO); result = std::rint(std::copysign(0.5 + std::fabs(x), x)); std::fesetround(save_round); } std::feupdateenv(&save_env); return result; }
示ä¾
#include <iostream> #include <cmath> #include <cfenv> #include <climits>  #pragma STDC FENV_ACCESS ON  int main() { // round std::cout << "round(+2.3) = " << std::round(2.3) << " round(+2.5) = " << std::round(2.5) << " round(+2.7) = " << std::round(2.7) << '\n' << "round(-2.3) = " << std::round(-2.3) << " round(-2.5) = " << std::round(-2.5) << " round(-2.7) = " << std::round(-2.7) << '\n';  std::cout << "round(-0.0) = " << std::round(-0.0) << '\n' << "round(-Inf) = " << std::round(-INFINITY) << '\n';  // lround std::cout << "lround(+2.3) = " << std::lround(2.3) << " lround(+2.5) = " << std::lround(2.5) << " lround(+2.7) = " << std::lround(2.7) << '\n' << "lround(-2.3) = " << std::lround(-2.3) << " lround(-2.5) = " << std::lround(-2.5) << " lround(-2.7) = " << std::lround(-2.7) << '\n';  std::cout << "lround(-0.0) = " << std::lround(-0.0) << '\n' << "lround(-Inf) = " << std::lround(-INFINITY) << '\n';  // é误å¤ç std::feclearexcept(FE_ALL_EXCEPT); std::cout << "std::lround(LONG_MAX+1.5) = " << std::lround(LONG_MAX+1.5) << '\n'; if (std::fetestexcept(FE_INVALID)) std::cout << " FE_INVALID was raised\n"; }
å¯è½çè¾åºï¼
round(+2.3) = 2 round(+2.5) = 3 round(+2.7) = 3
round(-2.3) = -2 round(-2.5) = -3 round(-2.7) = -3
round(-0.0) = -0
round(-Inf) = -inf
lround(+2.3) = 2 lround(+2.5) = 3 lround(+2.7) = 3
lround(-2.3) = -2 lround(-2.5) = -3 lround(-2.7) = -3
lround(-0.0) = 0
lround(-Inf) = -9223372036854775808
std::lround(LONG_MAX+1.5) = -9223372036854775808
FE_INVALID was raisedåé
| (C++11)(C++11) |
ä¸å¤§äºç»å®å¼çææ¥è¿æ´æ° (彿°) |
| (C++11)(C++11) |
ä¸å°äºç»å®å¼çææ¥è¿æ´æ°å¼ (彿°) |
| (C++11)(C++11)(C++11) |
ç»å¯¹å¼ä¸å¤§äºç»å®å¼çææ¥è¿æ´æ° (彿°) |