How do I find the difference between two values without knowing which is larger?

前端 未结 11 2211
借酒劲吻你
借酒劲吻你 2020-12-11 14:22

I was wondering if there was a function built into Python that can determine the distance between two rational numbers but without me telling it which number is larger. e.g.

相关标签:
11条回答
  • 2020-12-11 15:15

    If you are curious about a different answer, the following one-liners also work:

    • max(x - y, y - x)

    • max(x, y) - min(x, y)

    • (x - y) * math.copysign(1, x - y), or equivalently (d := x - y) * math.copysign(1, d) in Python ≥3.8

    • functools.reduce(operator.sub, sorted([x, y], reverse=True))

    Using abs(x - y) or equivalently abs(y - x) certainly is preferred.

    0 讨论(0)
  • 2020-12-11 15:15

    use this function.

    its the same convention you wanted. using the simple abs feature of python.

    also - sometimes the answers are so simple we miss them, its okay :)

    >>> def distance(x,y):
        return abs(x-y)
    
    0 讨论(0)
  • 2020-12-11 15:15

    abs function is definitely not what you need as it is not calculating the distance. Try abs (-25+15) to see that it's not working. A distance between the numbers is 40 but the output will be 10. Because it's doing the math and then removing "minus" in front. I am using this custom function:

    
    def distance(a, b):
        if (a < 0) and (b < 0) or (a > 0) and (b > 0):
            return abs( abs(a) - abs(b) )
        if (a < 0) and (b > 0) or (a > 0) and (b < 0):
            return abs( abs(a) + abs(b) )

    print distance(-25, -15) print distance(25, -15) print distance(-25, 15) print distance(25, 15)

    0 讨论(0)
  • 2020-12-11 15:16

    This does not address the original question, but I thought I would expand on the answer zinturs gave. If you would like to determine the appropriately-signed distance between any two numbers, you could use a custom function like this:

    import math
    
    def distance(a, b):
        if (a == b):
            return 0
        elif (a < 0) and (b < 0) or (a > 0) and (b > 0):
            if (a < b):
                return (abs(abs(a) - abs(b)))
            else:
                return -(abs(abs(a) - abs(b)))
        else:
            return math.copysign((abs(a) + abs(b)),b)
    
    print(distance(3,-5))  # -8
    
    print(distance(-3,5))  #  8
    
    print(distance(-3,-5)) #  2
    
    print(distance(5,3))   # -2
    
    print(distance(5,5))   #  0
    
    print(distance(-5,3))  #  8
    
    print(distance(5,-3))  # -8
    

    Please share simpler or more pythonic approaches, if you have one.

    0 讨论(0)
  • 2020-12-11 15:18

    If you plan to use the signed distance calculation snippet posted by phi (like I did) and your b might have value 0, you probably want to fix the code as described below:

    import math
    
    def distance(a, b):
        if (a == b):
            return 0
        elif (a < 0) and (b < 0) or (a > 0) and (b >= 0): # fix: b >= 0 to cover case b == 0
            if (a < b):
                return (abs(abs(a) - abs(b)))
            else:
                return -(abs(abs(a) - abs(b)))
        else:
            return math.copysign((abs(a) + abs(b)),b)
    

    The original snippet does not work correctly regarding sign when a > 0 and b == 0.

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