Advertisement
Korotkodul

B. Точность

Oct 20th, 2024 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. from decimal import Decimal, getcontext
  2. from types import TracebackType
  3. from typing import Optional
  4.  
  5. class Precision:
  6.     old_prec = 3
  7.     new_prec = 3
  8.     def __init__(self, precision: int) -> None:
  9.         try:
  10.             precision = round(precision)
  11.         except Exception:
  12.             raise TypeError
  13.         if precision < 1:
  14.             raise ValueError
  15.         self.old_prec = getcontext().prec
  16.         self.new_prec = precision
  17.  
  18.     def __enter__(self) -> None:
  19.         #print("call __enter__")
  20.         getcontext().prec = self.new_prec
  21.  
  22.     def __exit__(self, exc_type, exc_value, exc_tb) -> None:
  23.         #print("call __exit__")
  24.         getcontext().prec = self.old_prec
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement