名前空間
変種
操作

nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
共通の数学関数
関数
基本的な数学関数
(C99)
(C99)
(C99)
(C99)(C99)(C99)(C23)
最大/最小演算
(C99)
(C99)
指数関数
(C23)
(C99)
(C99)
(C23)
(C23)

(C99)
(C99)(C23)
(C23)
(C23)
べき乗関数
(C99)
(C23)
(C23)

(C99)
(C23)
(C23)
三角関数と双曲線関数
(C23)
(C23)
(C23)
(C23)
(C99)
(C99)
(C99)
最も近い整数浮動小数点数
(C99)(C99)(C99)
(C99)

(C99)(C99)(C99)
(C23)(C23)(C23)(C23)
浮動小数点操作
(C99)(C99)
(C99)(C23)
(C99)
nextafternexttoward
(C99)(C99)
(C23)(C23)
縮小演算
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
量子および量子指数
Decimal再エンコーディング関数
総順序およびペイロード関数
分類
(C99)
(C99)
(C99)
(C23)
誤差関数とガンマ関数
(C99)
(C99)
(C99)
(C99)
マクロ定数
特殊な浮動小数点値
(C99)(C23)
引数と戻り値
エラーハンドリング
高速演算インジケータ
 
ヘッダー <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 の方向にある、表現可能な次の値を返します。fromto と等しい場合、to が返されます。
4-6) まず、最初の引数を関数の型に変換し、次に from から to の方向にある、表現可能な次の値を返します。fromto と等しい場合、to が返されます。このとき、long double から関数の戻り値の型への変換は、範囲や精度を失うことなく行われます。
7) 型汎用マクロ:引数のいずれかが long double 型の場合、nextafterl が呼び出されます。それ以外の場合で、いずれかの引数が整数型または double 型の場合、nextafter が呼び出されます。それ以外の場合は、nextafterf が呼び出されます。
8) 型汎用マクロ:引数 fromlong double 型の場合、nexttowardl が呼び出されます。それ以外の場合で、from が整数型または double 型の場合、nexttoward が呼び出されます。それ以外の場合は、nexttowardf が呼び出されます。

目次

[編集] パラメータ

from, to - 浮動小数点値

[編集] 戻り値

エラーが発生しない場合、from から to の方向にある、表現可能な次の値が返されます。fromto と等しい場合、関数の型に変換された to が返されます。

オーバーフローによる範囲エラーが発生した場合、( from と同じ符号を持つ) ±HUGE_VAL±HUGE_VALF、または ±HUGE_VALL が返されます。

アンダーフローによる範囲エラーが発生した場合、正しい結果が返されます。

[編集] エラー処理

エラーは math_errhandling で指定されたとおりに報告されます。

実装がIEEE浮動小数点算術 (IEC 60559) をサポートしている場合、

  • from が有限値であり、期待される結果が無限大である場合、FE_INEXACT および FE_OVERFLOW が発生します。
  • fromto と等しくなく、結果が非正規化数またはゼロである場合、FE_INEXACT および FE_UNDERFLOW が発生します。
  • いずれの場合も、返される値は現在の丸めモードに依存しません。
  • もし from または to のいずれかが NaN であった場合、NaN が返されます。

[編集] 注釈

POSIX では、オーバーフローおよびアンダーフロー条件は範囲エラーであると規定されています(errno が設定される場合があります)。

IEC 60559 では、from == to の場合に 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 The nextafter functions (p: TBD)
  • 7.12.11.4 The nexttoward functions (p: TBD)
  • 7.25 Type-generic math <tgmath.h> (p: TBD)
  • F.10.8.3 The nextafter functions (p: TBD)
  • F.10.8.4 The nexttoward functions (p: TBD)
  • C17標準 (ISO/IEC 9899:2018)
  • 7.12.11.3 The nextafter functions (p: 187)
  • 7.12.11.4 The nexttoward functions (p: 187)
  • 7.25 型総称数学関数 <tgmath.h> (p: 272-273)
  • F.10.8.3 The nextafter functions (p: 386)
  • F.10.8.4 The nexttoward functions (p: 386)
  • C11標準 (ISO/IEC 9899:2011)
  • 7.12.11.3 The nextafter functions (p: 256)
  • 7.12.11.4 The nexttoward functions (p: 257)
  • 7.25 型総称数学関数 <tgmath.h> (p: 373-375)
  • F.10.8.3 The nextafter functions (p: 529)
  • F.10.8.4 The nexttoward functions (p: 529)
  • C99標準 (ISO/IEC 9899:1999)
  • 7.12.11.3 The nextafter functions (p: 237)
  • 7.12.11.4 The nexttoward functions (p: 238)
  • 7.22 型総称数学関数 <tgmath.h> (p: 335-337)
  • F.9.8.3 The nextafter functions (p: 466)
  • F.9.8.4 The nexttoward functions (p: 466)

[編集] 関連項目

C++ ドキュメントnextafter
English 日本語 中文(简体) 中文(繁體)