函式
<cmath> <ctgmath>

nextafter

     double nextafter  (double x     , double y);      float nextafterf (float x      , float y);long double nextafterl (long double x, long double y);
     double nextafter (double x     , double y );      float nextafter (float x      , float y );long double nextafter (long double x, long double y );     double nextafter (Type1 x      , Type2 y);        // additional overloads
下一個可表示值
返回在y方向上緊隨x的下一個可表示值。

類似函式 nexttoward 具有相同的行為,但它的第二個引數型別為 long double

標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
此標頭檔案 (<cmath>) 中為其他算術型別Type1Type2)的組合提供了額外的過載:這些過載在計算前會有效地將其引數轉換為 double,除非至少有一個引數的型別是 long double(在這種情況下,兩個引數都會被轉換為 long double)。

引數

x
基準值。
y
返回值所趨近的值。
如果兩個引數比較結果相等,則函式返回 y

返回值

y方向上緊隨x的下一個可表示值。

如果 x 是該型別可表示的最大有限值,且結果是無限或不可表示的,則會發生上溢範圍錯誤

如果發生上溢範圍錯誤
- 並且 math_errhandling 設定了 MATH_ERRNO:全域性變數 errno 被設定為 ERANGE
- 並且 math_errhandling 設定了 MATH_ERREXCEPT:將引發 FE_OVERFLOW

示例

1
2
3
4
5
6
7
8
9
10
/* nextafter example */
#include <stdio.h>      /* printf */
#include <math.h>       /* nextafter */

int main ()
{
  printf ("first representable value greater than zero: %e\n", nextafter(0.0,1.0));
  printf ("first representable value less than zero: %e\n", nextafter(0.0,-1.0));
  return 0;
}

可能的輸出

first representable value greater than zero: 4.940656e-324
first representable value less than zero: -4.940656e-324


另見