class TestCalc: getcontext().prec = 10 @pytest.mark.parametrize("data", addatas, ids=addids) def check_add(self, data, calcu_m): c = Decimal(str(data["a"])) + Decimal(str(data["b"])) assert c == Decimal(str(data["expect"])) @pytest.mark.parametrize("data", subdatas, ids=subids) def check_sub(self, data): c = Decimal(str(data["a"])) - Decimal(str(data["b"])) assert c == Decimal(str(data["expect"])) @pytest.mark.parametrize("data", multdatas, ids=multids) def test_mult(self, data): c = Decimal(str(data["a"])) * Decimal(str(data["b"])) assert c == Decimal(str(data["expect"])) @pytest.mark.parametrize("data", divdatas, ids=divids) def test_div(self, data): try: c = Decimal(str(data["a"])) / Decimal(str(data["b"])) assert c == Decimal(str(data["expect"])) except Exception as e: print(e) raise e
def __init__(self, c_ctx=None, p_ctx=None): """Initialization is from the C context""" self.c = C.getcontext() if c_ctx is None else c_ctx self.p = P.getcontext() if p_ctx is None else p_ctx self.p.prec = self.c.prec self.p.Emin = self.c.Emin self.p.Emax = self.c.Emax self.p.rounding = self.c.rounding self.p.capitals = self.c.capitals self.settraps([sig for sig in self.c.traps if self.c.traps[sig]]) self.setstatus([sig for sig in self.c.flags if self.c.flags[sig]]) self.p.clamp = self.c.clamp
def stable(b, prec): def to_decimal(b): for m, n in metallic_ratio(b): yield Decimal(m) / Decimal(n) getcontext().prec = prec last = 0 for i, x in zip(count(), to_decimal(b)): if x == last: print(f"after {i} iterations:\n\t{x}") break last = x
def update_event(self, inp=-1): self.set_output_val(0, _pydecimal.getcontext())
print("\n# ======================================================================") print("# Calculating pi, 10000 iterations") print("# ======================================================================\n") to_benchmark = [pi_float, pi_decimal] if C is not None: to_benchmark.insert(1, pi_cdecimal) for prec in [9, 19]: print("\nPrecision: %d decimal digits\n" % prec) for func in to_benchmark: start = time.time() if C is not None: C.getcontext().prec = prec P.getcontext().prec = prec for i in range(10000): x = func() print("%s:" % func.__name__.replace("pi_", "")) print("result: %s" % str(x)) print("time: %fs\n" % (time.time()-start)) print("\n# ======================================================================") print("# Factorial") print("# ======================================================================\n") if C is not None: c = C.getcontext() c.prec = C.MAX_PREC c.Emax = C.MAX_EMAX