nextafter() and nexttoward() in C/C++

CC++Server Side ProgrammingProgramming

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
raja
Published on 08-May-2019 11:32:19
Advertisements