Python division

前端 未结 12 992
挽巷
挽巷 2020-11-21 06:23

I was trying to normalize a set of numbers from -100 to 0 to a range of 10-100 and was having problems only to notice that even with no variables at all, this does not evalu

12条回答
  •  执笔经年
    2020-11-21 07:04

    You're putting Integers in so Python is giving you an integer back:

    >>> 10 / 90
    0
    

    If if you cast this to a float afterwards the rounding will have already been done, in other words, 0 integer will always become 0 float.

    If you use floats on either side of the division then Python will give you the answer you expect.

    >>> 10 / 90.0
    0.1111111111111111
    

    So in your case:

    >>> float(20-10) / (100-10)
    0.1111111111111111
    >>> (20-10) / float(100-10)
    0.1111111111111111
    

提交回复
热议问题