in Blog Posts, Solution

log和logf

用C/C++处理数据的时候,如果数值比较大就得注意数据类型。比如这个log函数的问题。
log接受double类型的参数,而logf接受float类型。

比如这段代码:

#include 
#include 

int main()
{
    double x = 4.4e52;
    printf("log(%lf) = %f\n", x, log(x));
    printf("log(%lf) = %f\n", x, logf(x));
    return 0;
}

linux/gcc下的输出是

log(43999999999999999701721741671911630964315171445538816.000000) = 121.216029
log(43999999999999999701721741671911630964315171445538816.000000) = inf

Write a Comment

Comment