PDA

View Full Version : Integer


muziti
Nov 20, 2007, 11:22 AM
why is this happening?
including all necessary header files ,math.h and stdio.h ,
int main()
{

int I=2;
int y=pow(10,I);
printf("%d",y);
return 0;
}


this program prints 99.
can u help;
thank u.

red_cartoon
Nov 20, 2007, 11:50 AM
a text book example of 'loss of precision due to conversion from floating type to integer type'

pow( x, y) returns a double. When you try to put that inside an int, you experience a roundoff error.

asterisk_man
Nov 21, 2007, 06:39 AM
pow is defined as:
double pow(double x, double y);
so the two integers you supply, 10 and I, are cast into doubles, the function is evaluated and a double is returned. When that double is then cast into an int so that it can be stored in y some precision is lost due to the way that floating point numbers are represented.

hope this helps.

anuragplanet
Dec 18, 2007, 09:37 AM
why is this happening?
including all necessary header files ,math.h and stdio.h ,
int main()
{

int i=2;
int y=pow(10,i);
printf("%d",y);
return 0;
}


this program prints 99.
can u help;
thank u.
I don't know what the problem is because my compiler isn't showing any problem and printing 100 as the answer
May be your math.h has been tampered
If you get any more such problems, try loading turbo c again and then use it