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 を返し、ゼロ周辺の動作を一貫させます: 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("The next representable float after %.2f is %.20g (%a)\n", from1, to1, to1);
    float from2 = 1, to2 = nextafterf(from2, 2);
    printf("The next representable float after %.2f is %.20f (%a)\n", from2, to2, to2);
    double from3 = nextafter(0.1, 0), to3 = 0.1;
    printf("The number 0.1 lies between two valid doubles:\n"
           "    %.56f (%a)\nand %.55f  (%a)\n", from3, from3, to3, to3);
    // difference between nextafter and nexttoward:
    long double dir = nextafterl(from1, 1); // first subnormal long double
    float x = nextafterf(from1, dir); // first converts dir to float, giving 0
    printf("Using nextafter, next float after %.2f (%a) is %.20g (%a)\n",
           from1, from1, x, x);
    x = nexttowardf(from1, dir);
    printf("Using nexttoward, next float after %.2f (%a) is %.20g (%a)\n",
           from1, from1, x, x);
    // special values
    {
        #pragma STDC FENV_ACCESS ON
        feclearexcept(FE_ALL_EXCEPT);
        double from4 = DBL_MAX, to4 = nextafter(from4, INFINITY);
        printf("The next representable double after %.2g (%a) is %.23f (%a)\n",
               from4, from4, to4, to4);
        if(fetestexcept(FE_OVERFLOW)) puts("   raised FE_OVERFLOW");
        if(fetestexcept(FE_INEXACT)) puts("   raised FE_INEXACT");
    } // end FENV_ACCESS block
    float from5 = 0.0, to5 = nextafter(from5, -0.0);
    printf("nextafter(+0.0, -0.0) gives %.2g (%a)\n", to5, to5);
}

出力:

The next representable float after 0.00 is 1.4012984643248170709e-45 (0x1p-149)
The next representable float after 1.00 is 1.00000011920928955078 (0x1.000002p+0)
The number 0.1 lies between two valid doubles:
    0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4)
and 0.1000000000000000055511151231257827021181583404541015625  (0x1.999999999999ap-4)
Using nextafter, next float after 0.00 (0x0p+0) is 0 (0x0p+0)
Using nexttoward, next float after 0.00 (0x0p+0) is 1.4012984643248170709e-45 (0x1p-149)
The next representable double after 1.8e+308 (0x1.fffffffffffffp+1023) is inf (inf)
   raised FE_OVERFLOW
   raised FE_INEXACT
nextafter(+0.0, -0.0) gives -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)

関連項目