1
def height(cent):
    height= {}
    height["feet"]= int(cent/30.48)
    height["inch"]= int(cent%30.48)/2.54
    print (height)
height (182.88)
print (182.88/30.48)
print (182.88%30.48)

The output is: {'inch': 11, 'feet': 6}

6.0

30.479999999999993

Why does 182.88%30.48 not equal zero?

Tim D.
  • 11
  • 1
  • 1
    Why are you expecting it to be? `182.88 / 30.48` is 6.033, not 6 exactly. – chepner Oct 06 '17 at 22:08
  • 1
    [Similar question/answer](https://stackoverflow.com/questions/14763722/python-modulo-on-floats) – Kyle Oct 06 '17 at 22:09
  • Do as little floating-point math as possible. Compute `h = int(cent / 2.54)`, then use integer arithmetic to compute `feet, inches = divmod(h, 12)`. – chepner Oct 06 '17 at 22:12

1 Answers1

1

Because the value of 30.48 is really 30.4799.. This is because of the way that floating point numbers are stored in python. So when you are dividing 30.479999 by 182.88, the resulting rounded integer is 5 (i.e. 182.88 // 30.48 == 5). So the remainder is 30.47999...

Kevin K.
  • 1,327
  • 2
  • 13
  • 18