C++官网参考链接:https://cplusplus.com/reference/cmath/logb/
函数
<cmath> <ctgmath>
logb
C99
double logb(double x);
float logbf(float x);
long double logbl(long double x);
C++11
double logb(double x);
float logb(float x);
long double logb(long double x);
double logb (T x); // additional overloads for integral types
计算浮点数为底数的对数
返回|x|的对数,使用FLT_RADIX作为对数的基数。
在大多数平台上,FLT_RADIX为2,因此该函数等价于正值的log2。
C99
头文件<tgmath.h>提供了该函数的类型泛型宏版本。
C++11
在此头文件(<cmath>)中为整型(integral types)提供了额外的重载:这些重载在计算之前有效地将x转换为double类型(定义为T为任何整型(integral types))。
参数
计算其对数的值。
返回值
以FLT_RADIX为底x的对数。
如果x为零,则可能导致域错误或极点错误(或者没有错误,取决于库实现)。
如果发生域错误:
—math_errhandling设置了MATH_ERRNO:全局变量errno设置为EDOM。
—math_errhandling设置了MATH_ERREXCEPT:将引发FE_INVALID。
如果发生极点错误:
—math_errhandling设置了MATH_ERRNO:全局变量errno设置为ERANGE。
—math_errhandling设置了MATH_ERREXCEPT:引发FE_DIVBYZERO。
用例
/* logb example */
#include <stdio.h> /* printf */
#include <math.h> /* logb */
int main ()
{
double param, result;
param = 1024.0;
result = logb (param);
printf ("logb (%f) = %f.\n", param, result );
return 0;
}
输出:
另请参考
ilogb Integer binary logarithm (function) (以FLT_RADIX为底的对数(函数))
log2 Compute binary logarithm (function) (计算以2为底的对数(函数))
pow Raise to power (function) (求幂(函数))