函式
<cmath> <ctgmath>

nexttoward

     double nexttoward  (double x     , long double y);      float nexttowardf (float x      , long double y);long double nexttowardl (long double x, long double y);
     double nexttoward (double x     , long double y);      float nexttoward (float x      , long double y);long double nexttoward (long double x, long double y);     double nexttoward (T x          , long double y);  // additional overloads
朝向精確值的下一個可表示值
返回在 y 方向上緊隨 x 的下一個可表示值。

此函式的行為與 nextafter 類似,但 y 可能更精確。

標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
此標頭檔案 (<cmath>) 中為整數型別提供了額外的過載:這些過載在計算前有效地將 x 轉換為 double(定義為 T 是任何整數型別)。

引數

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
/* nexttoward example */
#include <stdio.h>      /* printf */
#include <math.h>       /* nexttoward */

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

可能的輸出

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


另見