def test_decimal(): if sys.version_info >= (3, ): # Check whether the _decimal package was built successfully import _decimal as decimal else: import decimal decimal.getcontext().prec = 6 print("1/7 =", decimal.Decimal(1) / decimal.Decimal(7)) decimal.getcontext().prec = 40 print("1/7 =", decimal.Decimal(1) / decimal.Decimal(7))
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
try: from datetime import timezone as timezone_cls except: timezone_cls = timezone try: from base64 import encodebytes, decodebytes except: from base64 import encodestring as encodebytes, decodestring as decodebytes try: import _decimal except: import decimal as _decimal default_decimal_context = _decimal.getcontext() _str2decimal = default_decimal_context.create_decimal _decimal2str = default_decimal_context.to_eng_string from _weakref import proxy as _proxy # ATOMIC = 1 # DICT = 2 # LIST = 3 # TUPLE = 4 # COMPLEX = 5 # END = 6 # REFERENCE = 7 # LABEL = 8 # ATTRIBUTE = 9 # KEY = 10
count = loc + len(recur_list) pivot = count continue recur_list.append(data_list[count]) recurring = max(recurring, len(recur_list)) else: break count += 1 return recurring if __name__ == '__main__': cal_time = time.time() limit = 1000 count = 3 longest_recur = 0 value = 0 getcontext().prec = 2000 while count < limit: remainder = str(Decimal(1) / Decimal(count)) data_list = list(remainder) data_list = data_list[2:] tmp_val = get_recurring(data_list) if tmp_val > longest_recur: longest_recur = tmp_val value = count count += 2 print( "the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part is %d" % value) print("calculation time is %f" % (time.time() - cal_time))
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
''' Create two classes that model a rectangle and a circle. The rectangle class should be constructed by length and width while the circle class should be constructed by radius. Write methods in the appropriate class so that you can calculate the area (of the rectangle and circle), perimeter (of the rectangle) and circumference of the circle. ''' import math from _decimal import getcontext, Decimal getcontext().prec = 4 pi = Decimal(math.pi) class Rectangle(): """creating class rectabgle""" def __init__(self, length, width): self.length = length self.width = width def __str__(self): return f"this is a rectangle with a width {self.width} and length {self.length}" def area(self): """method to calculate area""" area = self.length * self.width return area def perim(self): """method to calculate perimeter""" perim = self.width * self.length * 2