Python division

前端 未结 12 989
挽巷
挽巷 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:03

    It has to do with the version of python that you use. Basically it adopts the C behavior: if you divide two integers, the results will be rounded down to an integer. Also keep in mind that Python does the operations from left to right, which plays a role when you typecast.

    Example: Since this is a question that always pops in my head when I am doing arithmetic operations (should I convert to float and which number), an example from that aspect is presented:

    >>> a = 1/2/3/4/5/4/3
    >>> a
    0
    

    When we divide integers, not surprisingly it gets lower rounded.

    >>> a = 1/2/3/4/5/4/float(3)
    >>> a
    0.0
    

    If we typecast the last integer to float, we will still get zero, since by the time our number gets divided by the float has already become 0 because of the integer division.

    >>> a = 1/2/3/float(4)/5/4/3
    >>> a
    0.0
    

    Same scenario as above but shifting the float typecast a little closer to the left side.

    >>> a = float(1)/2/3/4/5/4/3
    >>> a
    0.0006944444444444445
    

    Finally, when we typecast the first integer to float, the result is the desired one, since beginning from the first division, i.e. the leftmost one, we use floats.

    Extra 1: If you are trying to answer that to improve arithmetic evaluation, you should check this

    Extra 2: Please be careful of the following scenario:

    >>> a = float(1/2/3/4/5/4/3)
    >>> a
    0.0
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-21 07:04

    You're casting to float after the division has already happened in your second example. Try this:

    float(20-10) / float(100-10)
    
    0 讨论(0)
  • 2020-11-21 07:07

    In Python 2.7, the / operator is an integer division if inputs are integers:

    >>>20/15
    1
    
    >>>20.0/15.0
    1.33333333333
    
    >>>20.0/15
    1.33333333333
    

    In Python 3.3, the / operator is a float division even if the inputs are integer.

    >>> 20/15
    1.33333333333
    
    >>>20.0/15
    1.33333333333
    

    For integer division in Python 3, we will use the // operator.

    The // operator is an integer division operator in both Python 2.7 and Python 3.3.

    In Python 2.7 and Python 3.3:

    >>>20//15
    1
    

    Now, see the comparison

    >>>a = 7.0/4.0
    >>>b = 7/4
    >>>print a == b
    

    For the above program, the output will be False in Python 2.7 and True in Python 3.3.

    In Python 2.7 a = 1.75 and b = 1.

    In Python 3.3 a = 1.75 and b = 1.75, just because / is a float division.

    0 讨论(0)
  • 2020-11-21 07:07

    Personally I preferred to insert a 1. * at the very beginning. So the expression become something like this:

    1. * (20-10) / (100-10)
    

    As I always do a division for some formula like:

    accuracy = 1. * (len(y_val) - sum(y_val)) / len(y_val)
    

    so it is impossible to simply add a .0 like 20.0. And in my case, wrapping with a float() may lose a little bit readability.

    0 讨论(0)
  • 2020-11-21 07:10

    I'm somewhat surprised that no one has mentioned that the original poster might have liked rational numbers to result. Should you be interested in this, the Python-based program Sage has your back. (Currently still based on Python 2.x, though 3.x is under way.)

    sage: (20-10) / (100-10)
    1/9
    

    This isn't a solution for everyone, because it does do some preparsing so these numbers aren't ints, but Sage Integer class elements. Still, worth mentioning as a part of the Python ecosystem.

    0 讨论(0)
提交回复
热议问题