Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Compiled Languages > C   »   integer

 
Thread Tools Display Modes
Question
 
 
#1  
Old Nov 20, 2007, 10:22 AM
muziti
New Member
muziti is offline
 
Join Date: May 2007
Posts: 5
muziti See this member's comment history on his/her Profile page.
integer

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.

Reply With Quote
 
     

Answers
 
 
Old Nov 20, 2007, 10:50 AM   #2  
Junior Member
red_cartoon is offline
 
red_cartoon's Avatar
 
Join Date: Nov 2007
Posts: 52
red_cartoon See this member's comment history on his/her Profile page.
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.
  Reply With Quote
 
     
 
 
Old Nov 21, 2007, 05:39 AM   #3  
Full Member
asterisk_man is offline
 
asterisk_man's Avatar
 
Join Date: Nov 2006
Location: East coast of U.S.A.
Posts: 472
asterisk_man See this member's comment history on his/her Profile page.
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.
  Reply With Quote
 
     
 
 
Old Dec 18, 2007, 08:37 AM   #4  
New Member
anuragplanet is offline
 
Join Date: Dec 2007
Posts: 2
anuragplanet See this member's comment history on his/her Profile page.
Quote:
Originally Posted by muziti
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 dont know what the problem is coz my compiler isnt showing any problem and printing 100 as the answer
may b your math.h has been tampered
if u get any more such problems, try loading turbo c again and then use it
  Reply With Quote
 
     


Thread Tools
Display Modes

 
Similar Sponsors

Similar Threads
Question Asker Forum Answers Last Post
Holy Crap, I'm going to be a Dad. OMG Pregnancy & New Motherhood 5 Jun 30, 2007 09:09 AM
hormonal moment leads to haircut. crap. macrolover Pregnancy & New Motherhood 3 Jun 10, 2007 05:30 PM
Crap telephone conversations lisalou Relationships 4 Apr 4, 2007 05:53 PM
Okay, so I tore the crap out of my basement floor... murley Construction 5 May 31, 2005 11:41 AM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 03:24 AM.