std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl, std::llround, std::llroundf
來自 cppreference.com
定義於標頭檔案 <cmath> |
||
舍入到浮點型別 |
||
(1) | ||
float round ( float num ); double round ( double num ); |
(C++11 起) (直至 C++23) |
|
constexpr /* 浮點型別 */ round ( /* 浮點型別 */ num ); |
(C++23 起) | |
float roundf( float num ); |
(2) | (C++11 起) (C++23 起為 constexpr) |
long double roundl( long double num ); |
(3) | (C++11 起) (C++23 起為 constexpr) |
舍入到 long |
||
(4) | ||
long lround ( float num ); long lround ( double num ); |
(C++11 起) (直至 C++23) |
|
constexpr long lround( /* 浮點型別 */ num ); |
(C++23 起) | |
long lroundf( float num ); |
(5) | (C++11 起) (C++23 起為 constexpr) |
long lroundl( long double num ); |
(6) | (C++11 起) (C++23 起為 constexpr) |
舍入到 long long |
||
(7) | ||
long long llround ( float num ); long long llround ( double num ); |
(C++11 起) (直至 C++23) |
|
constexpr long long llround( /* 浮點型別 */ num ); |
(C++23 起) | |
long long llroundf( float num ); |
(8) | (C++11 起) (C++23 起為 constexpr) |
long long llroundl( long double num ); |
(9) | (C++11 起) (C++23 起為 constexpr) |
定義於標頭檔案 <cmath> |
||
template< class Integer > double round( Integer num ); |
(A) | (C++11 起) (C++23 起為 constexpr) |
template< class Integer > long lround( Integer num ); |
(B) | (C++11 起) (C++23 起為 constexpr) |
template< class Integer > long long llround( Integer num ); |
(C) | (C++11 起) (C++23 起為 constexpr) |
1-3) 計算最接近 num 的整數值(浮點格式),半值遠離零舍入,而無論當前舍入模式如何。 庫為所有 cv-不限定浮點型別提供了
std::round
的過載作為引數 num 的型別。(C++23 起)4-9) 計算最接近 num 的整數值(整數格式),半值遠離零舍入,而無論當前舍入模式如何。 庫為所有 cv-不限定浮點型別提供了
std::lround
和 std::llround
的過載作為引數 num 的型別。(C++23 起)A-C) 為所有整數型別提供額外過載,它們被當作 double 處理。
目錄 |
[編輯] 引數
num | - | 浮點值或整數值 |
[編輯] 返回值
若無錯誤發生,返回最接近 num 的整數值,半值遠離零舍入。
返回值
num
若發生定義域錯誤,返回實現定義的值。
[編輯] 錯誤處理
錯誤按 math_errhandling 指定的方式報告。
若 std::lround
或 std::llround
的結果超出返回型別可表示的範圍,可能發生定義域錯誤或範圍錯誤。
如果實現支援 IEEE 浮點運算 (IEC 60559),
- 對於
std::round
函式- 當前 舍入模式 無效。
- 若 num 為 ±∞,則返回它,不修改。
- 若 num 為 ±0,則返回它,不修改。
- 若 num 為 NaN,則返回 NaN。
- 對於
std::lround
和std::llround
函式- 永不引發 FE_INEXACT。
- 當前 舍入模式 無效。
- 若 num 為 ±∞,則引發 FE_INVALID 並返回實現定義的值。
- 若舍入結果超出返回型別的範圍,則引發 FE_INVALID 並返回實現定義的值。
- 若 num 為 NaN,則引發 FE_INVALID 並返回實現定義的值。
[編輯] 註釋
當舍入非整數有限值時,std::round
可能會(但非必須)引發 FE_INEXACT。
所有標準浮點格式中,最大的可表示浮點值都是精確整數,所以 std::round
本身從不溢位;然而,結果在儲存到整數變數時可能溢位任何整數型別(包括 std::intmax_t)。
POSIX 指定 std::lround
或 std::llround
引發 FE_INEXACT 的所有情況都是定義域錯誤。
std::round
的 double 版本行為如同以下方式實現
#include <cfenv> #include <cmath> #pragma STDC FENV_ACCESS ON double round(double x) { const int save_round = std::fegetround(); std::fesetround(FE_TOWARDZERO); const double result = std::rint(std::copysign(0.5 + std::fabs(x), x)); std::fesetround(save_round); return result; }
額外過載無需完全按照 (A-C) 提供。它們只需足以確保對於其整數型別的引數 num
- std::round(num) 具有與 std::round(static_cast<double>(num)) 相同的效果。
- std::lround(num) 具有與 std::lround(static_cast<double>(num)) 相同的效果。
- std::llround(num) 具有與 std::llround(static_cast<double>(num)) 相同的效果。
[編輯] 示例
執行此程式碼
#include <cassert> #include <cfenv> #include <cfloat> #include <climits> #include <cmath> #include <iostream> // #pragma STDC FENV_ACCESS ON double custom_round(double x) { const int save_round = std::fegetround(); std::fesetround(FE_TOWARDZERO); const double result = std::rint(std::copysign(0.5 + std::fabs(x), x)); std::fesetround(save_round); return result; } void test_custom_round() { for (const double x : { 0.0, 0.3, 0.5 - DBL_EPSILON / 2, 0.5, 0.5 + DBL_EPSILON / 2, 0.7, 1.0, 2.3, 2.5, 2.7, 3.0, static_cast<double>(INFINITY) }) assert(round(+x) == custom_round(+x) && round(-x) == custom_round(-x)); } int main() { test_custom_round(); std::cout << std::showpos; // 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'; // error handling 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) |
不大於給定值幅度的最接近整數 (函式) |
C 文件 關於 round
|