非常教程

C参考手册

数值 | Numerics

log10

在头文件<math.h>中定义

float log10f( float arg );

(1)

(since C99)

double log10( double arg );

(2)

long double log10l( long double arg );

(3)

(since C99)

Defined in header <tgmath.h>

#define log10( arg )

(4)

(since C99)

1-3)计算公共(基数为10)的对数arg

4)类型 - 通用宏:如果arg有类型long doublelog10l被调用。否则,如果arg有整数类型或类型doublelog10则调用。否则,log10f被调用。

参数

arg

-

浮点值

返回值

如果没有错误发生,则以(log 10)的常见(以10为底)对数arg

10(arg)或lg(arg))。

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

如果发生极错误-HUGE_VAL-HUGE_VALF-HUGE_VALL返回。

错误处理

按照math_errhandling中的指定报告错误。

如果arg小于零,则会发生域错误。

如果arg为零,则可能出现极点错误。

如果实现支持IEEE浮点运算(IEC 60559),

  • 如果参数为±0,则返回-∞并FE_DIVBYZERO引发。
  • 如果参数是1,则返回+0
  • 如果参数是否定的,则返回并FE_INVALID提出NaN 。
  • 如果参数是+∞,则返回+∞
  • 如果参数是NaN,则返回NaN

#include <stdio.h>
#include <math.h>
#include <float.h>
#include <errno.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("log10(1000) = %f\n", log10(1000));
    printf("log10(0.001) = %f\n", log10(0.001));
    printf("base-5 logarithm of 125 = %f\n", log10(125)/log10(5));
    // special values
    printf("log10(1) = %f\n", log10(1));
    printf("log10(+Inf) = %f\n", log10(INFINITY));
    //error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("log10(0) = %f\n", log10(0));
    if(errno == ERANGE) perror("    errno == ERANGE");
    if(fetestexcept(FE_DIVBYZERO)) puts("    FE_DIVBYZERO raised");
}

可能的输出:

log10(1000) = 3.000000
log10(0.001) = -3.000000
base-5 logarithm of 125 = 3.000000
log10(1) = 0.000000
log10(+Inf) = inf
log10(0) = -inf
    errno == ERANGE: Numerical result out of range
    FE_DIVBYZERO raised

参考

  • C11标准(ISO / IEC 9899:2011):
    • 7.12.6.8 log10函数(p:245)
    • 7.25类型通用数学<tgmath.h>(p:373-375)
    • F.10.3.8 log10函数(p:522)
  • C99标准(ISO / IEC 9899:1999):
    • 7.12.6.8 log10函数(p:225-226)
    • 7.22类型通用数学<tgmath.h>(p:335-337)
    • F.9.3.8 log10函数(p:459)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 4.5.4.5 log10函数
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。