X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=math_utils.py;h=62771231bb67925483bcbf714fe2a8373b591058;hb=e024f55a3d5b652d0745b6978a28c83f62711e59;hp=2cacc2f628ee90761acdce30442cb3d1beb9f1de;hpb=497fb9e21f45ec08e1486abaee6dfa7b20b8a691;p=python_utils.git diff --git a/math_utils.py b/math_utils.py index 2cacc2f..6277123 100644 --- a/math_utils.py +++ b/math_utils.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import functools import math from typing import List from heapq import heappush, heappop @@ -43,7 +44,7 @@ def gcd_floats(a: float, b: float) -> float: def gcd_float_sequence(lst: List[float]) -> float: if len(lst) <= 0: - raise Exception("Need at least one number") + raise ValueError("Need at least one number") elif len(lst) == 1: return lst[0] assert len(lst) >= 2 @@ -60,6 +61,23 @@ def truncate_float(n: float, decimals: int = 2): return int(n * multiplier) / multiplier +def percentage_to_multiplier(percent: float) -> float: + multiplier = percent / 100 + multiplier += 1.0 + return multiplier + + +def multiplier_to_percent(multiplier: float) -> float: + percent = multiplier + if percent > 0.0: + percent -= 1.0 + else: + percent = 1.0 - percent + percent *= 100.0 + return percent + + +@functools.lru_cache(maxsize=1024, typed=True) def is_prime(n: int) -> bool: """Returns True if n is prime and False otherwise""" if not isinstance(n, int):