Namespaces
Variants

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

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
round lround llround
(C++11) (C++11) (C++11)
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
Definiert im Header <cmath>
Rundung auf Gleitkommatypen
(1)
float round ( float num ) ;

double round ( double num ) ;

long double round ( long double num ) ;
(seit C++11)
(bis C++23)
constexpr /* floating-point-type */
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)
Rundung zu long
(4)
long lround ( float num ) ;

long lround ( double num ) ;

long lround ( long double num ) ;
(seit C++11)
(bis C++23)
constexpr long lround ( /* Gleitkommatyp */ 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)
Rundung zu 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 ( /* Gleitkommatyp */ 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 im Header <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 zu num (im Fließkommaformat), wobei halbe Fälle von Null weg gerundet werden, unabhängig vom aktuellen Rundungsmodus. Die Bibliothek bietet Überladungen von std::round für alle cv-unqualifizierten Fließkommatypen als Typ des Parameters num an. (seit C++23)
4-9) Berechnet den nächstgelegenen ganzzahligen Wert zu num (im Ganzzahlformat), wobei halbe Fälle von Null weg gerundet werden, unabhängig vom aktuellen Rundungsmodus. Die Bibliothek bietet Überladungen von std::lround und std::llround für alle cv-unqualifizierten Gleitkommatypen als Typ des Parameters num an. (seit C++23)
A-C) Zusätzliche Überladungen werden für alle Ganzzahltypen bereitgestellt, die als double behandelt werden.

Inhaltsverzeichnis

Parameter

num - Gleitkomma- oder Ganzzahlwert

Rückgabewert

Wenn keine Fehler auftreten, wird der nächstgelegene ganzzahlige Wert zu num zurückgegeben, wobei halbe 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.

Fehlerbehandlung

Fehler werden gemeldet, wie in math_errhandling spezifiziert.

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

Für die std::round -Funktion:
  • Der aktuelle Rundungsmodus hat keine Auswirkung.
  • 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 niemals ausgelöst.
  • Der aktuelle Rundungsmodus hat keine Auswirkung.
  • Wenn num ±∞ 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 num NaN ist, wird FE_INVALID ausgelöst und ein implementierungsdefinierter Wert zurückgegeben.

Hinweise

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

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

POSIX spezifiziert 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, 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 als (A-C) bereitgestellt werden. Sie müssen lediglich ausreichen, um sicherzustellen, dass für ihr Argument num vom Ganzzahltyp:

  • std :: round ( num ) hat denselben Effekt 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 ) ) .

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;
    // runden
    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';
    // Fehlerbehandlung
    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 wurde ausgelöst\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 wurde ausgelöst

Siehe auch

(C++11) (C++11)
nächstliegende Ganzzahl, die nicht größer als der gegebene Wert ist
(Funktion)
(C++11) (C++11)
nächstliegende Ganzzahl, die nicht kleiner als der gegebene Wert ist
(Funktion)
(C++11) (C++11) (C++11)
nächstliegende Ganzzahl, die nicht größer im Betrag als der gegebene Wert ist
(Funktion)