How to calculates the distance between two decimal numbers (absolute difference) in Python

1 Answer

0 votes
# Function that returns the distance between two decimal numbers
def distance_between(a, b):
    # The distance is the absolute value of the difference
    return abs(a - b)

# Test values
x1, y1 = 100.0, 45.0
x2, y2 = 100.0, -15.0
x3, y3 = -100.0, -125.0
x4, y4 = -600.0, 100.0

# Print results
print("Distance between", x1, "and", y1, "=", distance_between(x1, y1))
print("Distance between", x2, "and", y2, "=", distance_between(x2, y2))
print("Distance between", x3, "and", y3, "=", distance_between(x3, y3))
print("Distance between", x4, "and", y4, "=", distance_between(x4, y4))



"""
run:

Distance between 100.0 and 45.0 = 55.0
Distance between 100.0 and -15.0 = 115.0
Distance between -100.0 and -125.0 = 25.0
Distance between -600.0 and 100.0 = 700.0

"""

 



answered Jun 28 by avibootz

Related questions

...