Namensräume
Varianten
Aktionen

std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl, std::llround, std::llroundf

Von cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
Definiert in der Header-Datei <cmath>
Runden auf Gleitkommatypen
(1)
float       round ( float num );

double      round ( double num );

long double round ( long double num );
(seit C++11)
(bis C++23)
constexpr /* gleitkommazahl-Typ */
            round ( /* floating-point-type */ num );
(seit C++23)
float       roundf( float num );
(2) (seit C++11)
(constexpr seit C++23)
long double roundl( long double num );
(3) (seit C++11)
(constexpr seit C++23)
Runden auf long
(4)
long lround ( float num );

long lround ( double num );

long lround ( long double num );
(seit C++11)
(bis C++23)
constexpr long lround( /* floating-point-type */ num );
(seit C++23)
long lroundf( float num );
(5) (seit C++11)
(constexpr seit C++23)
long lroundl( long double num );
(6) (seit C++11)
(constexpr seit C++23)
Runden auf long long
(7)
long long llround ( float num );

long long llround ( double num );

long long llround ( long double num );
(seit C++11)
(bis C++23)
constexpr long long llround( /* floating-point-type */ num );
(seit C++23)
long long llroundf( float num );
(8) (seit C++11)
(constexpr seit C++23)
long long llroundl( long double num );
(9) (seit C++11)
(constexpr seit C++23)
Definiert in der Header-Datei <cmath>
template< class Integer >
double round( Integer num );
(A) (seit C++11)
(constexpr seit C++23)
template< class Integer >
long lround( Integer num );
(B) (seit C++11)
(constexpr seit C++23)
template< class Integer >
long long llround( Integer num );
(C) (seit C++11)
(constexpr seit C++23)
1-3) Berechnet den nächstgelegenen ganzzahligen Wert von num (im Gleitkommaformat), wobei halbzahlige Fälle von Null weg gerundet werden, unabhängig vom aktuellen Rundungsmodus. Die Bibliothek stellt Überladungen von std::round für alle nicht-const-und-nicht-volatile Gleitkommatypen als Parameter-Typ für num bereit.(seit C++23)
4-9) Berechnet den nächstgelegenen ganzzahligen Wert von num (im Ganzzahlformat), wobei halbzahlige Fälle von Null weg gerundet werden, unabhängig vom aktuellen Rundungsmodus. Die Bibliothek stellt Überladungen von std::lround und std::llround für alle nicht-const-und-nicht-volatile Gleitkommatypen als Parameter-Typ für num bereit.(seit C++23)
A-C) Zusätzliche Überladungen werden für alle Ganzzahltypen bereitgestellt, die als double behandelt werden.

Inhalt

[edit] Parameter

num - Gleitkomma- oder Ganzzahlwert

[edit] Rückgabewert

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

Rückgabewert
math-round away zero.svg
num

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

[edit] Fehlerbehandlung

Fehler werden wie in math_errhandling beschrieben gemeldet.

Wenn das Ergebnis von std::lround oder std::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 Funktion std::round
  • Der aktuelle Rundungsmodus hat keinen Einfluss.
  • Wenn num ±∞ ist, wird es unverändert zurückgegeben.
  • Wenn num ±0 ist, wird es unverändert zurückgegeben.
  • Wenn num NaN ist, wird NaN zurückgegeben.
Für die Funktionen std::lround und std::llround
  • FE_INEXACT wird nie ausgelöst.
  • Der aktuelle Rundungsmodus hat keinen Einfluss.
  • Wenn num ±∞ 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 num NaN ist, wird FE_INVALID ausgelöst und ein implementierungsdefinierter Wert zurückgegeben.

[edit] Hinweise

FE_INEXACT kann ausgelöst werden (ist aber nicht zwingend erforderlich) von std::round, wenn ein endlicher Wert gerundet wird, der keine Ganzzahl ist.

Die größten darstellbaren Gleitkommawerte sind in allen Standard-Gleitkommaformaten exakte ganze Zahlen, daher kommt es bei std::round nie zu einem Überlauf; das Ergebnis kann jedoch bei Speicherung in einer Ganzzahlvariable jeden Ganzzahltyp (einschließlich std::intmax_t) überlaufen.

