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?

3
  • 1
    Why are you expecting it to be? 182.88 / 30.48 is 6.033, not 6 exactly. Commented Oct 6, 2017 at 22:08
  • 1
    Similar question/answer Commented Oct 6, 2017 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). Commented Oct 6, 2017 at 22:12

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.