std::log2,s...f,std::log2l(3)
π std::log2, std::log2f, std::log2l
okay bestie, welcome to the std::log2 page β straight out of the C++ Standard Library section of your man pages. this is the girlies-who-compute-binary logarithms club and honestly? it's giving math major with a minor in slaying. π
β¨ NAME β¨
std::log2, std::log2f, std::log2l β yep, that's literally the whole name. it's std::log2,std::log2f,std::log2l and we're not gonna pretend otherwise. π
πΈ Synopsis πΈ
Defined in header <cmath>
float log2 ( float num );
double log2 ( double num ); (until C++23)
long double log2 ( long double num );
/* floating-point-type */ (since C++23)
log2 ( /* floating-point-type */ num );
(constexpr since C++26)
float log2f( float num ); (1) (2) (since C++11)
(constexpr since C++26)
long double log2l( 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 log2 ( Integer num );
so here's the tea, numbered and everything:
- 1-3) computes the binary (base-2) logarithm of
num. that's it. that's the whole job. π¦
and since C++23, the library provides overloads of std::log2 for all cv-unqualified floating-point types as the type of the parameter. iconic behavior, honestly. β¨
- A) additional overloads are provided for all integer types, and they get treated as
double. (since C++11)
π§ Parameters π§
numβ a floating-point or integer value. that's the whole vibe check: gimme a number, any number. π«Ά
π Return value π
if no errors occur, you get the base-2 logarithm of num β that's log<sub>2</sub>(num) or lb(num), whichever notation your heart desires. π
if a domain error occurs, an implementation-defined value is returned (NaN where supported). π
if a pole error occurs, -HUGE_VAL, -HUGE_VALF, or -HUGE_VALL is returned. heads up, bestie.
π Error handling π
errors are reported as specified in math_errhandling. no cap.
- a domain error occurs if
numis less than zero. π© - a pole error may occur if
numis zero. π©
if the implementation supports IEEE floating-point arithmetic (IEC 60559), then boom:
- if the argument is
Β±0,-βis returned andFE_DIVBYZEROis raised. π³ - if the argument is
1,+0is returned. slay, easy. - if the argument is negative,
NaNis returned andFE_INVALIDis raised. bestie no π - if the argument is
+β,+βis returned. infinity squared energy. - if the argument is
NaN,NaNis returned. it is what it is. π
π Notes π
for integer num, the binary logarithm can be interpreted as the zero-based index of the most significant 1 bit in the input. that's the fun fact you drop at the function and everyone's like "wait that's actually so cool."
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::log2(num) has the same effect as std::log2(static_cast<double>(num)). iykyk. π
π¦ Example π¦
// Run this code
#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
int main()
{
std::cout << "log2(65536) = " << std::log2(65536) << '\n'
<< "log2(0.125) = " << std::log2(0.125) << '\n'
<< "log2(0x020f) = " << std::log2(0x020f)
<< " (highest set bit is in position 9)\n"
<< "base-5 logarithm of 125 = "
<< std::log2(125) / std::log2(5) << '\n';
// special values
std::cout << "log2(1) = " << std::log2(1) << '\n'
<< "log2(+Inf) = " << std::log2(INFINITY) << '\n';
// error handling
errno = 0;
std::feclearexcept(FE_ALL_EXCEPT);
std::cout << "log2(0) = " << std::log2(0) << '\n';
if (errno == ERANGE)
std::cout << " errno == ERANGE: " << std::strerror(errno) << '\n';
if (std::fetestexcept(FE_DIVBYZERO))
std::cout << " FE_DIVBYZERO raised\n";
}
β Possible output β
log2(65536) = 16
log2(0.125) = -3
log2(0x020f) = 9.04166 (highest set bit is in position 9)
base-5 logarithm of 125 = 3
log2(1) = 0
log2(+Inf) = inf
log2(0) = -inf
errno == ERANGE: Numerical result out of range
FE_DIVBYZERO raised
look at her go π the -inf at the bottom is the pole error doing its dramatic main character moment.
π See also π
log,logf,loglβ computes natural (base e) logarithm (ln(x)) (function) (C++11) (C++11)log10,log10f,log10lβ computes common (base 10) logarithm (log<sub>10</sub>(x)) (function) (C++11) (C++11)log1p,log1pf,log1plβ natural logarithm (to base e) of 1 plus the given number (ln(1+x)) (C++11) (function) (C++11) (C++11)exp2,exp2f,exp2lβ returns 2 raised to the given power (2<sup>x</sup>) (C++11) (function) (C++11) (C++11)- C documentation for
log2β for when you wanna hang out with the C girlies π
okay that's the whole page, you ate that fr fr. this lil masterpiece is brought to you by http://cppreference.com, version 2024.06.10 β big kiss to the std::log2,s...f,std::log2l(3) girlies, 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