here's my 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?

use log10 --> base 10 logarithm, log() is natural logarithm!

>> What's the problem here?
Nothing. Your program produced expected results for me. I used VC++ 2008 Express.

Well, then

cout << "natural logarithm (base e) of 7.9454E-18 is " << log(7.9454E-18) 
<< "\ncommon logarithm (base 10) of 7.9454E-18 is " << log10(7.9454E-18);

//natural logarithm (base e) of 7.9454E-18 is -39.3739
//common logarithm (base 10) of 7.9454E-18 is -17.0999

krs,
tesu

when you start thinking that standard C libraries are "misbehaving", you need to take a step back and recheck your assumptions.

the most likely problems are

(1) a bug in your own code
(2) you dont understand the function like you think you do
...
(3) a distant but possible reason could be that you're using some crappy free compiler you found from some sketchy website. If you're doing that, well... stop doing that.

what you're NOT going to find is evidence of "misbehavior" in an ANSI C library.

what you're NOT going to find is evidence of "misbehavior" in an ANSI C library.

Yes, I agree. Those functions have been around for quite a few years not and testest by millions of programmers all over the world. The likelyhood that anyone will find a bug is next to zero, unless you are using some newfangled compiler that has not been well tested.

>> What's the problem here?
Nothing. Your program produced expected results for me. I used VC++ 2008 Express.

That was incorrect. I actually got the same results that you did.

Well, then

cout << "natural logarithm (base e) of 7.9454E-18 is " << log(7.9454E-18) 
<< "\ncommon logarithm (base 10) of 7.9454E-18 is " << log10(7.9454E-18);

//natural logarithm (base e) of 7.9454E-18 is -39.3739
//common logarithm (base 10) of 7.9454E-18 is -17.0999

krs,
tesu

I typed this in a spreadsheet on A1
7.95E-18

formula on A2 LOG(A1) resulted in the following
-17.09988423

Excel 2003 has both LOG() & LOG10(), why would the LOG() in spreadsheet be log10() in C?

unless you are using some newfangled compiler that has not been well tested.

I use Visual Studio 2005 Standard...

Excel 2003 has both LOG() & LOG10(), why would the LOG() in spreadsheet be log10() in C?

because EXCEL is written mainly for people who don't really understand undergraduate-level mathematics, whereas C is designed for all sorts of folks, including engineers, scientists and mathematicians.

the natural logarithm, sometimes written as "ln", is the logarithm to the base e... all other logarithm bases are arbitrary, whether it's base 2, or base 10, or base 27.3 ... just because you have 10 fingers and think base 10 should always be implied, doesn't mean that's the case for the natural sciences, engineering, or even finance.

so C is universal in its application of logarithmic bases.

log(x) is the natural log of x.
log10(x) is the base 10 log of x
log2(x) is the base 2 log of x

if you want to use any other base, fine: use the simple property of logarithms to change the base to any arbitrary value b

logb(x) = log(x) / log(b)


.

because EXCEL is written mainly for people who don't really understand undergraduate-level mathematics, whereas C is designed for all sorts of folks, including engineers, scientists and mathematicians.

the natural logarithm, sometimes written as "ln", is the logarithm to the base e... all other logarithm bases are arbitrary, whether it's base 2, or base 10, or base 27.3 ... just because you have 10 fingers and think base 10 should always be implied, doesn't mean that's the case for the natural sciences, engineering, or even finance.

so C is universal in its application of logarithmic bases.

log(x) is the natural log of x.
log10(x) is the base 10 log of x
log2(x) is the base 2 log of x

if you want to use any other base, fine: use the simple property of logarithms to change the base to any arbitrary value b

logb(x) = log(x) / log(b)


.

thnx 4 yo info...

thnx 4 yo info...

fo' shizzle


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.