Maths help: log()

It’s been a very, very long time since I have used logarithms, so this may be more of a maths lesson than an Arduino question.

When trying to use log() in my Arduino code, I don’t get the results I’d expect. I had modelled the logarithm profile in Excel, so am surprised and confused by the same equation yielding different results. Even when I strip it back to a plain log(i), can triple check it with Windows Calc and Google, the result on the Arduino is 2.3 times more, for any value of i.

log(100) according to Google is 2.

log(100) according to Arduino is 4.61.

  float a;
  for (int i=0; i < 1000; i=i+10) {
    a = log(i) ;

What might I be doing wrong?

Many thanks for your input!

A few seconds spent with google will reveal that in C/C++, log() is the natural logarithm to the base e.

On the Arduino the log() function is the natural log, not log base 10.
See: Arduino - MathHeader

So you need to use log10().


The log() function expects a floating point number to be passed to it. You're passing an int. What happens with:

   a = log( (float) i);


You want log base 10.

You need to divide by log(10) to convert to common log.

Yeah, that factor of 2.3 should be a red flag right there that you're calculating natural logarithms.