名前空間
変種
操作

std::log2, std::log2f, std::log2l

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
ヘッダー <cmath> で定義
(1)
float       log2 ( float num );

double      log2 ( double num );

long double log2 ( long double num );
(C++23まで)
/*浮動小数点数型*/
            log2 ( /*floating-point-type*/ num );
(C++23から)
(C++26 以降 constexpr)
float       log2f( float num );
(2) (C++11以降)
(C++26 以降 constexpr)
long double log2l( long double num );
(3) (C++11以降)
(C++26 以降 constexpr)
ヘッダー <simd> で定義
template< /*math-floating-point*/ V >

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

            log2 ( const V& v_num );
(S) (C++26以降)
ヘッダー <cmath> で定義
template< class Integer >
double      log2 ( Integer num );
(A) (C++26 以降 constexpr)
1-3) Computes the binary (base-2) logarithm of num. The library provides overloads of std::log2 for all cv-unqualified floating-point types as the type of the parameter.(since C++23)
S) The SIMD overload performs an element-wise std::log2 on v_num.
(定義については、math-floating-point および deduced-simd-t を参照してください。)
(C++26以降)
A) すべての整数型に対する追加のオーバーロードが提供されます。これらは double として扱われます。
(C++11以降)

目次

[edit] Parameters

num - 浮動小数点数または整数値

[edit] Return value

If no errors occur, the base-2 logarithm of num (log2(num) or lb(num)) is returned.

領域エラーが発生した場合、実装定義の値が返される (サポートされている場合はNaN)。

If a pole error occurs, -HUGE_VAL, -HUGE_VALF, or -HUGE_VALL is returned.

[edit] Error handling

エラーは math_errhandling で指定された通りに報告される。

Domain error occurs if num is less than zero.

Pole error may occur if num is zero.

実装がIEEE浮動小数点算術 (IEC 60559) をサポートしている場合、

  • If the argument is ±0, -∞ is returned and FE_DIVBYZERO is raised.
  • If the argument is 1, +0 is returned.
  • If the argument is negative, NaN is returned and FE_INVALID is raised.
  • 引数が +∞ の場合、+∞ が返されます。
  • 引数が NaN の場合、NaN が返されます。

[edit] Notes

For integer num, the binary logarithm can be interpreted as the zero-based index of the most significant 1 bit in the input.

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)).

[edit] Example

#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";
}

実行結果の例

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

[edit] See also

(C++11)(C++11)
自然対数 (e を底とする) を計算する (ln(x))
(関数) [編集]
(C++11)(C++11)
常用対数 (10 を底とする) を計算する (log10(x))
(関数) [編集]
(C++11)(C++11)(C++11)
与えられた数値に 1 を足した値の自然対数 (e を底とする) (ln(1+x))
(関数) [編集]
(C++11)(C++11)(C++11)
与えられたべき乗に累乗した 2 を返す (2x)
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)