C nextafter() Function

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

Definition

In C, nextafter() returns the next representable double after x in the direction of y. Floating-point numbers have gaps. This function takes one tiny step toward y.

You pass two double values: the start point and the direction target. Include <math.h>. On GCC, link with -lm. Related helpers include nextup() and nextdown().

If x already equals y, nextafter returns y. Otherwise it moves by one unit in the last place (one ULP) toward y.

Syntax

Pass the current value and the direction value.

C
nextafter(1.0, 2.0);
CallWhat it does
nextafter(1.0, 2.0)Returns the smallest double greater than 1.0.

Include <math.h>. On GCC, link with -lm. Both arguments are required.

Library declarations
C
double nextafter(double x, double y);
float nextafterf(float x, float y);
long double nextafterl(long double x, long double y);

Parameters

ParameterDescription
xRequired. The starting double.
yRequired. The direction target. The result steps from x toward y.

Return Value

Return ValueDescription
doubleThe next representable value after x toward y, or y if x equals y.

C Examples

Example 1 — Step Up from 1.0

Move one representable step above 1.0 and print the difference.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 1.0;
    double n = nextafter(x, 2.0);

    printf("next = %.17f\n", n);
    printf("diff = %.17e\n", n - x);
    return 0;
}

How It Works

  1. Start at 1.0. A common reference point.
  2. Aim toward 2.0. That chooses the upward direction.
  3. One ULP up. The next double after 1.0 appears.
  4. Diff is epsilon-ish. Around 1.0 this gap is about 2.22e-16.

Example 2 — Step Toward Zero

Move one step downward from a value by aiming at a smaller target.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 10.0;
    double n = nextafter(x, 0.0);

    printf("%.17f\n", n);
    return 0;
}

How It Works

  1. Start at 10. A larger magnitude than 1.
  2. Aim at 0. Direction is downward.
  3. One step lower. The printed value is just under 10.
  4. Spacing grows. ULP size depends on the magnitude.

Example 3 — Walk a Few Steps

Call nextafter in a short loop to climb several representable values.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 0.0;
    int i;

    for (i = 0; i < 3; i++) {
        x = nextafter(x, 1.0);
        printf("%.17e\n", x);
    }
    return 0;
}

How It Works

  1. Start at 0. The next values are tiny subnormals.
  2. Loop three times. Each call advances one representable step.
  3. Aim at 1.0. That keeps the direction positive.
  4. See the gaps. Near zero the steps are extremely small.

Frequently Asked Questions

nextafter(x, y) returns the next representable double after x in the direction of y.
x is the start value. y is the direction target. Both are required.
The next representable double toward y, or y if x already equals y.
Include <math.h>. On GCC, link with -lm.
Use it to nudge a value by one ULP, explore floating gaps, or build strict inequalities.

Next: nextdown()

Move one representable step toward negative infinity.

nextdown() 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