**How would you solve below problems in C/C++?**

- What is the smallest representable positive floating point number in C/C++?
- What is the largest representable negative floating point number in C/C++?
- Given a positive floating point number x, find the largest representable floating point value smaller than x?

**nextafter(x, y) and nexttoward(x.y)**

In C and C++, both nextafter(x, y) and nexttoward(x.y) are similar functions defined in math.h or cmath header files. They both return next representable value after x in the direction of y. *nexttoward() has more precise second parameter y.*

The following program demonstrates the concept :

`// C program to demonstrate use of nextafter() and nexttoward() ` `#include <stdio.h> ` `#include <math.h> ` `int` `main () ` `{ ` ` ` `// using nextafter ` ` ` `printf` `(` `"Smallest positive floating point number : %e\n"` `, ` ` ` `nextafter(0.0, 1.0)); ` ` ` `printf` `(` `"Largest negative floating point number :%e\n"` `, ` ` ` `nextafter(0.0, -1.0)); ` ` ` `printf` `(` `"Largest positive floating point number smaller than 0.5 : %e\n"` `, ` ` ` `nextafter(0.5, 0.0)); ` ` ` ` ` `// using nexttoward ` ` ` `printf` `(` `"Smallest positive floating point number : %e\n"` `, ` ` ` `nexttoward(0.0, 1.0)); ` ` ` `printf` `(` `"Largest negative floating point number : %e\n"` `, ` ` ` `nexttoward(0.0, -1.0)); ` ` ` `printf` `(` `"Largest positive floating point number smaller than 0.5 : %e\n"` `, ` ` ` `nexttoward(0.5, 0.0)); ` ` ` `return` `(0); ` `} ` |

*chevron_right*

*filter_none*

Output:

nextafter first value greater than zero: 4.940656e-324 nextafter first value less than zero: -4.940656e-324 nexttoward first value greater than zero: 4.940656e-324 nexttoward first valnextafter first value greater than zero: 4.940656e-324 nextafter first value less than zero: -4.940656e-324 nexttoward first value greater than zero: 4.940656e-324 nexttoward first value less than zero: -4.940656e-324 ue less than zero: -4.940656e-324

This article is contributed by** Aditya Chatterjee.** If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Dividing a Large file into Separate Modules in C/C++, Java and Python
- How to delete a range of values from the List using Iterator
- How to create a List with Constructor in C++ STL
- C program to store Student records as Structures and Sort them by Name
- How to delete last element from a List in C++ STL
- Can C++ reference member be declared without being initialized with declaration?
- Finding Floor and Ceil of a Sorted Array using C++ STL
- Some useful C++ tricks for beginners in Competitive Programming
- Replace each node with its Surpasser Count in Linked List
- Comparison of Java with other programming languages
- Print multiples of Unit Digit of Given Number
- Similarities and Differences between Ruby and C language
- Similarities and Differences between Ruby and C++
- Program to copy the contents of one array into another in the reverse order