Python division

前端 未结 12 988
挽巷
挽巷 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 06:45

    You're using Python 2.x, where integer divisions will truncate instead of becoming a floating point number.

    >>> 1 / 2
    0
    

    You should make one of them a float:

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

    or from __future__ import division, which the forces / to adopt Python 3.x's behavior that always returns a float.

    >>> from __future__ import division
    >>> (10 - 20) / (100 - 10)
    -0.1111111111111111
    

提交回复
热议问题