nextafter() Function in C
Last Updated :
17 Jul, 2024
In C, the nextafter() is a standard library function that is used to find the next representable floating-point value after a given number in the direction of another specified number.
In this article, we will learn how to use the nextafter() function in C and its variant for different floating point types.
Syntax of nextafter() in C
double nextafter(double x, double y);
Parameters
The nextafter() function in C takes two parameters:
- x: The starting point (a floating-point number).
- y: The direction point (a floating-point number).
Return Value
- The return value is the next representable floating-point value after x in the direction of y.
- If x equals y, the function returns y.
Variants of nextafter in C
The nextafter() function comes in three variations, catering to different floating-point types:
nextafterf() for float
float nextafterf(float x, float y);
nextafterl() for long double
long double nextafterl(long double x, long double y);
Examples of nextafter() in C
Example 1: Program to demonstrate how to use the nextafter() function
C++
// C Program to demonstrate the nextafter() function
#include <math.h>
#include <stdio.h>
int main()
{
double x = 1.0, y = 2.0;
double result = nextafter(x, y);
// Print the result
printf("The next representable value after %.2lf "
"towards %.2lf is: %.17g\n",
x, y, result);
return 0;
}
OutputThe next representable value after 1.00 towards 2.00 is: 1.0000000000000002
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2: Program to demonstrate the use of the variants of the nextafter() function
C++
// C program to demonstrate the nextafter() function for
// different data types
#include <math.h>
#include <stdio.h>
int main()
{
double x1 = 1.0, y1 = 2.0;
float x2 = 2.0f, y2 = 4.0f;
long double x3 = 3.0l, y3 = 5.0l;
// nextafter function for double
double result1 = nextafter(x1, y1);
// nextafterf function for float
float result2 = nextafterf(x2, y2);
// nextafterl function for long double
long double result3 = nextafterl(x3, y3);
// Print the results
printf("The next representable value after %.2lf "
"towards %.2lf is: %.17g\n",
x1, y1, result1);
printf("The next representable value after %.2f "
"towards %.2f is: %.8g\n",
x2, y2, result2);
printf("The next representable value after %.2Lf "
"towards %.2Lf is: %.21Lg\n",
x3, y3, result3);
return 0;
}
Output
The next representable value after 1.00 towards 2.00 is: 1.0000000000000002
The next representable value after 2.00 towards 4.00 is: 2.0000002
The next representable value after 3.00 towards 5.00 is: 3.00000000000000000022
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
Nested Functions in C Nesting of functions refers to placing the definition of the function inside another functions. In C programming, nested functions are not allowed. We can only define a function globally.Example:C#include <stdio.h> int main() { void fun(){ printf("GeeksForGeeks"); } fun(); return 0; }Outputmai
4 min read
toupper() function in C The toupper() function is used to convert lowercase alphabet to uppercase. i.e. If the character passed is a lowercase alphabet then the toupper() function converts a lowercase alphabet to an uppercase alphabet. It is defined in the ctype.h header file.Syntax: int toupper(int ch);Parameter: It accep
2 min read
tolower() Function in C C tolower() function is part of the C standard Library used to convert the uppercase alphabet to the lowercase alphabet. It does not affect characters other than uppercase characters.ExampleC#include <stdio.h> #include <ctype.h> int main() { // Converting 'A' to 'a' printf("%c", tolower(
2 min read
putchar() function in C The putchar(int ch) method in C is used to write a character, of unsigned char type, to stdout. This character is passed as the parameter to this method. Syntax: int putchar(int ch) Parameters: This method accepts a mandatory parameter ch which is the character to be written to stdout. Return Value:
1 min read
round() Function in C In the C language, the <math.h> header file contains the Standard Math Library that provides various mathematical functions, including the round() function. In this article, we will see how to use the round() function in C.What is round() in C?C round() is a built-in library function that roun
3 min read