Weird problem with float or double....

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2006
Posts: 6
Reputation: shafter111 is an unknown quantity at this point 
Solved Threads: 0
shafter111 shafter111 is offline Offline
Newbie Poster

Weird problem with float or double....

 
0
  #1
Mar 25th, 2006
simple problem....but I have no clue why its not working.....

  1. if (w[0] > m[1]):
  2. temp = ((w[0]-m[1])/w[0])*100
  3. print temp


For some reason temp is always 0.........I want a double percentage difference between the two values....

Please guys..help me out here......after 500 lines of code ...I am stuck here......my assignment is due tonight.....

how the hell do I get a float value?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Weird problem with float or double....

 
0
  #2
Mar 25th, 2006
Hi shafter111,

If you are dividing an integer by some other number and you want the quotient to be a float, you need to cast the integer to a float. In other words:
  1. a, b = 8, 3
  2. temp = ((a-b)/a)*100 # Gives you zero
While:
  1. a, b = 8, 3
  2. temp = (float(a-b)/a)*100 # Gives you 62.5
If you need greater precision than that provided by float(), look up the decimal module in the tutorial:

Guido van Rossum's Python Tutorial

Hope that helps.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 6
Reputation: shafter111 is an unknown quantity at this point 
Solved Threads: 0
shafter111 shafter111 is offline Offline
Newbie Poster

Re: Weird problem with float or double....

 
0
  #3
Mar 25th, 2006
Thanks man......really appretiate the help
you rock!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC