Namespaces
Variants

nearbyint, nearbyintf, nearbyintl

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
nearbyint
(C99)
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
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 im Header <math.h>
float nearbyintf ( float arg ) ;
(1) (seit C99)
double nearbyint ( double arg ) ;
(2) (seit C99)
long double nearbyintl ( long double arg ) ;
(3) (seit C99)
Definiert im Header <tgmath.h>
#define nearbyint( arg )
(4) (seit C99)
1-3) Rundet das Fließkomma-Argument arg auf einen ganzzahligen Wert im Fließkommaformat, unter Verwendung des aktuellen Rundungsmodus .
4) Typgenerisches Makro: Wenn arg den Typ long double besitzt, wird nearbyintl aufgerufen. Andernfalls, wenn arg ganzzahligen Typ oder den Typ double besitzt, wird nearbyint aufgerufen. Andernfalls wird jeweils nearbyintf aufgerufen.

Inhaltsverzeichnis

Parameter

arg - Gleitkommawert

Rückgabewert

Der nächstgelegene ganzzahlige Wert zu arg wird entsprechend des aktuellen Rundungsmodus zurückgegeben.

Fehlerbehandlung

Diese Funktion unterliegt keinen der in math_errhandling spezifizierten Fehler.

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

  • FE_INEXACT wird niemals ausgelöst.
  • Wenn arg ±∞ ist, wird es unverändert zurückgegeben.
  • Wenn arg ±0 ist, wird es unverändert zurückgegeben.
  • Wenn arg NaN ist, wird NaN zurückgegeben.

Hinweise

Der einzige Unterschied zwischen nearbyint und rint besteht darin, dass nearbyint niemals FE_INEXACT auslöst.

Die größten darstellbaren Gleitkommawerte sind exakte Ganzzahlen in allen Standard-Gleitkommaformaten, daher nearbyint überläuft niemals allein; jedoch kann das Ergebnis jeden Ganzzahltyp (einschließlich intmax_t ) überlaufen, wenn es in einer Ganzzahlvariable gespeichert wird.

Wenn der aktuelle Rundungsmodus FE_TONEAREST ist, rundet diese Funktion in Halbwegsfällen zur geraden Zahl (wie rint , aber anders als round ).

Beispiel

#include <fenv.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
// #pragma STDC FENV_ACCESS ON
    fesetround(FE_TONEAREST);
    printf("rounding to nearest:\nnearbyint(+2.3) = %+.1f  ", nearbyint(2.3));
    printf("nearbyint(+2.5) = %+.1f  ", nearbyint(2.5));
    printf("nearbyint(+3.5) = %+.1f\n", nearbyint(3.5));
    printf("nearbyint(-2.3) = %+.1f  ", nearbyint(-2.3));
    printf("nearbyint(-2.5) = %+.1f  ", nearbyint(-2.5));
    printf("nearbyint(-3.5) = %+.1f\n", nearbyint(-3.5));
    fesetround(FE_DOWNWARD);
    printf("rounding down: \nnearbyint(+2.3) = %+.1f  ", nearbyint(2.3));
    printf("nearbyint(+2.5) = %+.1f  ", nearbyint(2.5));
    printf("nearbyint(+3.5) = %+.1f\n", nearbyint(3.5));
    printf("nearbyint(-2.3) = %+.1f  ", nearbyint(-2.3));
    printf("nearbyint(-2.5) = %+.1f  ", nearbyint(-2.5));
    printf("nearbyint(-3.5) = %+.1f\n", nearbyint(-3.5));
    printf("nearbyint(-0.0) = %+.1f\n", nearbyint(-0.0));
    printf("nearbyint(-Inf) = %+.1f\n", nearbyint(-INFINITY));
}

Ausgabe:

rounding to nearest:
nearbyint(+2.3) = +2.0  nearbyint(+2.5) = +2.0  nearbyint(+3.5) = +4.0
nearbyint(-2.3) = -2.0  nearbyint(-2.5) = -2.0  nearbyint(-3.5) = -4.0
rounding down:
nearbyint(+2.3) = +2.0  nearbyint(+2.5) = +2.0  nearbyint(+3.5) = +3.0
nearbyint(-2.3) = -3.0  nearbyint(-2.5) = -3.0  nearbyint(-3.5) = -4.0
nearbyint(-0.0) = -0.0
nearbyint(-Inf) = -inf

Referenzen

  • C23-Standard (ISO/IEC 9899:2024):
  • 7.12.9.3 Die nearbyint-Funktionen (S: TBD)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S: TBD)
  • F.10.6.3 Die nearbyint-Funktionen (S: TBD)
  • C17-Standard (ISO/IEC 9899:2018):
  • 7.12.9.3 Die nearbyint-Funktionen (S: TBD)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S: TBD)
  • F.10.6.3 Die nearbyint-Funktionen (S: TBD)
  • C11-Standard (ISO/IEC 9899:2011):
  • 7.12.9.3 Die nearbyint-Funktionen (S. 251-252)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S. 373-375)
  • F.10.6.3 Die nearbyint-Funktionen (S. 526)
  • C99-Standard (ISO/IEC 9899:1999):
  • 7.12.9.3 Die nearbyint-Funktionen (S. 232)
  • 7.22 Typgenerische Mathematik <tgmath.h> (S. 335-337)
  • F.9.6.3 Die nearbyint-Funktionen (S. 463)

Siehe auch

(C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99)
rundet auf eine Ganzzahl unter Verwendung des aktuellen Rundungsmodus mit
Ausnahme, falls das Ergebnis abweicht
(Funktion)
(C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99)
rundet auf die nächste Ganzzahl, bei Mittelwerten von Null weg
(Funktion)
ermittelt oder setzt die Rundungsrichtung
(Funktion)
C++-Dokumentation für nearbyint