std::cbrt,s...f,std::cbrtl(3)
π std::cbrt, std::cbrtf, std::cbrtl π
okay bestie, we're in the C++ Standard Libary manual β yes, that's the typo in the header, iconic β and this is the std::cbrt,s...f,std::cbrtl(3) page. Cube roots, but make it fashion. π§β¨
πΈ NAME πΈ
std::cbrt, std::cbrtf, std::cbrtl β that's the whole name, no cap.
β¨ Synopsis β¨
All of these live in the header <cmath>, and here's the whole lineup, bestie:
Defined in header <cmath>
float cbrt ( float num );
double cbrt ( double num ); (until C++23)
long double cbrt ( long double num );
/* floating-point-type */ (since C++23)
cbrt ( /* floating-point-type */ num );
(constexpr since C++26)
float cbrtf( float num ); (1) (2) (since C++11)
(constexpr since C++26)
long double cbrtl( long double num ); (3) (since C++11)
(constexpr since C++26)
Additional overloads (since C++11)
Defined in header <cmath>
template< class Integer > (A)
(constexpr since C++26)
double cbrt ( Integer num );
So like, 1-3) computes the cube root of num. Slay.
The library provides overloads of std::cbrt for all cv-unqualified floating-point types as the type of the parameter. (since C++23)
A) Additional overloads are provided for all integer types, which are treated as double. (since C++11)
π Parameters π
numβ floating-point or integer value. That's the whole vibe.
π Return value π
If no errors occur, the cube root of num is returned β that's \(\small{\sqrt[3]{num} }\), like the little 3 over the β over num. π«Ά
If a range error occurs due to underflow, the correct result (after rounding) is returned.
π¦ Error handling π¦
Errors are reported as specified in math_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559), then:
- if the argument is
Β±0orΒ±β, it is returned, unchanged. - if the argument is
NaN,NaNis returned.
π Notes π
std::cbrt(num) is not equivalent to std::pow(num, 1.0 / 3) because the rational number \(\small{\frac1{3} }\) β that's 1 over 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). π accurate queen behavior.
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::cbrt(num) has the same effect as std::cbrt(static_cast<double>(num)).
π Example π
// Run this code
#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';
}
π Possible output π
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
π See also π
pow,powf(C++11),powl(C++11) β raises a number to the given power (\(\small{x^y}\)/x^y) (function).sqrt,sqrtf(C++11),sqrtl(C++11) β computes square root (\(\small{\sqrt{x}}\)/βx) (function).hypot(C++11),hypotf(C++11),hypotl(C++11) β computes square root of the sum of the squares of two or three (since C++17) given numbers (\(\scriptsize{\sqrt{x^2+y^2}}\)/βx2+y2), (\(\scriptsize{\sqrt{x^2+y^2+z^2}}\)/βx2+y2+z2) (since C++17) (function).- C documentation for
cbrt.
mwah π this lil page is from http://cppreference.com, dated 2024.06.10, and the manual name is std::cbrt,s...f,std::cbrtl(3) β that's the footer, but make it cute. periodt. π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms