round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl
| Definiert in Header <math.h> |
||
| float roundf( float arg ); |
(1) | (seit C99) |
| double round( double arg ); |
(2) | (seit C99) |
| long double roundl( long double arg ); |
(3) | (seit C99) |
| Definiert in Header <tgmath.h> |
||
| #define round( arg ) |
(4) | (seit C99) |
| Definiert in Header <math.h> |
||
| long lroundf( float arg ); |
(5) | (seit C99) |
| long lround( double arg ); |
(6) | (seit C99) |
| long lroundl( long double arg ); |
(7) | (seit C99) |
| Definiert in Header <tgmath.h> |
||
| #define lround( arg ) |
(8) | (seit C99) |
| Definiert in Header <math.h> |
||
| long long llroundf( float arg ); |
(9) | (seit C99) |
| long long llround( double arg ); |
(10) | (seit C99) |
| long long llroundl( long double arg ); |
(11) | (seit C99) |
| Definiert in Header <tgmath.h> |
||
| #define llround( arg ) |
(12) | (seit C99) |
roundl, lroundl, llroundl aufgerufen. Andernfalls, wenn arg einen ganzzahligen Typ oder den Typ double hat, wird round, lround, llround aufgerufen. Andernfalls wird entsprechend roundf, lroundf, llroundf aufgerufen.Inhalt |
[edit] Parameter
| arg | - | Gleitkommawert |
[edit] Rückgabewert
Wenn keine Fehler auftreten, wird der nächstgelegene ganzzahlige Wert zu arg zurückgegeben, wobei die Hälfte der Fälle vom Nullpunkt weg gerundet wird.
Wenn ein Domänenfehler auftritt, wird ein implementierungsdefinierter Wert zurückgegeben.
[edit] Fehlerbehandlung
Fehler werden wie in math_errhandling angegeben gemeldet.
Wenn das Ergebnis von lround oder llround außerhalb des durch den Rückgabetyp darstellbaren Bereichs liegt, kann ein Domänenfehler oder ein Bereichsfehler auftreten.
Wenn die Implementierung IEEE-Gleitkomma-Arithmetik (IEC 60559) unterstützt
- Für die Funktionen
round,roundfundroundl- Der aktuelle Rundungsmodus hat keine Auswirkung.
- 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.
- Für die Funktionsfamilien
lroundundllround- FE_INEXACT wird niemals ausgelöst.
- Der aktuelle Rundungsmodus hat keine Auswirkung.
- Wenn arg ±∞ ist, wird FE_INVALID ausgelöst und ein implementierungsdefinierter Wert zurückgegeben.
- Wenn das Ergebnis der Rundung außerhalb des Bereichs des Rückgabetyps liegt, wird FE_INVALID ausgelöst und ein implementierungsdefinierter Wert zurückgegeben.
- Wenn arg NaN ist, wird FE_INVALID ausgelöst und ein implementierungsdefinierter Wert zurückgegeben.
[edit] Anmerkungen
FE_INEXACT kann (muss aber nicht) von round ausgelöst werden, wenn eine nicht-ganzzahlige endliche Zahl gerundet wird.
Die größten darstellbaren Gleitkommazahlen sind exakte ganze Zahlen in allen standardmäßigen Gleitkommaformaten, daher überläuft round allein nie; das Ergebnis kann jedoch jeden Ganzzahltyp (einschließlich intmax_t) überlaufen, wenn es in einer Ganzzahlvariable gespeichert wird.
POSIX gibt an, dass alle Fälle, in denen lround oder llround FE_INVALID auslösen, Domänenfehler sind.
Die double-Version von round verhält sich so, als würde sie wie folgt implementiert:
[edit] Beispiel
#include <assert.h> #include <fenv.h> #include <float.h> #include <limits.h> #include <math.h> #include <stdio.h> // #pragma STDC FENV_ACCESS ON double custom_round(double x) { return signbit(x) ? ceil(x - 0.5) : floor(x + 0.5); } void test_custom_round() { const double sample[] = { 0.0, 2.3, 2.5 - DBL_EPSILON, 2.5, 2.5 + DBL_EPSILON, 2.7, INFINITY }; for (size_t t = 0; t < sizeof sample / sizeof(double); ++t) assert(round(+sample[t]) == custom_round(+sample[t]) && round(-sample[t]) == custom_round(-sample[t])); } int main(void) { // round printf("round(+2.3) = %+.1f ", round(2.3)); printf("round(+2.5) = %+.1f ", round(2.5)); printf("round(+2.7) = %+.1f\n", round(2.7)); printf("round(-2.3) = %+.1f ", round(-2.3)); printf("round(-2.5) = %+.1f ", round(-2.5)); printf("round(-2.7) = %+.1f\n", round(-2.7)); printf("round(-0.0) = %+.1f\n", round(-0.0)); printf("round(-Inf) = %+f\n", round(-INFINITY)); test_custom_round(); // lround printf("lround(+2.3) = %+ld ", lround(2.3)); printf("lround(+2.5) = %+ld ", lround(2.5)); printf("lround(+2.7) = %+ld\n", lround(2.7)); printf("lround(-2.3) = %+ld ", lround(-2.3)); printf("lround(-2.5) = %+ld ", lround(-2.5)); printf("lround(-2.7) = %+ld\n", lround(-2.7)); printf("lround(-0.0) = %+ld\n", lround(-0.0)); printf("lround(-Inf) = %+ld\n", lround(-INFINITY)); // FE_INVALID raised // error handling feclearexcept(FE_ALL_EXCEPT); printf("lround(LONG_MAX+1.5) = %ld\n", lround(LONG_MAX + 1.5)); if (fetestexcept(FE_INVALID)) puts(" FE_INVALID was raised"); }
Mögliche Ausgabe
round(+2.3) = +2.0 round(+2.5) = +3.0 round(+2.7) = +3.0
round(-2.3) = -2.0 round(-2.5) = -3.0 round(-2.7) = -3.0
round(-0.0) = -0.0
round(-Inf) = -inf
lround(+2.3) = +2 lround(+2.5) = +3 lround(+2.7) = +3
lround(-2.3) = -2 lround(-2.5) = -3 lround(-2.7) = -3
lround(-0.0) = +0
lround(-Inf) = -9223372036854775808
lround(LONG_MAX+1.5) = -9223372036854775808
FE_INVALID was raised[edit] Referenzen
- C23-Standard (ISO/IEC 9899:2024)
- 7.12.9.6 The round functions (p: TBD)
- 7.12.9.7 The lround and llround functions (p: TBD)
- 7.25 Typ-generische Mathematik <tgmath.h> (S. TBD)
- F.10.6.6 The round functions (p: TBD)
- F.10.6.7 The lround and llround functions (p: TBD)
- C17-Standard (ISO/IEC 9899:2018)
- 7.12.9.6 The round functions (p: 184)
- 7.12.9.7 The lround and llround functions (p: 184-185)
- 7.25 Typ-generische Mathematik <tgmath.h> (S. 272-273)
- F.10.6.6 The round functions (p: 384)
- F.10.6.7 The lround and llround functions (p: 385)
- C11-Standard (ISO/IEC 9899:2011)
- 7.12.9.6 The round functions (p: 253)
- 7.12.9.7 The lround and llround functions (p: 253)
- 7.25 Typ-generische Mathematik <tgmath.h> (S. 373-375)
- F.10.6.6 The round functions (p: 527)
- F.10.6.7 The lround and llround functions (p: 528)
- C99-Standard (ISO/IEC 9899:1999)
- 7.12.9.6 The round functions (p: 233)
- 7.12.9.7 The lround and llround functions (p: 234)
- 7.22 Typ-generische Mathematik <tgmath.h> (S. 335-337)
- F.9.6.6 The round functions (p: 464)
- F.9.6.7 The lround and llround functions (p: 464)
[edit] Siehe auch
| (C99)(C99) |
berechnet die größte ganze Zahl, die nicht größer als der gegebene Wert ist (Funktion) |
| (C99)(C99) |
berechnet die kleinste ganze Zahl, die nicht kleiner als der gegebene Wert ist (Funktion) |
| (C99)(C99)(C99) |
rundet auf die größte ganze Zahl, deren Betrag nicht größer als der gegebene Wert ist (Funktion) |
| C++-Dokumentation für round
| |