Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write log function in c++

Tags:

c++

function

I'm trying to write the log function in my program. I found this on 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;
}

This calculates the log of 5.5 to be: 1.704708. When I put the log of 5.5 into my calculate I get: .740362. Why is this code giving incorrect values?

like image 709
B. Wilson Avatar asked Jun 22 '26 19:06

B. Wilson


1 Answers

log (5.5) is log with base 10 and is equal to 0.740362.

You are looking for the Natural log function, ie. with base e.

Use the cmath header, instead of the math.h header for natural log.

std::log (5.5); // Gives natural log with base e
std::log10 (5.5); // Gives common log with base 10

Read more here

like image 138
Madhav Datt Avatar answered Jun 25 '26 07:06

Madhav Datt