Namespaces
Variants

round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl

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
round lround llround
(C99) (C99) (C99)
(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 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)
1-3) Berechnet den nächstgelegenen ganzzahligen Wert zu arg (im Fließkommaformat), wobei Halbwegsfälle von Null weg gerundet werden, unabhängig vom aktuellen Rundungsmodus.
5-7, 9-11) Berechnet den nächstgelegenen ganzzahligen Wert zu arg (im Ganzzahlformat), wobei Halbfälle von null weg gerundet werden, unabhängig vom aktuellen Rundungsmodus.
4,8,12) Typgenerische Makros: Wenn arg den Typ long double hat, werden roundl , lroundl , llroundl aufgerufen. Andernfalls, wenn arg einen Ganzzahltyp oder den Typ double hat, werden round , lround , llround aufgerufen. Andernfalls werden entsprechend roundf , lroundf , llroundf aufgerufen.

Inhaltsverzeichnis

Parameter

arg - Gleitkommawert

Rückgabewert

Wenn keine Fehler auftreten, wird der nächstgelegene ganzzahlige Wert zu arg , wobei halbe Fälle von Null weg gerundet werden, zurückgegeben.

Rückgabewert
math-round away zero.svg
Argument

Wenn ein Domänenfehler auftritt, wird ein implementierungsdefinierter Wert zurückgegeben.

Fehlerbehandlung

Fehler werden gemeldet, wie in math_errhandling festgelegt.

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-Gleitkommaarithmetik (IEC 60559) unterstützt:

Für die round -, roundf - und roundl -Funktion:
  • 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 lround - und llround -Funktionsfamilien:
  • 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 Wertebereichs 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.

Hinweise

FE_INEXACT kann (muss aber nicht) von round ausgelöst werden, wenn ein nicht-ganzzahliger endlicher Wert gerundet wird.

Die größten darstellbaren Fließkommawerte sind exakte Ganzzahlen in allen Standard-Fließkommaformaten, daher round führt niemals allein zu einem Überlauf; jedoch kann das Ergebnis jeden Ganzzahltyp (einschließlich intmax_t ) überlaufen, wenn es in einer Ganzzahlvariable gespeichert wird.

POSIX spezifiziert , dass alle Fälle, in denen lround oder llround FE_INVALID auslösen, Bereichsfehler sind.

Die double -Version von round verhält sich, als wäre sie wie folgt implementiert:

#include <math.h>
#pragma STDC FENV_ACCESS ON
double round(double x)
{
    return signbit(x) ? ceil(x - 0.5) : floor(x + 0.5);
}

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

Referenzen

  • C23-Standard (ISO/IEC 9899:2024):
  • 7.12.9.6 Die round-Funktionen (S.: TBD)
  • 7.12.9.7 Die lround- und llround-Funktionen (S.: TBD)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S.: TBD)
  • F.10.6.6 Die round-Funktionen (S.: TBD)
  • F.10.6.7 Die lround- und llround-Funktionen (S.: TBD)
  • C17-Standard (ISO/IEC 9899:2018):
  • 7.12.9.6 Die round-Funktionen (S: 184)
  • 7.12.9.7 Die lround- und llround-Funktionen (S: 184-185)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S: 272-273)
  • F.10.6.6 Die round-Funktionen (S: 384)
  • F.10.6.7 Die lround- und llround-Funktionen (S: 385)
  • C11-Standard (ISO/IEC 9899:2011):
  • 7.12.9.6 Die round-Funktionen (S. 253)
  • 7.12.9.7 Die lround- und llround-Funktionen (S. 253)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S. 373-375)
  • F.10.6.6 Die round-Funktionen (S. 527)
  • F.10.6.7 Die lround- und llround-Funktionen (S. 528)
  • C99-Standard (ISO/IEC 9899:1999):
  • 7.12.9.6 Die round-Funktionen (S: 233)
  • 7.12.9.7 Die lround- und llround-Funktionen (S: 234)
  • 7.22 Typgenerische Mathematik <tgmath.h> (S: 335-337)
  • F.9.6.6 Die round-Funktionen (S: 464)
  • F.9.6.7 Die lround- und llround-Funktionen (S: 464)

Siehe auch

berechnet die größte Ganzzahl, die nicht größer als der gegebene Wert ist
(Funktion)
(C99) (C99)
berechnet die kleinste Ganzzahl, die nicht kleiner als der gegebene Wert ist
(Funktion)
(C99) (C99) (C99)
rundet auf die nächstgelegene Ganzzahl, die nicht größer im Betrag als der gegebene Wert ist
(Funktion)