Home C Math Functions nextup() C nextup() Function Definition In C, nextup() returns the next representable floating-point value after x in the direction of positive infinity. It steps one ulp upward on the number line.
You pass one double. The result is a double. Include <math.h>. nextup is newer (C23). Older compilers and libraries may not provide it yet—verify support before shipping code that calls it.
On many systems, nextup(x) matches the idea of nextafter(x, +INFINITY). Prefer nextup when your platform documents C23 math support.
Syntax Pass the floating-point value. Start with this call.
Call What it does nextup(1.0);Returns the next double strictly greater than 1.0.
Include <math.h>. On GCC, link with -lm. Confirm C23 support; some platforms still lack nextup.
Library declarations double nextup(double x);
float nextupf(float x);
long double nextupl(long double x); Parameters Parameter Description x Required. The double to step upward from.
Return Value Return Value Description Next higher double The next representable value toward +∞. Special cases NaN and infinity handling follows your library’s floating-point rules.
C Examples 1. Basic nextup 2. Compare with nextafter 3. Build a neighbor Example 1 — Basic Upward Step Start at 1.0 and print the next larger representable double.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 1.0;
double y = nextup(x);
printf("x = %.17g\n", x);
printf("nextup(x) = %.17g\n", y);
return 0;
} x = 1
nextup(x) = 1.0000000000000002 How It Works Include math.h. That declares nextup when supported.Start at 1.0. A simple binary floating value.Step up. nextup(1.0) is the next double above 1.Print many digits. %.17g shows the tiny bump.Example 2 — Compare with nextafter On supporting libraries, nextup(x) agrees with stepping toward positive infinity.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = -2.0;
double a = nextup(x);
double b = nextafter(x, INFINITY);
printf("nextup = %.17g\n", a);
printf("nextafter = %.17g\n", b);
printf("same? %s\n", a == b ? "yes" : "no");
return 0;
} nextup = -1.9999999999999998
nextafter = -1.9999999999999998
same? yes How It Works Negative start. Begin at -2.0.Call nextup. One step toward +∞ (still negative, but larger).Call nextafter. Same direction via an older API.Compare. Equal results mean the libraries agree.Example 3 — Build a Strict Upper Neighbor Use nextup when you need a value that is guaranteed to be greater than x.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 100.0;
double hi = nextup(x);
if (hi > x) {
printf("hi is strictly above x\n");
}
printf("gap = %.17g\n", hi - x);
return 0;
} hi is strictly above x
gap = 1.4210854715202004e-14 How It Works Pick x. 100.0 is a normal finite value.Build hi. nextup(x) is the next slot up.Strict compare. hi > x should hold for finite x.See the gap. The difference is one ulp at that magnitude.Frequently Asked Questions What does nextup() do in C? nextup(x) returns the next representable value after x toward positive infinity.
What is the parameter of nextup()? x is required. It is the double you want to step upward from.
What does nextup() return? A double that is the next higher representable value from x.
Which header file does nextup() need? Include <math.h>. On GCC, link with -lm. You also need a C23-capable math library.
Is nextup() available everywhere? Not yet. It is newer (C23). Many older toolchains do not declare it. Check support or fall back to nextafter toward +INFINITY.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion