Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

What is nextafter() in C++?

Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The nextafter() function is used to get the smallest possible value after a number in a given direction. The declaration of nextafter() is shown below:

double nextafter (double num, double dir);

Or:

long double nextafterl (long double num, long double dir);

Or:

float nextafterf (float num, float dir);

The nextafter() function returns the next representable floating-point number. It does that after num in the direction of dir.

Library

To use the nextafter() function, include the library as shown below:

#include <cmath>

Code

Consider the code snippet below, which demonstrates the implementation of nextafter():

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = 0;
double b = 1;
double x = nextafter(a, b);
cout<< "nextafter ( " << a << "," << b << ") = " << x <<endl;
return 0;
}

The nextafter() function is used in line 10 to compute the next representable floating-point number after a in the direction of b.

RELATED TAGS

c++

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring