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
Definiert in Header <math.h>
float nextafterf ( float from, float to ) ;
(1) (seit C99)
double nextafter ( double from, double to ) ;
(2) (seit C99)
long double nextafterl ( long double from, long double to ) ;
(3) (seit C99)
float nexttowardf ( float from, long double to ) ;
(4) (seit C99)
double nexttoward ( double from, long double to ) ;
(5) (seit C99)
long double nexttowardl ( long double from, long double to ) ;
(6) (seit C99)
Definiert in Header <tgmath.h>
#define nextafter(from, to)
(7) (seit C99)
#define nexttoward(from, to)
(8) (seit C99)
1-3) Zuerst werden beide Argumente in den Typ der Funktion konvertiert, dann wird der nächste darstellbare Wert von from in Richtung to zurückgegeben. Wenn from gleich to ist, wird to zurückgegeben.
4-6) Zuerst wird das erste Argument in den Typ der Funktion konvertiert, dann wird der nächste darstellbare Wert von from in Richtung to zurückgegeben. Wenn from gleich to ist, wird to zurückgegeben, konvertiert von long double in den Rückgabetyp der Funktion ohne Verlust des Wertebereichs oder der Genauigkeit.
7) Typgenerisches Makro: Wenn ein Argument den Typ long double hat, wird nextafterl aufgerufen. Andernfalls, wenn ein Argument ganzzahligen Typ oder den Typ double hat, wird nextafter aufgerufen. Andernfalls wird nextafterf aufgerufen.
8) Typgenerisches Makro: Wenn das Argument from den Typ long double besitzt, wird nexttowardl aufgerufen. Andernfalls, wenn from einen Ganzzahltyp oder den Typ double hat, wird nexttoward aufgerufen. Andernfalls wird nexttowardf aufgerufen.

Inhaltsverzeichnis

Parameter

from, to - Fließkommawerte

Rückgabewert

Wenn keine Fehler auftreten, wird der nächste darstellbare Wert von from in Richtung to zurückgegeben. Wenn from gleich to ist, dann wird to zurückgegeben, konvertiert in den Typ der Funktion.

Wenn ein Bereichsfehler aufgrund von Überlauf auftritt, ± HUGE_VAL , ±HUGE_VALF , oder ±HUGE_VALL wird zurückgegeben (mit demselben Vorzeichen wie from ).

Wenn ein Bereichsfehler aufgrund von Unterlauf auftritt, wird das korrekte Ergebnis zurückgegeben.

Fehlerbehandlung

Fehler werden gemeldet, wie in math_errhandling festgelegt.

Wenn die Implementierung IEEE-Gleitkommaarithmetik (IEC 60559) unterstützt,

  • wenn from endlich ist, aber das erwartete Ergebnis eine Unendlichkeit ist, löst dies FE_INEXACT und FE_OVERFLOW aus.
  • wenn from nicht gleich to ist und das Ergebnis subnormal oder null ist, löst dies FE_INEXACT und FE_UNDERFLOW aus.
  • in jedem Fall ist der zurückgegebene Wert unabhängig vom aktuellen Rundungsmodus
  • wenn entweder from oder to NaN ist, wird NaN zurückgegeben.

Hinweise

POSIX spezifiziert , dass die Überlauf- und Unterlaufbedingungen Bereichsfehler sind ( errno kann gesetzt werden).

IEC 60559 empfiehlt, dass from zurückgegeben wird, wann immer from == to . Diese Funktionen geben stattdessen to zurück, was das Verhalten um Null konsistent macht: nextafter(-0.0, +0.0) gibt + 0.0 zurück und nextafter(+0.0, -0.0) gibt - 0.0 zurück.

nextafter wird typischerweise durch Manipulation der IEEE-Darstellung implementiert ( glibc musl ).

Beispiel

#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
    float from1 = 0, to1 = nextafterf(from1, 1);
    printf("Die nächsthöhere darstellbare float-Zahl nach %.2f ist %.20g (%a)\n", from1, to1, to1);
    float from2 = 1, to2 = nextafterf(from2, 2);
    printf("Die nächsthöhere darstellbare float-Zahl nach %.2f ist %.20f (%a)\n", from2, to2, to2);
    double from3 = nextafter(0.1, 0), to3 = 0.1;
    printf("Die Zahl 0.1 liegt zwischen zwei gültigen double-Werten:\n"
           "    %.56f (%a)\nund %.55f  (%a)\n", from3, from3, to3, to3);
    // Unterschied zwischen nextafter und nexttoward:
    long double dir = nextafterl(from1, 1); // erste subnormale long double
    float x = nextafterf(from1, dir); // wandelt zuerst dir in float um, ergibt 0
    printf("Mit nextafter ist die nächste float-Zahl nach %.2f (%a) %.20g (%a)\n",
           from1, from1, x, x);
    x = nexttowardf(from1, dir);
    printf("Mit nexttoward ist die nächste float-Zahl nach %.2f (%a) %.20g (%a)\n",
           from1, from1, x, x);
    // Spezielle Werte
    {
        #pragma STDC FENV_ACCESS ON
        feclearexcept(FE_ALL_EXCEPT);
        double from4 = DBL_MAX, to4 = nextafter(from4, INFINITY);
        printf("Die nächsthöhere darstellbare double-Zahl nach %.2g (%a) ist %.23f (%a)\n",
               from4, from4, to4, to4);
        if(fetestexcept(FE_OVERFLOW)) puts("   FE_OVERFLOW ausgelöst");
        if(fetestexcept(FE_INEXACT)) puts("   FE_INEXACT ausgelöst");
    } // Ende des FENV_ACCESS-Blocks
    float from5 = 0.0, to5 = nextafter(from5, -0.0);
    printf("nextafter(+0.0, -0.0) ergibt %.2g (%a)\n", to5, to5);
}

Ausgabe:

Die nächsthöhere darstellbare float-Zahl nach 0.00 ist 1.4012984643248170709e-45 (0x1p-149)
Die nächsthöhere darstellbare float-Zahl nach 1.00 ist 1.00000011920928955078 (0x1.000002p+0)
Die Zahl 0.1 liegt zwischen zwei gültigen double-Werten:
    0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4)
und 0.1000000000000000055511151231257827021181583404541015625  (0x1.999999999999ap-4)
Mit nextafter ist die nächste float-Zahl nach 0.00 (0x0p+0) 0 (0x0p+0)
Mit nexttoward ist die nächste float-Zahl nach 0.00 (0x0p+0) 1.4012984643248170709e-45 (0x1p-149)
Die nächsthöhere darstellbare double-Zahl nach 1.8e+308 (0x1.fffffffffffffp+1023) ist inf (inf)
   FE_OVERFLOW ausgelöst
   FE_INEXACT ausgelöst
nextafter(+0.0, -0.0) ergibt -0 (-0x0p+0)

Referenzen

  • C23-Standard (ISO/IEC 9899:2024):
  • 7.12.11.3 Die nextafter-Funktionen (S: TBD)
  • 7.12.11.4 Die nexttoward-Funktionen (S: TBD)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S: TBD)
  • F.10.8.3 Die nextafter-Funktionen (S: TBD)
  • F.10.8.4 Die nexttoward-Funktionen (S: TBD)
  • C17-Standard (ISO/IEC 9899:2018):
  • 7.12.11.3 Die nextafter-Funktionen (S: 187)
  • 7.12.11.4 Die nexttoward-Funktionen (S: 187)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S: 272-273)
  • F.10.8.3 Die nextafter-Funktionen (S: 386)
  • F.10.8.4 Die nexttoward-Funktionen (S: 386)
  • C11-Standard (ISO/IEC 9899:2011):
  • 7.12.11.3 Die nextafter-Funktionen (S. 256)
  • 7.12.11.4 Die nexttoward-Funktionen (S. 257)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S. 373-375)
  • F.10.8.3 Die nextafter-Funktionen (S. 529)
  • F.10.8.4 Die nexttoward-Funktionen (S. 529)
  • C99-Standard (ISO/IEC 9899:1999):
  • 7.12.11.3 Die nextafter-Funktionen (S. 237)
  • 7.12.11.4 Die nexttoward-Funktionen (S. 238)
  • 7.22 Typgenerische Mathematik <tgmath.h> (S. 335-337)
  • F.9.8.3 Die nextafter-Funktionen (S. 466)
  • F.9.8.4 Die nexttoward-Funktionen (S. 466)

Siehe auch

C++ Dokumentation für nextafter