C nexttoward() Function

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

Definition

In C, nexttoward() returns the next representable value of x in the direction of y. It is like nextafter(), but the direction argument is a long double.

You pass a double start value and a long double target direction. The result is a double. Include <math.h>. On GCC, link with -lm.

Use it when your direction comes from a wider type, or when you want the same stepping idea as nextafter with a long double goal.

Syntax

Pass the start value, then the long double direction. Start with this call.

C
nexttoward(1.0, 0.0L);
CallWhat it does
nexttoward(1.0, 0.0L);Returns the next double from 1.0 toward 0.

Include <math.h>. On GCC, link with -lm. The second argument is long double, not double.

Library declarations
C
double nexttoward(double x, long double y);
float nexttowardf(float x, long double y);
long double nexttowardl(long double x, long double y);

Parameters

ParameterDescription
xRequired. The double (or matching float type) to step from.
yRequired. A long double that sets the direction to move toward.

Return Value

Return ValueDescription
Next valueThe next representable value of x in the direction of y.
x unchangedIf x already equals y (as floating values), x is returned.

C Examples

Example 1 — Basic Direction Step

Move from 1.0 toward a smaller long double goal.

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

int main(void) {
    double x = 1.0;
    double y = nexttoward(x, 0.0L);

    printf("x = %.17g\n", x);
    printf("next = %.17g\n", y);
    return 0;
}

How It Works

  1. Include math.h. That declares nexttoward.
  2. Start at 1.0. A simple double.
  3. Aim toward 0.0L. The L suffix makes a long double.
  4. One step. The result is the next double below 1.

Example 2 — Step Toward a Larger Goal

When the direction is larger than x, the result moves up.

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

int main(void) {
    double x = 1.0;
    double y = nexttoward(x, 10.0L);

    printf("%.17g\n", y);
    printf("greater? %s\n", y > x ? "yes" : "no");
    return 0;
}

How It Works

  1. Same start. Still begin at 1.0.
  2. Larger y. 10.0L points upward.
  3. Result grows. You get the next double above 1.
  4. Tiny bump. One ulp is a very small change.

Example 3 — Side by Side with nextafter

Show that both APIs can take the same conceptual step when the goal matches.

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

int main(void) {
    double x = -4.0;
    double a = nexttoward(x, 0.0L);
    double b = nextafter(x, 0.0);

    printf("nexttoward = %.17g\n", a);
    printf("nextafter  = %.17g\n", b);
    return 0;
}

How It Works

  1. Negative start. Begin at -4.0.
  2. Aim toward zero. Both calls move closer to 0.
  3. Compare results. Printed values match on typical hosts.
  4. Type detail. Only the direction argument’s type differs.

Frequently Asked Questions

nexttoward(x, y) returns the next representable value of x in the direction of y. y is a long double.
x is the start value. y is the long double direction to move toward. Both are required.
A value with the type of x that is one step closer to y on the floating-point line.
Include <math.h>. On GCC, link with -lm.
Both step one representable value toward a goal. nexttoward always takes a long double direction; nextafter uses the same floating type for both arguments.

Next: nextup()

Step one representable value toward positive infinity (C23).

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