如何在c ++中编写日志函数

B. *_*son 2 c++ function

我正在尝试在我的程序中编写日志功能.我在cplusplus.com上找到了这个:

/* log example */
#include <stdio.h>      /* printf */
#include <math.h>       /* log */

int main ()
{
  double param, result;
  param = 5.5;
  result = log (param);
  printf ("log(%f) = %f\n", param, result );
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这将计算5.5的日志为:1.704708.当我将5.5的日志放入我的计算中时,我得到:.740362.为什么此代码提供的值不正确?

Mad*_*att 5

log (5.5)是以10基数的log,等于0.740362.

您正在寻找自然日志功能,即.与基地e.

使用cmath标题而不是math.h自然日志的标题.

std::log (5.5); // Gives natural log with base e
std::log10 (5.5); // Gives common log with base 10
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多