"""Maths.""" from decimal import Decimal import math def find_multiple_of_n_nearest_x(n, x): """Find multiple of n nearest x.""" if n == 0: return None n = Decimal(int(n)) x = Decimal(str(x)) return int(n * round(x / n)) def percent_change(n1, n2): """Calculate the percent change from n1 to n2.""" n1 = Decimal(n1) n2 = Decimal(n2) if n1 == 0: return math.inf else: s = ((n2 - n1) / n1.copy_abs()) return s * 100 def euclidean_distance_2d(x1, y1, x2, y2): """Calculate the distance between two points in 2d space. Args: x1 (float): x coordinate of point 1. y1 (float): y coordinate of point 1. x2 (float): x coordinate of point 2. y2 (float): y coordinate of point 2. Returns: float: Distance between points 1 and 2. """ return math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2))