Namespaces
Variants

std:: nearbyint, std:: nearbyintf, std:: nearbyintl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
nearbyint
(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>
(1)
float nearbyint ( float num ) ;

double nearbyint ( double num ) ;

long double nearbyint ( long double num ) ;
(bis C++23)
/*floating-point-type*/
nearbyint ( /*floating-point-type*/ num ) ;
(seit C++23)
float nearbyintf ( float num ) ;
(2) (seit C++11)
long double nearbyintl ( long double num ) ;
(3) (seit C++11)
SIMD-Überladung (seit C++26)
Definiert im Header <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

nearbyint ( const V & v_num ) ;
(S) (seit C++26)
Definiert im Header <cmath>
template < class Integer >
double nearbyint ( Integer num ) ;
(A)
1-3) Rundet das Fließkomma-Argument num auf einen ganzzahligen Wert im Fließkommaformat, unter Verwendung des aktuellen Rundungsmodus . Die Bibliothek stellt Überladungen von std::nearbyint für alle cv-unqualifizierten Fließkommatypen als Typ des Parameters bereit. (seit C++23)
S) Die SIMD-Überladung führt eine elementweise std::nearbyint -Operation auf v_num durch.
(Siehe math-floating-point und deduced-simd-t für deren Definitionen.)
(seit C++26)
A) Zusätzliche Überladungen werden für alle Ganzzahltypen bereitgestellt, die als double behandelt werden.
(since C++11)

Inhaltsverzeichnis

Parameter

num - Gleitkomma- oder Ganzzahlwert

Rückgabewert

Der nächstgelegene ganzzahlige Wert zu num wird entsprechend dem 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 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.

Hinweise

Der einzige Unterschied zwischen std::nearbyint und std::rint ist, dass std::nearbyint niemals FE_INEXACT auslöst.

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

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

Die zusätzlichen Überladungen müssen nicht exakt wie (A) bereitgestellt werden. Sie müssen lediglich sicherstellen, dass für ihr Argument num vom Ganzzahltyp std :: nearbyint ( num ) die gleiche Wirkung hat wie std :: nearbyint ( static_cast < double > ( num ) ) .

Beispiel

#include <cfenv>
#include <cmath>
#include <iostream>
#pragma STDC FENV_ACCESS ON
int main()
{
    std::fesetround(FE_TONEAREST);
    std::cout << "Rundung zur nächsten Ganzzahl: \n"
              << "nearbyint(+2.3) = " << std::nearbyint(2.3)
              << "  nearbyint(+2.5) = " << std::nearbyint(2.5)
              << "  nearbyint(+3.5) = " << std::nearbyint(3.5) << '\n'
              << "nearbyint(-2.3) = " << std::nearbyint(-2.3)
              << "  nearbyint(-2.5) = " << std::nearbyint(-2.5)
              << "  nearbyint(-3.5) = " << std::nearbyint(-3.5) << '\n';
    std::fesetround(FE_DOWNWARD);
    std::cout << "Abrundung:\n"
              << "nearbyint(+2.3) = " << std::nearbyint(2.3)
              << "  nearbyint(+2.5) = " << std::nearbyint(2.5)
              << "  nearbyint(+3.5) = " << std::nearbyint(3.5) << '\n'
              << "nearbyint(-2.3) = " << std::nearbyint(-2.3)
              << "  nearbyint(-2.5) = " << std::nearbyint(-2.5)
              << "  nearbyint(-3.5) = " << std::nearbyint(-3.5) << '\n';
    std::cout << "nearbyint(-0.0) = " << std::nearbyint(-0.0)  << '\n'
              << "nearbyint(-Inf) = " << std::nearbyint(-INFINITY) << '\n';
}

Ausgabe:

Rundung zur nächsten Ganzzahl: 
nearbyint(+2.3) = 2  nearbyint(+2.5) = 2  nearbyint(+3.5) = 4
nearbyint(-2.3) = -2  nearbyint(-2.5) = -2  nearbyint(-3.5) = -4
Abrundung:
nearbyint(+2.3) = 2  nearbyint(+2.5) = 2  nearbyint(+3.5) = 3
nearbyint(-2.3) = -3  nearbyint(-2.5) = -3  nearbyint(-3.5) = -4
nearbyint(-0.0) = -0
nearbyint(-Inf) = -inf

Siehe auch

(C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11)
nächstliegende Ganzzahl unter Verwendung des aktuellen Rundungsmodus mit
Ausnahme, falls das Ergebnis abweicht
(Funktion)
(C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11)
nächstliegende Ganzzahl, bei Mittelwerten von Null weg rundend
(Funktion)
(C++11) (C++11)
ermittelt oder setzt Rundungsrichtung
(Funktion)
C-Dokumentation für nearbyint