f | from functools import wraps | f | from functools import wraps |
| | | |
| class Fix: | | class Fix: |
| | | |
| def __init__(self, n): | | def __init__(self, n): |
n | self.n = n | n | self.prec = n |
| | | |
| def __call__(self, func): | | def __call__(self, func): |
| | | |
| @wraps(func) | | @wraps(func) |
| def wrapper(*args, **kwargs): | | def wrapper(*args, **kwargs): |
n | rounded_args = [round(arg, self.n) if isinstance(arg, float) | n | rounded_args = [round(a, self.prec) if isinstance(a, float) |
| else arg for arg in args] | | else a for a in args] |
| rounded_kwargs = {k: round(v, self.n) if isinstance(v, float | | rounded_kwargs = {k: round(v, self.prec) if isinstance(v, fl |
| ) else v for k, v in kwargs.items()} | | oat) else v for k, v in kwargs.items()} |
| result = func(*rounded_args, **rounded_kwargs) | | result = func(*rounded_args, **rounded_kwargs) |
| if isinstance(result, float): | | if isinstance(result, float): |
t | return round(result, self.n) | t | return round(result, self.prec) |
| | | else: |
| return result | | return result |
| return wrapper | | return wrapper |