# C math log() function misbehaves...

• 05-22-2008
csonx_p
C math log() function misbehaves...
Hi! , here's my code

Code:

#include <math.h>
#define M_PI 3.14159265358979323846

// Calculate Constant
double constant()
{
const double figure = 7.9454E-18;
double number = log (figure);
return -10 * number ;
}

The value in number should be -17.099... But i get -39.379... What's the problem here?
• 05-22-2008
fredb
The log function uses base e and you want base 10. The conversion is log(x) / log(10)
• 05-22-2008
matsp
You probably want log10(), not log(), the latter gives you the natural logarithm, and e^-39 is about 7.9E-18.

If you don't have log10() in your math library, log(x)/log(10) will be the same.

--
Mats
• 05-22-2008
Thantos
You are looking for the base 10 log but the log() function returns the natural (base e) log.
• 05-22-2008
Salem
And the wastage of brain power continues - http://www.daniweb.com/forums/thread125552.html

http://www.catb.org/~esr/faqs/smart-...ons.html#forum
Don't ask trivial questions on multiple forums. All you're going to get is a bunch of near identical answers on all of them.

It's fine to ask the same question on another forum if one doesn't provide the answer in a couple of days. But to broadcast it all over the place at the same time just smacks of http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
• 05-23-2008
csonx_p
Quote:

Originally Posted by Salem
And the wastage of brain power continues - http://www.daniweb.com/forums/thread125552.html

http://www.catb.org/~esr/faqs/smart-...ons.html#forum
Don't ask trivial questions on multiple forums. All you're going to get is a bunch of near identical answers on all of them.

It's fine to ask the same question on another forum if one doesn't provide the answer in a couple of days. But to broadcast it all over the place at the same time just smacks of http://www.catb.org/~esr/faqs/smart-...ns.html#urgent

Thanx for the info even though not helpful to my question... but i guess you learn few 'other' things by posing questions, and don't mind being reprimanded...
• 05-23-2008
csonx_p
Quote:

Originally Posted by fredb
The log function uses base e and you want base 10. The conversion is log(x) / log(10)

Quote:

You are looking for the base 10 log but the log() function returns the natural (base e) log.
Know, i'm not looking for the base 10, i know in math.h the function log10() exist... I used the calculator to test the log of 7.9454E-18... Now as you all noticed from Daniweb forum some1 spoke about common algorithm and natural algorithm, how does log() in math.h relate to log function in the calculator and the log function from Excel spreadsheet? The -17.099 results as i said were found using scientific calculator and the LOG() function in excel 2003....
• 05-23-2008
matsp
Right, logarithms exist in "every base" - all of them work the same, x = base**logbase(x). One of those is the natural logarithm (with base e, where e = ~ 2.72). On your calculator, you probably have a "ln" button, which will give you the natural logarithm. And as explained earlier, log of baseX can be converted to log of baseY by dividing log baseX(value) / log baseX(baseY).

--
Mats
• 05-23-2008
csonx_p
Quote:

Originally Posted by matsp
Right, logarithms exist in "every base" - all of them work the same, x = base**logbase(x). One of those is the natural logarithm (with base e, where e = ~ 2.72). On your calculator, you probably have a "ln" button, which will give you the natural logarithm. And as explained earlier, log of baseX can be converted to log of baseY by dividing log baseX(value) / log baseX(baseY).

--
Mats

Hi matsp, is it correct to say ln() in a calculator is the log() function in C, and log() in calculator is the log10() function in C....

If so, then something is not clear on how excel log functions work... LOG() & LOG10() both always yield same results... both LOG(7.9454E-18) & LOG10(7.9454E-18) yield to -17.099... Thing is i've been given a spreadsheet with all data & calculations/formulars and have to translate it into a program in C. Now what i did is i presumed whenever i see LOG() in spreadsheet i should use
log() in C and LOG10 should be log10(), which of cause is not necessary true in this case.... Learnt something though
• 05-23-2008
matsp
Excel does have two log function, the LOG() function would, in C++ style be defined as:
Code:

double log(double x, double base = 10);
The log10() is shorthand for Log(x, 10).

--
Mats
• 05-23-2008
csonx_p
Quote:

Originally Posted by matsp
Excel does have two log function, the LOG() function would, in C++ style be defined as:
Code:

double log(double x, double base = 10);
The log10() is shorthand for Log(x, 10).

--
Mats

Think you didn't get me well.. Do you mind testing both Excel LOG(7.9454E-18) & LOG10(7.9454E-18) and see if you get different results? That's where my problem is, i get same results...
• 05-23-2008
matsp
Quote:

Originally Posted by csonx_p
Think you didn't get me well.. Do you mind testing both Excel LOG(7.9454E-18) & LOG10(7.9454E-18) and see if you get different results? That's where my problem is, i get same results...

If I do, I suspect that means that I should send a bug report to Microsoft (unless the difference is in the 11th decimal point or so).

What I was trying to say is that log(x) == log10(x) in excel, but it also allows two parameters to log, e.g. log(8, 2), which would give you log2(8) [which is three]. So if you give only one parameter, you get log10, if you give two parameters, log(x, y) you get logy(x).

Does that make more sense?

--
Mats
• 05-23-2008
csonx_p
Quote:

Originally Posted by matsp
If I do, I suspect that means that I should send a bug report to Microsoft (unless the difference is in the 11th decimal point or so).

What I was trying to say is that log(x) == log10(x) in excel, but it also allows two parameters to log, e.g. log(8, 2), which would give you log2(8) [which is three]. So if you give only one parameter, you get log10, if you give two parameters, log(x, y) you get logy(x).

Does that make more sense?

--
Mats

Oh! eish... it's friday so forgive me..

Thnx
• 05-23-2008
csonx_p
Quote:

Originally Posted by matsp
If I do, I suspect that means that I should send a bug report to Microsoft (unless the difference is in the 11th decimal point or so).

What I was trying to say is that log(x) == log10(x) in excel, but it also allows two parameters to log, e.g. log(8, 2), which would give you log2(8) [which is three]. So if you give only one parameter, you get log10, if you give two parameters, log(x, y) you get logy(x).

Does that make more sense?

--
Mats

Interesting this

The Log base 10....

In C/C++: log10(x)... Excel: LOG10(x) == LOG(x) == LOG(x, 10),... Sci Calc: log

Log base e....

In C/C++: log(x) ... Excel: LN(x)... Sci Calc: ln

Can be confusing if using all to test a function...