Namespaces
Variants

round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
round lround llround
(C99) (C99) (C99)
(C99)

(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
헤더 파일에 정의됨 <math.h>
float roundf ( float arg ) ;
(1) (C99부터)
double round ( double arg ) ;
(2) (C99부터)
long double roundl ( long double arg ) ;
(3) (C99부터)
헤더 파일에 정의됨 <tgmath.h>
#define round( arg )
(4) (C99부터)
헤더 파일에 정의됨 <math.h>
long lroundf ( float arg ) ;
(5) (C99부터)
long lround ( double arg ) ;
(6) (C99부터)
long lroundl ( long double arg ) ;
(7) (C99부터)
헤더 파일에 정의됨 <tgmath.h>
#define lround( arg )
(8) (C99부터)
헤더 파일에 정의됨 <math.h>
long long llroundf ( float arg ) ;
(9) (C99부터)
long long llround ( double arg ) ;
(10) (C99부터)
long long llroundl ( long double arg ) ;
(11) (C99부터)
헤더 파일에 정의됨 <tgmath.h>
#define llround( arg )
(12) (C99부터)
1-3) arg (부동 소수점 형식)에 가장 가까운 정수 값을 계산하며, 현재 반올림 모드와 관계없이 중간 값은 0에서 멀어지는 방향으로 반올림합니다.
5-7, 9-11) arg 의 가장 가까운 정수 값을 계산합니다(정수 형식으로). 중간 값(0.5)의 경우 현재 반올림 모드와 관계없이 0에서 멀어지는 방향으로 반올림합니다.
4,8,12) 타입-제네릭 매크로: 만약 arg long double 타입을 가지면, roundl , lroundl , llroundl 이 호출됩니다. 그렇지 않고 arg 가 정수 타입이나 double 타입을 가지면, round , lround , llround 이 호출됩니다. 그 외의 경우에는 각각 roundf , lroundf , llroundf 이 호출됩니다.

목차

매개변수

arg - 부동소수점 값

반환값

오류가 발생하지 않으면, arg 에 가장 가까운 정수 값(중간 값은 0에서 멀어지는 방향으로 반올림)이 반환됩니다.

반환값
math-round away zero.svg
인수

도메인 오류가 발생하면 구현에서 정의한 값이 반환됩니다.

오류 처리

오류는 math_errhandling 에 명시된 대로 보고됩니다.

lround 또는 llround 의 결과가 반환 타입으로 표현 가능한 범위를 벗어나는 경우, 도메인 오류 또는 범위 오류가 발생할 수 있습니다.

구현체가 IEEE 부동 소수점 연산(IEC 60559)을 지원하는 경우:

round , roundf , roundl 함수의 경우:
  • 현재 반올림 모드 는 영향을 미치지 않습니다.
  • arg 가 ±∞인 경우, 수정 없이 그대로 반환됩니다.
  • arg 가 ±0인 경우, 수정 없이 그대로 반환됩니다.
  • arg 가 NaN인 경우, NaN이 반환됩니다.
lround llround 함수 계열의 경우:
  • FE_INEXACT 는 절대 발생하지 않습니다.
  • 현재 반올림 모드 는 영향을 미치지 않습니다.
  • arg 가 ±∞인 경우, FE_INVALID 이 발생하고 구현 정의 값이 반환됩니다.
  • 반올림 결과가 반환 타입의 범위를 벗어나는 경우, FE_INVALID 이 발생하고 구현 정의 값이 반환됩니다.
  • arg 가 NaN인 경우, FE_INVALID 이 발생하고 구현 정의 값이 반환됩니다.

참고 사항

FE_INEXACT 는 정수가 아닌 유한 값을 반올림할 때 round 에 의해 (필수는 아니지만) 발생할 수 있습니다.

표준 부동 소수점 형식에서 표현 가능한 가장 큰 부동 소수점 값들은 모두 정확한 정수이므로, round 함수 자체는 절대 오버플로우를 발생시키지 않습니다; 그러나 그 결과를 정수 변수( intmax_t 를 포함한)에 저장할 때는 어떤 정수 타입으로도 오버플로우가 발생할 수 있습니다.

POSIX는 lround 또는 llround FE_INVALID 를 발생시키는 모든 경우가 도메인 오류라고 명시합니다.

double 버전의 round 함수는 다음과 같이 구현된 것처럼 동작합니다:

