470,848 Members | 1,189 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

log10

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
2 3828
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
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.

Similar topics

1 post views Thread by bblochl | last post: by
17 posts views Thread by Filipe Martins | last post: by
7 posts views Thread by a.dheeraj.kumar | last post: by
3 posts views Thread by BernsteinVsTheDb | last post: by
14 posts views Thread by Peter Sprenger | last post: by
13 posts views Thread by ptn | last post: by
17 posts views Thread by spooler123 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.