def getCohrenValue(size_of_selections, qty_of_selections, significance): size_of_selections += 1 partResult1 = significance / (size_of_selections - 1) params = [ partResult1, qty_of_selections, (size_of_selections - 1 - 1) * qty_of_selections ] fisher = f.isf(*params) result = fisher / (fisher + (size_of_selections - 1 - 1)) return _pydecimal.Decimal(result).quantize( _pydecimal.Decimal('.0001')).__float__()
def check_fillchar(i): try: c = chr(i) c.encode('utf-8').decode() format(P.Decimal(0), c + '<19g') return c except: return None
def p_from_triple(triple): sign, coeff, exp = triple if coeff < 0 or coeff >= 2**128: raise ValueError("value out of bounds for a uint128 triple") digits = tuple(int(c) for c in str(coeff)) return P.Decimal((sign, digits, exp))
def printit(testno, s, fmt, encoding=None): if not encoding: encoding = get_preferred_encoding() try: result = format(P.Decimal(s), fmt) fmt = str(fmt.encode(encoding))[2:-1] result = str(result.encode(encoding))[2:-1] if "'" in result: sys.stdout.write("xfmt%d format %s '%s' -> \"%s\"\n" % (testno, s, fmt, result)) else: sys.stdout.write("xfmt%d format %s '%s' -> '%s'\n" % (testno, s, fmt, result)) except Exception as err: sys.stderr.write("%s %s %s\n" % (err, s, fmt))
def check_ulpdiff(self, exact, rounded): # current precision p = context.p.prec # Convert infinities to the largest representable number + 1. x = exact if exact.is_infinite(): x = _dec_from_triple(exact._sign, '10', context.p.Emax) y = rounded if rounded.is_infinite(): y = _dec_from_triple(rounded._sign, '10', context.p.Emax) # err = (rounded - exact) / ulp(rounded) self.maxctx.prec = p * 2 t = self.maxctx.subtract(y, x) if context.c.flags[C.Clamped] or \ context.c.flags[C.Underflow]: # The standard ulp does not work in Underflow territory. ulp = self.harrison_ulp(y) else: ulp = self.standard_ulp(y, p) # Error in ulps. err = self.maxctx.divide(t, ulp) dir = self.rounding_direction(x, context.p.rounding) if dir == 0: if P.Decimal("-0.6") < err < P.Decimal("0.6"): return True elif dir == 1: # directed, upwards if P.Decimal("-0.1") < err < P.Decimal("1.1"): return True elif dir == -1: # directed, downwards if P.Decimal("-1.1") < err < P.Decimal("0.1"): return True else: # ROUND_05UP if P.Decimal("-1.1") < err < P.Decimal("1.1"): return True print("ulp: %s error: %s exact: %s c_rounded: %s" % (ulp, err, exact, rounded)) return False
def bin_resolve_ulp(self, t): """Check if results of _decimal's power function are within the allowed ulp ranges.""" # NaNs are beyond repair. if t.rc.is_nan() or t.rp.is_nan(): return False # "exact" result, double precision, half_even self.maxctx.prec = context.p.prec * 2 op1, op2 = t.pop[0], t.pop[1] if t.contextfunc: exact = getattr(self.maxctx, t.funcname)(op1, op2) else: exact = getattr(op1, t.funcname)(op2, context=self.maxctx) # _decimal's rounded result rounded = P.Decimal(t.cresults[0]) self.ulpdiff += 1 return self.check_ulpdiff(exact, rounded)
def rounding_direction(self, x, mode): """Determine the effective direction of the rounding when the exact result x is rounded according to mode. Return -1 for downwards, 0 for undirected, 1 for upwards, 2 for ROUND_05UP.""" cmp = 1 if x.compare_total(P.Decimal("+0")) >= 0 else -1 if mode in (P.ROUND_HALF_EVEN, P.ROUND_HALF_UP, P.ROUND_HALF_DOWN): return 0 elif mode == P.ROUND_CEILING: return 1 elif mode == P.ROUND_FLOOR: return -1 elif mode == P.ROUND_UP: return cmp elif mode == P.ROUND_DOWN: return -cmp elif mode == P.ROUND_05UP: return 2 else: raise ValueError("Unexpected rounding mode: %s" % mode)
def fuzz(): with open(sys.argv[1], errors="surrogateescape") as fp: data = fp.read() try: dp = decimal_py.Decimal(data) dc = decimal_c.Decimal(data) except ValueError: return except ZeroDivisionError: return except (decimal_py.InvalidOperation, decimal_c.InvalidOperation): return assert repr(dp) == repr(dc) assert dp.is_canonical() == dc.is_canonical() assert dp.is_finite() == dc.is_finite() assert dp.is_infinite() == dc.is_infinite() assert dp.is_nan() == dc.is_nan() assert dp.is_normal() == dc.is_normal() assert dp.is_snan() == dc.is_snan() assert dp.is_qnan() == dc.is_qnan() assert dp.is_signed() == dc.is_signed() assert dp.is_subnormal() == dc.is_subnormal() assert dp.is_zero() == dc.is_zero() try: assert repr(dp + dp) == repr(dc + dc) assert repr(dp - dp) == repr(dc - dc) assert repr(dp * dp) == repr(dc * dc) assert repr(dp.adjusted()) == repr(dc.adjusted()) assert repr(dp.as_tuple()) == repr(dc.as_tuple()) assert repr(dp.canonical()) == repr(dc.canonical()) assert repr(dp.compare(dp)) == repr(dc.compare(dc)) assert repr(dp.conjugate()) == repr(dc.conjugate()) assert repr(dp.copy_abs()) == repr(dc.copy_abs()) assert repr(dp.copy_negate()) == repr(dc.copy_negate()) assert repr(dp.copy_sign(dp)) == repr(dc.copy_sign(dc)) assert repr(dp.exp()) == repr(dc.exp()) assert repr(dp.fma(1, 1)) == repr(dc.fma(1, 1)) assert repr(dp.ln()) == repr(dc.ln()) assert repr(dp.log10()) == repr(dc.log10()) assert repr(dp.logb()) == repr(dc.logb()) assert repr(dp.logical_and(dp)) == repr(dc.logical_and(dc)) assert repr(dp.logical_invert()) == repr(dc.logical_invert()) assert repr(dp.logical_or(dp)) == repr(dc.logical_or(dc)) assert repr(dp.logical_xor(dp)) == repr(dc.logical_xor(dc)) assert repr(dp.max(dp)) == repr(dc.max(dc)) assert repr(dp.max_mag(dp)) == repr(dc.max_mag(dc)) assert repr(dp.min(dp)) == repr(dc.min(dc)) assert repr(dp.min_mag(dp)) == repr(dc.min_mag(dc)) assert repr(dp.next_minus()) == repr(dc.next_minus()) assert repr(dp.next_plus()) == repr(dc.next_plus()) assert repr(dp.next_toward(dp)) == repr(dc.next_toward(dc)) assert repr(dp.normalize()) == repr(dc.normalize()) assert repr(dp.number_class()) == repr(dc.number_class()) assert repr(dp.quantize(dp)) == repr(dc.quantize(dc)) assert repr(dp.radix()) == repr(dc.radix()) assert repr(dp.remainder_near(dp)) == repr(dc.remainder_near(dc)) assert repr(dp.rotate(dp)) == repr(dc.rotate(dc)) assert repr(dp.same_quantum(dp)) == repr(dc.same_quantum(dc)) assert repr(dp.scaleb(dp)) == repr(dc.scaleb(dc)) assert repr(dp.shift(dp)) == repr(dc.shift(dc)) assert repr(dp.sqrt()) == repr(dc.sqrt()) assert repr(dp.to_eng_string()) == repr(dc.to_eng_string()) assert repr(dp.to_integral()) == repr(dc.to_integral()) assert repr(dp.to_integral_exact()) == repr(dc.to_integral_exact()) assert repr(dp.to_integral_value()) == repr(dc.to_integral_value()) try: assert repr(dp.as_integer_ratio()) == repr(dc.as_integer_ratio()) assert repr(round(dp, 1)) == repr(round(dc, 1)) assert repr(float(dp)) == repr(float(dc)) assert repr(int(dp)) == repr(int(dc)) assert repr(math.floor(dp)) == repr(math.floor(dc)) assert repr(math.ceil(dp)) == repr(math.ceil(dc)) except OverflowError: pass except ValueError: pass except decimal_py.DecimalException: pass
def getFisherValue(f3, f4, significance): return _pydecimal.Decimal(abs(f.isf(significance, f4, f3))).quantize( _pydecimal.Decimal('.0001')).__float__()
def getStudentValue(f3, significance): return _pydecimal.Decimal(abs(t.ppf(significance / 2, f3))).quantize( _pydecimal.Decimal('.0001')).__float__()