std::log2,s...f,std::log2l(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 num is less than zero. 🚩
  • a pole error may occur if num is zero. 🚩

if the implementation supports IEEE floating-point arithmetic (IEC 60559), then boom:

  • if the argument is Β±0, -∞ is returned and FE_DIVBYZERO is raised. 😳
  • if the argument is 1, +0 is returned. slay, easy.
  • if the argument is negative, NaN is returned and FE_INVALID is raised. bestie no 😭
  • if the argument is +∞, +∞ is returned. infinity squared energy.
  • if the argument is NaN, NaN is 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