當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 nexttoward用法及代碼示例

C語言math頭文件(math.h)中nexttoward函數的用法及代碼示例。

用法:

     double nexttoward  (double x     , long double y);
      float nexttowardf (float x      , long double y);
long double nexttowardl (long double x, long double y);
下一個可表示的值向精確值的方向
返回後麵的下一個可表示的值x在...方向y

此函數的行為如下nextafter,但可能更精確y

標頭<tgmath.h>提供此函數的type-generic宏版本。
額外的過載在此頭文件中提供(<cmath>) 為了整數類型:這些重載有效地轉換x到一個double計算之前(為T有任何整數類型)。

參數

x
基本值。
y
返回值近似於的值。
如果兩個參數比較相等,則函數返回y(轉換為返回類型)。

返回值

下一個可表示的值x在...方向y

如果x是類型中可表示的最大有限值,並且結果是無限或不可表示,將發生溢出範圍誤差發生。

如果溢出範圍誤差發生:
- 和math_errhandlingMATH_ERRNO設置:全局變量errno被設定為ERANGE
- 和math_errhandlingMATH_ERREXCEPT設置:FE_OVERFLOW被拋出

示例

/* 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

相關用法


注:本文由純淨天空篩選整理自C標準庫大神的英文原創作品 C nexttoward function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。