名稱空間
變體
操作

nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl

來自 cppreference.com
< c‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(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)
量子與量子指數
十進位制重新編碼函式
總序和載荷函式
分類
(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) 首先,將兩個引數都轉換為函式的型別,然後返回 fromto 方向上的下一個可表示值。如果 from 等於 to,則返回 to
4-6) 首先,將第一個引數轉換為函式的型別,然後返回 fromto 方向上的下一個可表示值。如果 from 等於 to,則返回 to,該值從 long double 型別轉換為函式的返回型別,且不損失範圍或精度。
7) 型別通用宏:如果任何引數的型別為 long double,則呼叫 nextafterl。否則,如果任何引數具有整數型別或型別 double,則呼叫 nextafter。否則,呼叫 nextafterf
8) 型別通用宏:如果引數 from 的型別為 long double,則呼叫 nexttowardl。否則,如果 from 具有整數型別或型別 double,則呼叫 nexttoward。否則,呼叫 nexttowardf

目錄

[編輯] 引數

from, to - 浮點值

[編輯] 返回值

如果沒有錯誤發生,則返回 fromto 方向上的下一個可表示值。如果 from 等於 to,則返回 to,該值已轉換為函式的型別。

如果因溢位導致範圍錯誤,則返回 ±HUGE_VAL±HUGE_VALF±HUGE_VALL(與 from 的符號相同)。

如果由於下溢導致範圍錯誤,則返回正確結果。

[編輯] 錯誤處理

錯誤按 math_errhandling 中指定的方式報告。

如果實現支援 IEEE 浮點運算 (IEC 60559),

  • 如果 from 是有限的,但預期結果為無窮大,則會引發 FE_INEXACTFE_OVERFLOW
  • 如果 from 不等於 to 且結果為次正常數或零,則會引發 FE_INEXACTFE_UNDERFLOW
  • 在任何情況下,返回值都與當前的舍入模式無關。
  • 如果 fromto 是 NaN,則返回 NaN。

[編輯] 注意

POSIX 指定溢位和下溢條件是範圍錯誤(errno 可能會被設定)。

IEC 60559 建議當 from == to 時返回 from。這些函式改為返回 to,這使得零附近的行為一致:nextafter(-0.0, +0.0) 返回 +0.0nextafter(+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: 待定)
  • 7.12.11.4 nexttoward 函式 (p: 待定)
  • 7.25 型別通用數學 <tgmath.h> (p: TBD)
  • F.10.8.3 nextafter 函式 (p: 待定)
  • F.10.8.4 nexttoward 函式 (p: 待定)
  • 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++ 文件,關於 nextafter