当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。