Namespaces
Variants

std:: cbrt, std:: cbrtf, std:: cbrtl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(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 in Header <cmath>
(1)
float cbrt ( float num ) ;

double cbrt ( double num ) ;

long double cbrt ( long double num ) ;
(bis C++23)
/*floating-point-type*/
cbrt ( /*floating-point-type*/ num ) ;
(seit C++23)
(constexpr seit C++26)
float cbrtf ( float num ) ;
(2) (seit C++11)
(constexpr seit C++26)
long double cbrtl ( long double num ) ;
(3) (seit C++11)
(constexpr seit C++26)
SIMD-Überladung (seit C++26)
Definiert in Header <simd>
template < /*math-floating-point*/ V >

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

cbrt ( const V & v_num ) ;
(S) (seit C++26)
Definiert in Header <cmath>
template < class Integer >
double cbrt ( Integer num ) ;
(A) (constexpr seit C++26)
1-3) Berechnet die Kubikwurzel von num . Die Bibliothek bietet Überladungen von std::cbrt für alle cv-unqualifizierten Gleitkommatypen als Typ des Parameters an. (since C++23)
S) Die SIMD-Überladung führt eine elementweise std::cbrt auf v_num aus.
(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

Wenn keine Fehler auftreten, wird die Kubikwurzel von num ( 3 num ) zurückgegeben.

Wenn ein Bereichsfehler aufgrund von Unterlauf auftritt, wird das korrekte Ergebnis (nach Rundung) zurückgegeben.

Fehlerbehandlung

Fehler werden gemeldet, wie in math_errhandling spezifiziert.

Wenn die Implementierung IEEE-Gleitkommaarithmetik (IEC 60559) unterstützt,

  • wenn das Argument ±0 oder ±∞ ist, wird es unverändert zurückgegeben.
  • wenn das Argument NaN ist, wird NaN zurückgegeben.

Hinweise

std :: cbrt ( num ) is not equivalent to std:: pow ( num, 1.0 / 3 ) because the rational number
1
3
is typically not equal to 1.0 / 3 and std::pow cannot raise a negative base to a fractional exponent. Moreover, std :: cbrt ( num ) usually gives more accurate results than std:: pow ( num, 1.0 / 3 ) (see example).

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 :: cbrt ( num ) dieselbe Wirkung hat wie std :: cbrt ( static_cast < double > ( num ) ) .

Beispiel

#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
int main()
{
    std::cout
        << "Normal use:\n"
        << "cbrt(729)       = " << std::cbrt(729) << '\n'
        << "cbrt(-0.125)    = " << std::cbrt(-0.125) << '\n'
        << "Special values:\n"
        << "cbrt(-0)        = " << std::cbrt(-0.0) << '\n'
        << "cbrt(+inf)      = " << std::cbrt(INFINITY) << '\n'
        << "Accuracy and comparison with `pow`:\n"
        << std::setprecision(std::numeric_limits<double>::max_digits10)
        << "cbrt(343)       = " << std::cbrt(343) << '\n'
        << "pow(343,1.0/3)  = " << std::pow(343, 1.0 / 3) << '\n'
        << "cbrt(-343)      = " << std::cbrt(-343) << '\n'
        << "pow(-343,1.0/3) = " << std::pow(-343, 1.0 / 3) << '\n';
}

Mögliche Ausgabe:

Normal use:
cbrt(729)       = 9
cbrt(-0.125)    = -0.5
Special values:
cbrt(-0)        = -0
cbrt(+inf)      = inf
Accuracy and comparison with `pow`:
cbrt(343)       = 7
pow(343,1.0/3)  = 6.9999999999999991
cbrt(-343)      = -7
pow(-343,1.0/3) = -nan

Siehe auch

(C++11) (C++11)
potenziert eine Zahl mit dem gegebenen Exponenten ( x y )
(Funktion)
(C++11) (C++11)
berechnet Quadratwurzel ( x )
(Funktion)
(C++11) (C++11) (C++11)
berechnet Hypotenuse x 2
+y 2
und x 2
+y 2
+z 2
(seit C++17)

(Funktion)