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


C++ nexttoward()用法及代碼示例


C++ nexttoward() 函數

nexttoward() 函數是cmath頭文件的庫函數,用於在給定的第二個數字的方向上獲取給定的第一個數字之後的下一個可表示的值,它接受兩個數字(x, y) 並在之後返回下一個可表示的值x在...方向y

注意:nexttoward() 函數類似於 nextafter() 函數,但它返回下一個可表示的值,可能更精確y

nexttoward() 函數的語法:

    nexttoward(x, y);

參數: x, y– 是用於查找下一個可表示值的數字x在...方向y

返回值: float/double/long double– 基於輸入類型,它返回下一個可表示的值x

例:

    Input:
    float x = 0.0;
    long double  y = 1.0L;

    Function call:
    nexttoward(x,y);    
    
    Output:
    1.4013e-45

C++代碼演示nexttoward()函數的例子

// C++ code to demonstrate the example of 
// nexttoward() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x;
    long double  y;
    
    x = 0.0;
    y = 1.0L;
    cout<<"nexttoward("<<x<<","<<y<<"):"<<nexttoward(x,y);
    cout<<endl;

    x = 0.0;
    y = 1.1L;
    cout<<"nexttoward("<<x<<","<<y<<"):"<<nexttoward(x,y);
    cout<<endl;    

    x =  0.0;
    y = -1.0L;
    cout<<"nexttoward("<<x<<","<<y<<"):"<<nexttoward(x,y);
    cout<<endl;

    x = 0.1;
    y = 0.1L;
    cout<<"nexttoward("<<x<<","<<y<<"):"<<nexttoward(x,y);
    cout<<endl;

    return 0;
}

輸出

nexttoward(0,1):1.4013e-45
nexttoward(0,1.1):1.4013e-45
nexttoward(0,-1):-1.4013e-45
nexttoward(0.1,0.1):0.1

參考:C++ nexttoward() 函數



相關用法


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