×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

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.

Sample Input and Output

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

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

Example

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

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.