Namespaces
Variants

nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl

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
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
nextafter nexttoward
(C99) (C99)
(C23) (C23)
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 nextafterf ( float from, float to ) ;
(1) (C99부터)
double nextafter ( double from, double to ) ;
(2) (C99부터)
long double nextafterl ( long double from, long double to ) ;
(3) (C99부터)
float nexttowardf ( float from, long double to ) ;
(4) (C99부터)
double nexttoward ( double from, long double to ) ;
(5) (C99부터)
long double nexttowardl ( long double from, long double to ) ;
(6) (C99부터)
헤더 파일에 정의됨 <tgmath.h>
#define nextafter(from, to)
(7) (C99부터)
#define nexttoward(from, to)
(8) (C99부터)
1-3) 먼저 두 인수를 함수의 타입으로 변환한 다음, from 에서 to 방향으로의 다음 표현 가능한 값을 반환합니다. from to 와 같으면 to 가 반환됩니다.
4-6) 먼저 첫 번째 인수를 함수의 타입으로 변환한 다음, from 에서 to 방향으로의 다음 표현 가능한 값을 반환합니다. from to 와 같으면, to 가 반환되며, 범위나 정밀도의 손실 없이 long double 에서 함수의 반환 타입으로 변환됩니다.
7) 타입-제네릭 매크로: 인자 중 하나라도 long double 타입을 가지면 nextafterl 이 호출됩니다. 그렇지 않고 인자 중 하나라도 정수 타입이거나 double 타입을 가지면 nextafter 이 호출됩니다. 그 외의 경우에는 nextafterf 이 호출됩니다.
8) 타입-제네릭 매크로: 인수 from long double 타입을 가지면 nexttowardl 이 호출됩니다. 그렇지 않고 from 이 정수 타입이나 double 타입을 가지면 nexttoward 이 호출됩니다. 그 외의 경우에는 nexttowardf 이 호출됩니다.

목차

매개변수

from, to - 부동 소수점 값

반환값

오류가 발생하지 않으면, from 에서 to 방향으로의 다음 표현 가능한 값이 반환됩니다. 만약 from to 와 같다면, to 가 함수의 타입으로 변환되어 반환됩니다.

오버플로로 인한 범위 오류가 발생하면, ± HUGE_VAL , ±HUGE_VALF , 또는 ±HUGE_VALL 이 반환됩니다 ( from 과 동일한 부호로).

언더플로로 인해 범위 오류가 발생하면 올바른 결과가 반환됩니다.

오류 처리

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

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

  • 만약 from 이 유한하지만 예상 결과가 무한대인 경우, FE_INEXACT FE_OVERFLOW 를 발생시킵니다.
  • 만약 from to 와 같지 않고 결과가 서브노멀 또는 제로인 경우, FE_INEXACT FE_UNDERFLOW 를 발생시킵니다.
  • 어떤 경우든 반환된 값은 현재 반올림 모드와 무관합니다
  • 만약 from 또는 to 가 NaN인 경우, NaN이 반환됩니다.

참고 사항

POSIX는 오버플로우 및 언더플로우 조건이 범위 오류( errno 가 설정될 수 있음)라고 명시합니다.

IEC 60559는 from 이 반환될 것을 권장합니다. 이러한 함수들은 대신 to 를 반환하며, 이로 인해 0 주변의 동작이 일관성을 갖습니다: nextafter(-0.0, +0.0) + 0.0 를 반환하고 nextafter(+0.0, -0.0) - 0.0 를 반환합니다.

nextafter 는 일반적으로 IEEE 표현 방식을 조작하여 구현됩니다 ( glibc musl ).

예제

