log2 c语言不用循环,log2 - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

在头文件中定义float       log2f( float arg );(1)(since C99)

double      log2( double arg );(2)(since C99)

long double log2l( long double arg );(3)(since C99)

Defined in header

#define log2( arg )(4)(since C99)

1-3)计算基数的2对数arg。

4)类型 - 通用宏:如果arg有类型long double,log2l被调用。否则,如果arg有整数类型或类型double,log2则调用。否则,log2f被调用。

参数

arg-浮点值

返回值

如果没有发生错误,基极- 2对数arg(日志

2(arg)或lb(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

笔记

对于整数arg,二进制对数可以解释为输入中最重要的1位的从零开始的索引。

#include #include #include #include #include #pragma STDC FENV_ACCESS ON

int main(void){    printf("log2(65536) = %f\n", log2(65536));    printf("log2(0.125) = %f\n", log2(0.125));    printf("log2(0x020f) = %f (highest set bit is in position 9)\n", log2(0x020f));    printf("base-5 logarithm of 125 = %f\n", log2(125)/log2(5));    // special values    printf("log2(1) = %f\n", log2(1));    printf("log2(+Inf) = %f\n", log2(INFINITY));    //error handling

errno = 0; feclearexcept(FE_ALL_EXCEPT);    printf("log2(0) = %f\n", log2(0));    if(errno == ERANGE) perror("    errno == ERANGE");    if(fetestexcept(FE_DIVBYZERO)) puts("    FE_DIVBYZERO raised");}

可能的输出:

log2(65536) = 16.000000log2(0.125) = -3.000000log2(0x020f) = 9.041659 (highest set bit is in position 9)base-5 logarithm of 125 = 3.000000log2(1) = 0.000000log2(+Inf) = inflog2(0) = -inf

errno == ERANGE: Numerical result out of range

FE_DIVBYZERO raised

参考

C11标准(ISO / IEC 9899:2011):7.12.6.10 log2函数(p:246)

7.25类型通用数学(p:373-375)

F.10.3.10 log2函数(p:522)

C99标准(ISO / IEC 9899:1999):7.12.6.10 log2函数(p:226)

7.22类型通用数学(p:335-337)

F.9.3.10 log2函数(p:459)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值