Exemple #1
0
 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)
Exemple #2
0
 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)
Exemple #3
0
 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)
Exemple #4
0
 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)
Exemple #5
0
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))
Exemple #6
0
 def gcd_long(a, b):
     return gcd(rbigint.fromlong(a), rbigint.fromlong(b)).tolong()
Exemple #7
0
 def gcd_long(a, b):
     return gcd(rbigint.fromlong(a), rbigint.fromlong(b)).tolong()
Exemple #8
0
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))