名前空間
変種
操作

std::log, std::logf, std::logl

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

double      log ( double num );

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

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

            log ( const V& v_num );
(S) (C++26以降)
ヘッダー <cmath> で定義
template< class Integer >
double      log ( Integer num );
(A) (C++26 以降 constexpr)
1-3) e を底とする自然対数を num に対して計算します。ライブラリは、パラメータの型としてすべての cv-修飾されていない浮動小数点型に対する std::log のオーバーロードを提供します。(C++23 以降)
S) SIMD オーバーロードは、v_num に対して要素ごとの std::log を実行します。
(定義については、math-floating-point および deduced-simd-t を参照してください。)
(C++26以降)
A) すべての整数型に対する追加のオーバーロードが提供されます。これらは double として扱われます。
(C++11以降)

目次

[編集] パラメータ

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

[編集] 戻り値

エラーが発生しなかった場合、num の自然対数 (底 e) (ln(num) または loge(num)) が返されます。

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

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

[編集] エラー処理

エラーは 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 が返されます。

[編集] 注記

追加のオーバーロードは、(A) とまったく同じように提供される必要はありません。整数型の引数 num に対して、std::log(num)std::log(static_cast<double>(num)) と同じ効果を持つことを保証するのに十分であればよいのです。

[編集]

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
 
int main()
{
    std::cout << "log(1) = " << std::log(1) << '\n'
              << "base-5 logarithm of 125 = " << std::log(125) / std::log(5) << '\n';
 
    // special values
    std::cout << "log(1) = " << std::log(1) << '\n'
              << "log(+Inf) = " << std::log(INFINITY) << '\n';
 
    // error handling
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
 
    std::cout << "log(0) = " << std::log(0) << '\n';
 
    if (errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_DIVBYZERO))
        std::cout << "    FE_DIVBYZERO raised\n";
}

実行結果の例

log(1) = 0
base-5 logarithm of 125 = 3
log(1) = 0
log(+Inf) = inf
log(0) = -inf
    errno == ERANGE: Numerical result out of range
    FE_DIVBYZERO raised

[編集] 関連項目

(C++11)(C++11)
常用対数 (10 を底とする) を計算する (log10(x))
(関数) [編集]
(C++11)(C++11)(C++11)
与えられた数値の 2 を底とする対数 (log2(x))
(関数) [編集]
(C++11)(C++11)(C++11)
与えられた数値に 1 を足した値の自然対数 (e を底とする) (ln(1+x))
(関数) [編集]
(C++11)(C++11)
与えられたべき乗に累乗した e を返す (ex)
(関数) [編集]
複素数の自然対数、分枝切断は負の実軸に沿う
(関数テンプレート) [編集]
valarray の各要素に std::log 関数を適用します。
(function template) [編集]
English 日本語 中文(简体) 中文(繁體)