#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
    float from1 = 0, to1 = nextafterf(from1, 1);
    printf("%.2f 이후 표현 가능한 다음 float 값은 %.20g (%a)입니다.\n", from1, to1, to1);
    float from2 = 1, to2 = nextafterf(from2, 2);
    printf("%.2f 이후 표현 가능한 다음 float 값은 %.20f (%a)입니다.\n", from2, to2, to2);
    double from3 = nextafter(0.1, 0), to3 = 0.1;
    printf("숫자 0.1은 두 개의 유효한 double 값 사이에 위치합니다:\n"
           "    %.56f (%a)\n및 %.55f  (%a)\n", from3, from3, to3, to3);
    // nextafter와 nexttoward의 차이:
    long double dir = nextafterl(from1, 1); // 첫 번째 subnormal long double
    float x = nextafterf(from1, dir); // 먼저 dir을 float로 변환하여 0을 반환
    printf("nextafter 사용 시, %.2f (%a) 이후의 다음 float 값은 %.20g (%a)입니다.\n",
           from1, from1, x, x);
    x = nexttowardf(from1, dir);
    printf("nexttoward 사용 시, %.2f (%a) 이후의 다음 float 값은 %.20g (%a)입니다.\n",
           from1, from1, x, x);
    // 특수 값
    {
        #pragma STDC FENV_ACCESS ON
        feclearexcept(FE_ALL_EXCEPT);
        double from4 = DBL_MAX, to4 = nextafter(from4, INFINITY);
        printf("%.2g (%a) 이후 표현 가능한 다음 double 값은 %.23f (%a)입니다.\n",
               from4, from4, to4, to4);
        if(fetestexcept(FE_OVERFLOW)) puts("   FE_OVERFLOW 예외 발생");
        if(fetestexcept(FE_INEXACT)) puts("   FE_INEXACT 예외 발생");
    } // FENV_ACCESS 블록 종료
    float from5 = 0.0, to5 = nextafter(from5, -0.0);
    printf("nextafter(+0.0, -0.0)의 결과는 %.2g (%a)입니다.\n", to5, to5);
}

출력:

0.00 이후 표현 가능한 다음 float 값은 1.4012984643248170709e-45 (0x1p-149)입니다.
1.00 이후 표현 가능한 다음 float 값은 1.00000011920928955078 (0x1.000002p+0)입니다.
숫자 0.1은 두 개의 유효한 double 값 사이에 위치합니다:
    0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4)
및 0.1000000000000000055511151231257827021181583404541015625  (0x1.999999999999ap-4)
nextafter 사용 시, 0.00 (0x0p+0) 이후의 다음 float 값은 0 (0x0p+0)입니다.
nexttoward 사용 시, 0.00 (0x0p+0) 이후의 다음 float 값은 1.4012984643248170709e-45 (0x1p-149)입니다.
1.8e+308 (0x1.fffffffffffffp+1023) 이후 표현 가능한 다음 double 값은 inf (inf)입니다.
   FE_OVERFLOW 예외 발생
   FE_INEXACT 예외 발생
nextafter(+0.0, -0.0)의 결과는 -0 (-0x0p+0)입니다.

참고문헌

  • C23 표준 (ISO/IEC 9899:2024):
  • 7.12.11.3 nextafter 함수들 (p: TBD)
  • 7.12.11.4 nexttoward 함수들 (p: TBD)
  • 7.25 타입-제네릭 수학 <tgmath.h> (p: TBD)
  • F.10.8.3 nextafter 함수들 (p: TBD)
  • F.10.8.4 nexttoward 함수들 (p: TBD)
  • C17 표준 (ISO/IEC 9899:2018):
  • 7.12.11.3 nextafter 함수들 (p: 187)
  • 7.12.11.4 nexttoward 함수들 (p: 187)
  • 7.25 타입-제네릭 수학 <tgmath.h> (p: 272-273)
  • F.10.8.3 nextafter 함수들 (p: 386)
  • F.10.8.4 nexttoward 함수들 (p: 386)
  • C11 표준 (ISO/IEC 9899:2011):
  • 7.12.11.3 nextafter 함수들 (p: 256)
  • 7.12.11.4 nexttoward 함수들 (p: 257)
  • 7.25 타입-제네릭 수학 <tgmath.h> (p: 373-375)
  • F.10.8.3 nextafter 함수들 (p: 529)
  • F.10.8.4 nexttoward 함수들 (p: 529)
  • C99 표준 (ISO/IEC 9899:1999):
  • 7.12.11.3 nextafter 함수들 (p: 237)
  • 7.12.11.4 nexttoward 함수들 (p: 238)
  • 7.22 타입-제네릭 수학 <tgmath.h> (p: 335-337)
  • F.9.8.3 nextafter 함수들 (p: 466)
  • F.9.8.4 nexttoward 함수들 (p: 466)

참고 항목

C++ documentation for nextafter