nextafter, nexttoward

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
nextafternexttoward
(C99)(C99)
(C99)
Classification
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       nextafterf( float from, float to );
(1) (since C99)
double      nextafter( double from, double to );
(1) (since C99)
long double nextafterl( long double from, long double to );
(1) (since C99)
float       nexttowardf( float from, long double to );
(2) (since C99)
double      nexttoward( double from, long double to );
(2) (since C99)
long double nexttowardl( long double from, long double to );
(2) (since C99)
Defined in header <tgmath.h>
#define nextafter(from, to)
(3) (since C99)
#define nexttoward(from, to)
(3) (since C99)

Returns the next representable value of from in the direction of to.

1) If from equals to to, to is returned.
2) If from equals to to, to is returned, converted from long double to the return type of the function without loss of range or precision.
3) Type-generic macro: If any argument has type long double, the long double version of the function is called. Otherwise, if any argument has type double, the double version of the function is called. Otherwise, the float version of the function is called.

If from is finite, but the result of nextafter or nexttoward is infinite, range error occurs and FE_OVERFLOW and FE_INEXACT may be raised.

If the result is subnormal or zero and from != to, raises FE_UNDERFLOW and FE_INEXACT.

In any case, the returned value is independent of the current rounding mode.

Contents

[edit] Parameters

from, to - floating point values

[edit] Return value

The next representable value of from in the direction of to.

[edit] Example

#include <math.h>
#include <stdio.h>
#include <float.h>
#include <fenv.h>
 
int main(void)
{
    float from1 = 0, to1 = nextafterf(0, 1);
    printf("The next representable float after %.2f (%a) is %.20g (%a)\n",
           from1, from1, to1, to1);
 
    float from2 = 1, to2 = nextafterf(1, 2);
    printf("The next representable float after %.2f (%a) is %.23f (%a)\n",
           from2, from2, to2, to2);
 
    double from3 = nextafter(0.1, 0), to3 = 0.1;
    printf("The number 0.1 lies between two valid doubles:\n"
           "    %.56f (%a)\nand %.55f  (%a)\n",
           from3, from3, to3, to3);
 
    {
        #pragma STDC FENV_ACCESS ON
        feclearexcept(FE_ALL_EXCEPT);
        double from4 = DBL_MAX, to4 = nextafter(DBL_MAX, INFINITY);
        printf("The next representable double after %.2g (%a) is %.23f (%a)\n",
               from4, from4, to4, to4);
        if(fetestexcept(FE_OVERFLOW)) puts("   raised FE_OVERFLOW");
        if(fetestexcept(FE_INEXACT)) puts("   raised FE_INEXACT");
    } // end FENV_ACCESS block
}

Output:

The next representable float after 0.00 (0x0p+0) is 1.4012984643248170709e-45 (0x1p-149)
The next representable float after 1.00 (0x1p+0) is 1.00000011920928955078125 (0x1.000002p+0)
The number 0.1 lies between two valid doubles:
    0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4)
and 0.1000000000000000055511151231257827021181583404541015625  (0x1.999999999999ap-4)
The next representable double after 1.8e+308 (0x1.fffffffffffffp+1023) is inf (inf)
   raised FE_OVERFLOW
   raised FE_INEXACT

[edit] See also

C++ documentation for nextafter