C nextup() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
nextup(1.0);
CallWhat 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
C
double nextup(double x);
float nextupf(float x);
long double nextupl(long double x);

Parameters

ParameterDescription
xRequired. The double to step upward from.

Return Value

Return ValueDescription
Next higher doubleThe next representable value toward +∞.
Special casesNaN and infinity handling follows your library’s floating-point rules.

C Examples

Example 1 — Basic Upward Step

Start at 1.0 and print the next larger representable double.

C
#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;
}

How It Works

  1. Include math.h. That declares nextup when supported.
  2. Start at 1.0. A simple binary floating value.
  3. Step up. nextup(1.0) is the next double above 1.
  4. Print many digits. %.17g shows the tiny bump.

Example 2 — Compare with nextafter

On supporting libraries, nextup(x) agrees with stepping toward positive infinity.

C
#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;
}

How It Works

  1. Negative start. Begin at -2.0.
  2. Call nextup. One step toward +∞ (still negative, but larger).
  3. Call nextafter. Same direction via an older API.
  4. 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.

C
#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;
}

How It Works

  1. Pick x. 100.0 is a normal finite value.
  2. Build hi. nextup(x) is the next slot up.
  3. Strict compare. hi > x should hold for finite x.
  4. See the gap. The difference is one ulp at that magnitude.

Frequently Asked Questions

nextup(x) returns the next representable value after x toward positive infinity.
x is required. It is the double you want to step upward from.
A double that is the next higher representable value from x.
Include <math.h>. On GCC, link with -lm. You also need a C23-capable math library.
Not yet. It is newer (C23). Many older toolchains do not declare it. Check support or fall back to nextafter toward +INFINITY.

Next: pow()

Get the IEEE remainder of x divided by y.

pow() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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