POSIX gibt an, dass alle Fälle, in denen std::lround oder std::llround FE_INEXACT auslösen, Domänenfehler sind.

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

#include <cfenv>
#include <cmath>
 
#pragma STDC FENV_ACCESS ON
 
double round(double x)
{
    const int save_round = std::fegetround();
    std::fesetround(FE_TOWARDZERO);
    const double result = std::rint(std::copysign(0.5 + std::fabs(x), x));
    std::fesetround(save_round);
    return result;
}

Die zusätzlichen Überladungen müssen nicht exakt wie (A-C) angegeben werden. Sie müssen nur ausreichen, um sicherzustellen, dass für ihr Argument num vom Ganzzahltyp

  • std::round(num) denselben Effekt hat wie std::round(static_cast<double>(num)).
  • std::lround(num) hat denselben Effekt wie std::lround(static_cast<double>(num)).
  • std::llround(num) hat denselben Effekt wie std::llround(static_cast<double>(num)).

[edit] Beispiel

#include <cassert>
#include <cfenv>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iostream>
 
// #pragma STDC FENV_ACCESS ON
 
double custom_round(double x)
{
    const int save_round = std::fegetround();
    std::fesetround(FE_TOWARDZERO);
    const double result = std::rint(std::copysign(0.5 + std::fabs(x), x));
    std::fesetround(save_round);
    return result;
}
 
void test_custom_round()
{
    for (const double x :
        {
            0.0, 0.3,
            0.5 - DBL_EPSILON / 2,
            0.5,
            0.5 + DBL_EPSILON / 2,
            0.7, 1.0, 2.3, 2.5, 2.7, 3.0,
            static_cast<double>(INFINITY)
        })
        assert(round(+x) == custom_round(+x) && round(-x) == custom_round(-x));
}
 
int main()
{
    test_custom_round();
 
    std::cout << std::showpos;
 
    // round
    std::cout << "round(+2.3) = " << std::round(2.3)
              << "  round(+2.5) = " << std::round(2.5)
              << "  round(+2.7) = " << std::round(2.7) << '\n'
              << "round(-2.3) = " << std::round(-2.3)
              << "  round(-2.5) = " << std::round(-2.5)
              << "  round(-2.7) = " << std::round(-2.7) << '\n';
 
    std::cout << "round(-0.0) = " << std::round(-0.0)  << '\n'
              << "round(-Inf) = " << std::round(-INFINITY) << '\n';
 
    // lround
    std::cout << "lround(+2.3) = " << std::lround(2.3)
              << "  lround(+2.5) = " << std::lround(2.5)
              << "  lround(+2.7) = " << std::lround(2.7) << '\n'
              << "lround(-2.3) = " << std::lround(-2.3)
              << "  lround(-2.5) = " << std::lround(-2.5)
              << "  lround(-2.7) = " << std::lround(-2.7) << '\n';
 
    std::cout << "lround(-0.0) = " << std::lround(-0.0)  << '\n'
              << "lround(-Inf) = " << std::lround(-INFINITY) << '\n';
 
    // error handling
    std::feclearexcept(FE_ALL_EXCEPT);
 
    std::cout << "std::lround(LONG_MAX+1.5) = "
              << std::lround(LONG_MAX + 1.5) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID was raised\n";
}

Mögliche Ausgabe

round(+2.3) = +2  round(+2.5) = +3  round(+2.7) = +3
round(-2.3) = -2  round(-2.5) = -3  round(-2.7) = -3
round(-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
std::lround(LONG_MAX+1.5) = -9223372036854775808
    FE_INVALID was raised

[edit] Siehe auch

(C++11)(C++11)
nächste ganze Zahl, die nicht größer ist als der gegebene Wert
(Funktion) [bearbeiten]
(C++11)(C++11)
nächste ganze Zahl, die nicht kleiner ist als der gegebene Wert
(Funktion) [bearbeiten]
(C++11)(C++11)(C++11)
nächste ganze Zahl, die nicht größer im Betrag ist als der gegebene Wert
(Funktion) [bearbeiten]
C-Dokumentation für round