#include <math.h>
#pragma STDC FENV_ACCESS ON
double round(double x)
{
    return signbit(x) ? ceil(x - 0.5) : floor(x + 0.5);
}

예제

#include <assert.h>
#include <fenv.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
double custom_round(double x)
{
    return signbit(x) ? ceil(x - 0.5) : floor(x + 0.5);
}
void test_custom_round()
{
    const double sample[] =
    {
        0.0, 2.3, 2.5 - DBL_EPSILON, 2.5, 2.5 + DBL_EPSILON, 2.7, INFINITY
    };
    for (size_t t = 0; t < sizeof sample / sizeof(double); ++t)
        assert(round(+sample[t]) == custom_round(+sample[t]) &&
               round(-sample[t]) == custom_round(-sample[t]));
}
int main(void)
{
    // round
    printf("round(+2.3) = %+.1f  ", round(2.3));
    printf("round(+2.5) = %+.1f  ", round(2.5));
    printf("round(+2.7) = %+.1f\n", round(2.7));
    printf("round(-2.3) = %+.1f  ", round(-2.3));
    printf("round(-2.5) = %+.1f  ", round(-2.5));
    printf("round(-2.7) = %+.1f\n", round(-2.7));
    printf("round(-0.0) = %+.1f\n", round(-0.0));
    printf("round(-Inf) = %+f\n",   round(-INFINITY));
    test_custom_round();
    // lround
    printf("lround(+2.3) = %+ld  ", lround(2.3));
    printf("lround(+2.5) = %+ld  ", lround(2.5));
    printf("lround(+2.7) = %+ld\n", lround(2.7));
    printf("lround(-2.3) = %+ld  ", lround(-2.3));
    printf("lround(-2.5) = %+ld  ", lround(-2.5));
    printf("lround(-2.7) = %+ld\n", lround(-2.7));
    printf("lround(-0.0) = %+ld\n", lround(-0.0));
    printf("lround(-Inf) = %+ld\n", lround(-INFINITY)); // FE_INVALID raised
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("lround(LONG_MAX+1.5) = %ld\n", lround(LONG_MAX + 1.5));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID was raised");
}

가능한 출력:

round(+2.3) = +2.0  round(+2.5) = +3.0  round(+2.7) = +3.0
round(-2.3) = -2.0  round(-2.5) = -3.0  round(-2.7) = -3.0
round(-0.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
lround(LONG_MAX+1.5) = -9223372036854775808
    FE_INVALID was raised

참고문헌

  • C23 표준 (ISO/IEC 9899:2024):
  • 7.12.9.6 round 함수들 (p: TBD)
  • 7.12.9.7 lround 및 llround 함수 (p: TBD)
  • 7.25 타입-제네릭 수학 <tgmath.h> (p: TBD)
  • F.10.6.6 round 함수들 (p: TBD)
  • F.10.6.7 lround 및 llround 함수 (p: TBD)
  • C17 표준 (ISO/IEC 9899:2018):
  • 7.12.9.6 round 함수들 (p: 184)
  • 7.12.9.7 lround 및 llround 함수들 (p: 184-185)
  • 7.25 타입-제네릭 수학 <tgmath.h> (p: 272-273)
  • F.10.6.6 round 함수들 (p: 384)
  • F.10.6.7 lround 및 llround 함수들 (p: 385)
  • C11 표준 (ISO/IEC 9899:2011):
  • 7.12.9.6 round 함수들 (p: 253)
  • 7.12.9.7 lround 및 llround 함수들 (p: 253)
  • 7.25 타입-제네릭 수학 <tgmath.h> (p: 373-375)
  • F.10.6.6 round 함수들 (p: 527)
  • F.10.6.7 lround 및 llround 함수들 (p: 528)
  • C99 표준 (ISO/IEC 9899:1999):
  • 7.12.9.6 round 함수들 (p: 233)
  • 7.12.9.7 lround 및 llround 함수들 (p: 234)
  • 7.22 타입-제네릭 수학 <tgmath.h> (p: 335-337)
  • F.9.6.6 round 함수들 (p: 464)
  • F.9.6.7 lround 및 llround 함수들 (p: 464)

참고 항목

주어진 값보다 크지 않은 가장 큰 정수를 계산
(함수)
(C99) (C99)
주어진 값보다 작지 않은 가장 작은 정수를 계산
(함수)
(C99) (C99) (C99)
주어진 값의 크기보다 크지 않은 가장 가까운 정수로 반올림
(함수)