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


C++ Math nextafter()用法及代碼示例

nextafter() 函數表示特定方向上的下一個可表示值。

假設兩個數字是 'from' 和 'to'。因此,nextafter() 函數在 'to' 的方向上查找 'from' 的下一個值。

用法

float nextafter( float from, float to);
double nextafter( double from, double to);
long double nextafter( long double from, long double to);
promoted nextafter( arithmetic from, arithmetic to);

注意:如果任何參數為 long double,則返回類型為 long double。如果不是,則返回類型為 double。

參數

( from, to):這些是浮點值。

返回值

  • 如果 'from' 等於 'to',則返回 'from' 的值。
  • 如果沒有發生錯誤,則返回 'from' 的下一個可表示值。

例子1

讓我們看一個簡單的例子,當 'from' 和 'to' 的值相等時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float from=6.7;
     float to=6.7;
     cout<<"Values of from and to are:"<<from<<", "<<to<<'\n';
     cout<<nextafter(from,to);
     return 0;
}

輸出:

Values of from and to are:6.7, 6.7
6.7

在上例中,'from' 和 'to' 的值相等。因此,該函數返回 'from' 的值。

例子2

讓我們看一個簡單的例子,當 'from' 和 'to' 是相同類型時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
      double from=0.0;
      double to=6.0;
      cout<<"Values of from and to are:"<<from<<", "<<to<<'\n';
      cout<<nextafter(from,to);
      return 0;
}

輸出:

Values of from and to are:0, 6
4.94066e-324

在上麵的例子中,'from' 和 'to' 是同類型但不相等。 nextafter() 函數返回值,即 4.94066e-324






相關用法


注:本文由純淨天空篩選整理自 C++ Math nextafter()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。