Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Nishtha Thakur

Here we will see the effect of nextafter() and nextforward() functions in C or C++. These functions are present in the math.h or cmath library.

if the functions are like nextafter(a, b) and nextforward(a, b). These functions are used to find the next representable value after a in the direction of b. The nextforward() has more precise second parameter b.

Example

#include <stdio.h>
#include <math.h>
main () {
   //The nextafter function()
   printf ("Smallest representable number after 0 towards 1 : %e\n", nextafter(0.0, 1.0));
   printf ("Largest representable number before -1 towards 0 :%e\n", nextafter(0.0, -1.0));
   printf ("Largest +ve representable number after 0.8 : %e\n", nextafter(0.8, 0.0));
   // using nexttoward
   printf("\n");
   printf ("Smallest representable number after 0 towards 1 : %e\n", nexttoward(0.0, 1.0));
   printf ("Largest representable number before -1 towards 0 : %e\n", nexttoward(0.0, -1.0));
   printf ("Largest +ve representable number after 0.8 : %e\n", nexttoward(0.8, 0.0));
}

Output

Smallest representable number after 0 towards 1 : 4.940656e-324
Largest representable number before -1 towards 0 :-4.940656e-324
Largest +ve representable number after 0.8 : 8.000000e-001
Smallest representable number after 0 towards 1 : 4.940656e-324
Largest representable number before -1 towards 0 : -4.940656e-324
Largest +ve representable number after 0.8 : 8.000000e-001

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.