Namespaces
Variants

nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
nextafter nexttoward
(C99) (C99)
(C23) (C23)
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
Definido en el encabezado <math.h>
float nextafterf ( float from, float to ) ;
(1) (desde C99)
double nextafter ( double from, double to ) ;
(2) (desde C99)
long double nextafterl ( long double from, long double to ) ;
(3) (desde C99)
float nexttowardf ( float from, long double to ) ;
(4) (desde C99)
double nexttoward ( double from, long double to ) ;
(5) (desde C99)
long double nexttowardl ( long double from, long double to ) ;
(6) (desde C99)
Definido en el encabezado <tgmath.h>
#define nextafter(from, to)
(7) (desde C99)
#define nexttoward(from, to)
(8) (desde C99)
1-3) Primero, convierte ambos argumentos al tipo de la función, luego retorna el siguiente valor representable de from en la dirección de to . Si from es igual a to , to es retornado.
4-6) Primero, convierte el primer argumento al tipo de la función, luego retorna el siguiente valor representable de from en la dirección de to . Si from es igual a to , to es retornado, convertido de long double al tipo de retorno de la función sin pérdida de rango o precisión.
7) Macro genérica de tipos: Si algún argumento tiene tipo long double , nextafterl es llamado. De lo contrario, si algún argumento tiene tipo entero o tiene tipo double , nextafter es llamado. De lo contrario, nextafterf es llamado.
8) Macro genérico de tipos: Si el argumento from tiene tipo long double , nexttowardl es llamado. De lo contrario, si from tiene tipo entero o el tipo double , nexttoward es llamado. De lo contrario, nexttowardf es llamado.

Contenidos

Parámetros

desde, hasta - valores de punto flotante

Valor de retorno

Si no ocurren errores, se devuelve el siguiente valor representable de from en la dirección de to . Si from es igual a to , entonces se devuelve to , convertido al tipo de la función.

Si ocurre un error de rango debido a desbordamiento, ± HUGE_VAL , ±HUGE_VALF , o ±HUGE_VALL es devuelto (con el mismo signo que from ).

Si ocurre un error de rango debido a subdesbordamiento, se devuelve el resultado correcto.

Manejo de errores

Los errores se reportan como se especifica en math_errhandling .

Si la implementación soporta aritmética de punto flotante IEEE (IEC 60559),

  • si from es finito, pero el resultado esperado es un infinito, genera FE_INEXACT y FE_OVERFLOW .
  • si from no es igual a to y el resultado es subnormal o cero, genera FE_INEXACT y FE_UNDERFLOW .
  • en cualquier caso, el valor devuelto es independiente del modo de redondeo actual
  • si from o to es NaN, se devuelve NaN.

Notas

POSIX especifica que las condiciones de desbordamiento y subdesbordamiento son errores de rango ( errno puede establecerse).

IEC 60559 recomienda que from sea devuelto cuando from == to . Estas funciones devuelven to en su lugar, lo que hace que el comportamiento alrededor de cero sea consistente: nextafter(-0.0, +0.0) devuelve + 0.0 y nextafter(+0.0, -0.0) devuelve - 0.0 .

nextafter normalmente se implementa mediante la manipulación de la representación IEEE ( glibc musl ).

Ejemplo

#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
    float from1 = 0, to1 = nextafterf(from1, 1);
    printf("El siguiente float representable después de %.2f es %.20g (%a)\n", from1, to1, to1);
    float from2 = 1, to2 = nextafterf(from2, 2);
    printf("El siguiente float representable después de %.2f es %.20f (%a)\n", from2, to2, to2);
    double from3 = nextafter(0.1, 0), to3 = 0.1;
    printf("El número 0.1 se encuentra entre dos doubles válidos:\n"
           "    %.56f (%a)\ny %.55f  (%a)\n", from3, from3, to3, to3);
    // diferencia entre nextafter y nexttoward:
    long double dir = nextafterl(from1, 1); // primer long double subnormal
    float x = nextafterf(from1, dir); // primero convierte dir a float, dando 0
    printf("Usando nextafter, el siguiente float después de %.2f (%a) es %.20g (%a)\n",
           from1, from1, x, x);
    x = nexttowardf(from1, dir);
    printf("Usando nexttoward, el siguiente float después de %.2f (%a) es %.20g (%a)\n",
           from1, from1, x, x);
    // valores especiales
    {
        #pragma STDC FENV_ACCESS ON
        feclearexcept(FE_ALL_EXCEPT);
        double from4 = DBL_MAX, to4 = nextafter(from4, INFINITY);
        printf("El siguiente double representable después de %.2g (%a) es %.23f (%a)\n",
               from4, from4, to4, to4);
        if(fetestexcept(FE_OVERFLOW)) puts("   generó FE_OVERFLOW");
        if(fetestexcept(FE_INEXACT)) puts("   generó FE_INEXACT");
    } // fin del bloque FENV_ACCESS
    float from5 = 0.0, to5 = nextafter(from5, -0.0);
    printf("nextafter(+0.0, -0.0) da %.2g (%a)\n", to5, to5);
}

Salida:

El siguiente float representable después de 0.00 es 1.4012984643248170709e-45 (0x1p-149)
El siguiente float representable después de 1.00 es 1.00000011920928955078 (0x1.000002p+0)
El número 0.1 se encuentra entre dos doubles válidos:
    0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4)
y 0.1000000000000000055511151231257827021181583404541015625  (0x1.999999999999ap-4)
Usando nextafter, el siguiente float después de 0.00 (0x0p+0) es 0 (0x0p+0)
Usando nexttoward, el siguiente float después de 0.00 (0x0p+0) es 1.4012984643248170709e-45 (0x1p-149)
El siguiente double representable después de 1.8e+308 (0x1.fffffffffffffp+1023) es inf (inf)
   generó FE_OVERFLOW
   generó FE_INEXACT
nextafter(+0.0, -0.0) da -0 (-0x0p+0)

Referencias

  • Estándar C23 (ISO/IEC 9899:2024):
  • 7.12.11.3 Las funciones nextafter (p: TBD)
  • 7.12.11.4 Las funciones nexttoward (p: TBD)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: TBD)
  • F.10.8.3 Las funciones nextafter (p: TBD)
  • F.10.8.4 Las funciones nexttoward (p: TBD)
  • Estándar C17 (ISO/IEC 9899:2018):
  • 7.12.11.3 Las funciones nextafter (p: 187)
  • 7.12.11.4 Las funciones nexttoward (p: 187)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: 272-273)
  • F.10.8.3 Las funciones nextafter (p: 386)
  • F.10.8.4 Las funciones nexttoward (p: 386)
  • Estándar C11 (ISO/IEC 9899:2011):
  • 7.12.11.3 Las funciones nextafter (p: 256)
  • 7.12.11.4 Las funciones nexttoward (p: 257)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: 373-375)
  • F.10.8.3 Las funciones nextafter (p: 529)
  • F.10.8.4 Las funciones nexttoward (p: 529)
  • Estándar C99 (ISO/IEC 9899:1999):
  • 7.12.11.3 Las funciones nextafter (p: 237)
  • 7.12.11.4 Las funciones nexttoward (p: 238)
  • 7.22 Matemáticas genéricas de tipos <tgmath.h> (p: 335-337)
  • F.9.8.3 Las funciones nextafter (p: 466)
  • F.9.8.4 Las funciones nexttoward (p: 466)

Véase también