首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::log

Defined in header <cmath>

float log( float arg );

(1)

double log( double arg );

(2)

long double log( long double arg );

(3)

double log( Integral arg );

(4)

(since C++11)

1-3%29计算自然%28碱基e%29对数arg...

4%29一组过载或接受任意参数的函数模板积分型等于2%29%28double29%。

参数

arg

-

value of floating-point or Integral type

返回值

如果没有错误发生,自然的%28碱基-e%29对数arg%28 ln%28 arg%29或log

返回E%28 arg%29%29。

如果发生域错误,则返回支持%29的实现定义值%28 NaN。

如果发生极差,-HUGE_VAL,,,-HUGE_VALF,或-HUGE_VALL会被归还。

错误处理

错误按数学[医]错误处理...

在以下情况下发生域错误arg小于零。

极错误可能发生在arg是零。

如果实现支持ieee浮点算法%28IEC 60559%29,

  • 如果参数为±0,则返回-∞FE_DIVBYZERO是被抚养的。
  • 如果参数为1,则返回+0。
  • 如果参数为否定,则返回NaNFE_INVALID是被抚养的。
  • 如果参数为+∞,则返回+∞
  • 如果参数为nan,则返回nan。

二次

代码语言:javascript
复制
#include <iostream>
#include <cmath>
#include <cerrno>
#include <cstring>
#include <cfenv>
#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";
}

二次

可能的产出:

二次

代码语言:javascript
复制
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

二次

另见

log10

computes common (base 10) logarithm (log10(x)) (function)

log2 (C++11)

base 2 logarithm of the given number (log2(x)) (function)

log1p (C++11)

natural logarithm (to base e) of 1 plus the given number (ln(1+x)) (function)

exp

returns e raised to the given power (ex) (function)

log(std::complex)

complex natural logarithm with the branch cuts along the negative real axis (function template)

log(std::valarray)

applies the function std::log to each element of valarray (function template)

c日志文件

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券