# 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) ;
Serial.print(i);
Serial.print("\t");
Serial.println(a);
}
``````

What might I be doing wrong?

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.

So you need to use log10().

Pete

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);
``````