By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

log10

P: n/a
Just starting with C++ in MS VC6++.

Why does this:

return (log10(70));

produce 1 and not 1.845098 ?

I can see it is giving me the rounded integer number, but why?
The return type of the function is a double.
Thanks for any advice.

RBS

Apr 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <6Sb0g.101579$zI1.33994
@fe2.news.blueyonder.co.uk>,
ba***********@blueyonder.co.uk says...
Just starting with C++ in MS VC6++.

Why does this:

return (log10(70));

produce 1 and not 1.845098 ?


It's virtually impossible for us to guess without seeing
your actual code. As written, it shouldn't compile --
log10 is overloaded for float, double and long double
arguments. You're passing an int, so all of these are
equally acceptable possibilities, creating an ambiguity.
To get this to compile as C++, you could use something
like 'return log10(70.0);' instead. Since you're passing
a double, the overload taking a double becomes preferred
and the ambiguity is removed.

If it's compiling as-is, you're probably compiling the
code as C. In this case, my first guess at the problem is
that you're not including <math.h>, so the proper return
type for the function isn't known (to the compiler). In
that case, it assumes the function returns an int. That
means the compiler will take whatever bit pattern it
returns and assume it's an int. Since you defined your
function to return a double, it's then converting that
bit pattern to a double. The fact that it's close to the
value you expected is mostly accidental.

Both of these seem to give correct answers:

// C++
#include <math.h>
#include <iostream>

int main() {
std::cout << log10(70.0);
return 0;
}

// C (won't compile as C++)
#include <math.h>
#include <stdio.h>

int main() {
printf("%f\n", log10(70));
return 0;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 15 '06 #2

P: n/a
Ignore this, I had declared a variable as int in Main.

RBS

"RB Smissaert" <ba***********@blueyonder.co.uk> wrote in message
news:6S********************@fe2.news.blueyonder.co .uk...
Just starting with C++ in MS VC6++.

Why does this:

return (log10(70));

produce 1 and not 1.845098 ?

I can see it is giving me the rounded integer number, but why?
The return type of the function is a double.
Thanks for any advice.

RBS


Apr 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.