std:: round, std:: roundf, std:: roundl, std:: lround, std:: lroundf, std:: lroundl, std:: llround, std:: llroundf
|
헤더 파일에 정의됨
<cmath>
|
||
|
부동 소수점 타입으로의 반올림
|
||
| (1) | ||
|
float
round
(
float
num
)
;
double
round
(
double
num
)
;
|
(C++11부터)
(C++23 이전까지) |
|
|
constexpr
/* floating-point-type */
round ( /* floating-point-type */ 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
(
/* floating-point-type */
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
(
/* floating-point-type */
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) |
std::round
의 오버로드를 제공합니다.
(C++23 이후)
std::lround
와
std::llround
의 모든 cv-unqualified 부동 소수점 타입에 대한 오버로드를 매개변수
num
의 타입으로 제공합니다.
(C++23부터)
목차 |
매개변수
| num | - | 부동 소수점 또는 정수 값 |
반환값
오류가 발생하지 않으면, num 에 가장 가까운 정수 값(0에서 먼 방향으로 반올림)이 반환됩니다.
도메인 오류가 발생하면, 구현에서 정의한 값이 반환됩니다.
오류 처리
오류는 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 가 발생하고 구현 정의 값이 반환됩니다.
참고 사항
FE_INEXACT
는 정수가 아닌 유한 값을 반올림할 때
std::round
에 의해 발생할 수 있습니다(반드시 발생해야 하는 것은 아닙니다).
표준 부동 소수점 형식에서 표현 가능한 가장 큰 부동 소수점 값들은 모두 정확한 정수이므로,
std::round
는 그 자체로는 절대 오버플로우를 일으키지 않습니다; 그러나 결과를 정수 변수(예:
std::intmax_t
를 포함한 모든 정수 타입)에 저장할 때는 오버플로우가 발생할 수 있습니다.
POSIX는
std::lround
또는
std::llround
가
FE_INEXACT
를 발생시키는 모든 경우가 도메인 오류라고 명시합니다.
double
버전의
std::round
는 다음과 같이 구현된 것처럼 동작합니다:
#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; // 반올림 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이 발생했습니다\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 documentation
for
round
|
|