pow problem

B

berthelot samuel

Hi,
I'm trying to do this :
double term2 = 0.0;
term2 = pow (0.1, 2.0);
the only thing I get in term2 is 1. it should be 0.01. Why is that ? I
don't get it... pow() is supposed to work with double and to return
double...
Thx
Sam
 
G

grobbeltje

berthelot samuel said:
I'm trying to do this :
double term2 = 0.0;
term2 = pow (0.1, 2.0);
the only thing I get in term2 is 1. it should be 0.01. Why is that ? I
don't get it... pow() is supposed to work with double and to return
double...

did you try to reduce the code to smallest possible piece which
still shows the problem? and compiled it with all warnings on?

#include <math.h>
#include <stdio.h>

int main()
{
double term2=0.0;
term2=pow(0.1,2.0);
printf("%f\n",term2);
return 0;
}

compiled with 'gcc -W -Wall -pedantic pow.c -o pow -lm' gives
the correct result for me. the -lm is something my compiler needs
to include the math library that has 'pow'. maybe yours doesn't.

good luck!
 
E

Eric Sosman

berthelot said:
Hi,
I'm trying to do this :
double term2 = 0.0;
term2 = pow (0.1, 2.0);
the only thing I get in term2 is 1. it should be 0.01. Why is that ? I
don't get it... pow() is supposed to work with double and to return
double...

A hint for the future: When an inexplicable problem has
baffled you, you are perhaps the least-qualified person to
decide which parts of your code are relevant to the problem
and which are not involved. Take your current difficulty,
for example: I can think of two different possible causes,
yet you have prevented me from seeing enough of your code
to determine which is the actual problem. There is nothing
at all wrong with the two lines you have shown, hence they
are not the cause of the problem. The problem arises from
the pieces you have not shown, so I can only guess what it
might be.

"Doctor, I am in pain."
"Where does it hurt?"
"Not in my left knee."

Next time, please post the *smallest* *complete* program
that demonstrates your difficulty.

Likely cause #1: You forgot to #include <math.h>, which
declares the pow() function and thus makes it known to the
compiler.

Likely cause #2: `term2' is calculated correctly, but
there is something wrong with the way you display the value.
Perhaps you wrote `printf("%d\n", term2)', using "%d" when
you should have used something like "%g" or "%f".
 
M

Martin Dickopp

Hi,
I'm trying to do this :
double term2 = 0.0;
term2 = pow (0.1, 2.0);

Why do you initialize `term2' when you assign to it immediately
afterwards?
the only thing I get in term2 is 1. it should be 0.01. Why is that ? I
don't get it... pow() is supposed to work with double and to return
double...

Please post the smallest possible /complete/ program which exhibits the
problem. The program below prints `0.010000' on my system.


#include <stdio.h>
#include <math.h>

int main (void)
{
double term2 = 0.0;
term2 = pow (0.1, 2.0);
printf ("%f\n", term2);
return 0;
}


Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top