Home » C++ programming language

nexttoward() function with example in C++

C++ nexttoward() function: Here, we are going to learn about the nexttoward() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 28, 2019

C++ nexttoward() function

nexttoward() function is a library function of cmath header, it is used to get the next representable value after the given first number in the direction of given second number, it accepts two numbers (x, y) and returns the next representable value after x in the direction of y.

Note: The nexttoward() function is similar to nextafter() function, but it returns next representable value with a potentially more precise y.

Syntax of nexttoward() function:

    nexttoward(x, y);

Parameter(s): x, y – are the numbers to be used to find the next representable value of x in the direction of y.

Return value: float/double/long double – based on the input type, it returns the next representable value of x.

Example:

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

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

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

// 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;
}

Output

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

Reference: C++ nexttoward() function






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.