def fromfloat(f): # FIXME: this is the temporary not exact implementation assert isinstance(f, float) d = 1000000 n = int(f * d) from fractions import gcd _gcd = gcd(n, d) return W_Rational.fromint(n/_gcd, d/_gcd)
def fromfloat(f): # FIXME: this is the temporary not exact implementation assert isinstance(f, float) d = 1000000 n = int(f * d) from fractions import gcd _gcd = gcd(n, d) return W_Rational.fromint(n / _gcd, d / _gcd)
def frombigint(n, d=rbigint.fromint(1)): from pycket.arithmetic import gcd g = gcd(n, d) n = n.floordiv(g) d = d.floordiv(g) if d.eq(rbigint.fromint(1)): return W_Bignum.frombigint(n) return W_Rational(n, d)
def test_gcd_random(): from pycket.arithmetic import gcd for _ in range(100): a = random_bigint(100) b = random_bigint(100) c = random_bigint(100) # Commutative assert gcd(a, b) == gcd(b, a) # Idempotent assert gcd(a, a) == a assert gcd(b, b) == b # Associative assert gcd(a, gcd(b, c)) == gcd(gcd(a, b), c) a = a.abs() b = b.abs() if a.ge(b): assert gcd(a, b) == gcd(a.sub(b), b) else: assert gcd(a, b) == gcd(a, b.sub(a))
def gcd_long(a, b): return gcd(rbigint.fromlong(a), rbigint.fromlong(b)).tolong()