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


C語言 nextafter用法及代碼示例

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

用法:

     double nextafter  (double x     , double y);
      float nextafterf (float x      , float y);
long double nextafterl (long double x, long double y);
下一個可表示的值
返回後麵的下一個可表示的值x在...方向y

類似的函數,nexttoward具有相同的行為,但是需要一個long double作為第二個論點。

標頭<tgmath.h>提供此函數的type-generic宏版本。
額外的過載在此頭文件中提供(<cmath>)的其他組合算術類型(Type1Type2):這些重載有效地將其參數轉換為double在計算之前,除非至少有一個參數是類型long double(在這種情況下,兩者都強製轉換為long double反而)。

參數

x
基本值。
y
返回值近似於的值。
如果兩個參數比較相等,則函數返回y

返回值

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

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

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

示例

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

相關用法


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