Property definitions

gmp $ BigInt :: defaultinit
# Multi precision Integer numbers.
class BigInt
    super Discrete
    super Numeric
    super FinalizableOnce

    redef type OTHER: BigInt

    private var val: NativeMPZ

    redef fun successor(i) do return self + i.to_bi
    redef fun predecessor(i) do return self - i.to_bi

    redef fun hash do return self.to_i

    redef fun <=>(i) do
        var res = val.cmp(i.val)
        if (res) < 0 then
            return -1
        else if (res) > 0 then
            return 1
        else
            return 0
        end
    end

    redef fun ==(i) do return i isa BigInt and (self <=> i) == 0
    redef fun <=(i) do return (self <=> i) <= 0
    redef fun <(i) do return (self <=> i) < 0
    redef fun >=(i) do return (self <=> i) >= 0
    redef fun >(i) do return (self <=> i) > 0


    #     assert(2.to_bi + 2.to_bi == 4.to_bi)
    redef fun +(i) do
        var res = new NativeMPZ
        val.add(res, i.val)
        return new BigInt(res)
    end

    #     assert(-(2.to_bi) == (-2).to_bi)
    redef fun - do
        var res = new NativeMPZ
        val.neg res
        return new BigInt(res)
    end

    #     assert(2.to_bi - 2.to_bi == 0.to_bi)
    redef fun -(i) do
        var res = new NativeMPZ
        val.sub(res, i.val)
        return new BigInt(res)
    end

    #     assert(2.to_bi * 2.to_bi == 4.to_bi)
    redef fun *(i) do
        var res = new NativeMPZ
        val.mul(res, i.val)
        return new BigInt(res)
    end

    #     assert(3.to_bi / 2.to_bi == 1.to_bi)
    redef fun /(i) do
        var res = new NativeMPZ
        val.tdiv_q(res, i.val)
        return new BigInt(res)
    end

    # Modulo of `self` with `i`.
    #
    # Finds the remainder of the division of `self` by `i`.
    #
    #     assert(5.to_bi % 2.to_bi == 1.to_bi)
    fun %(i: BigInt): BigInt do
        var res = new NativeMPZ
        val.mod(res, i.val)
        return new BigInt(res)
    end

    # Returns `self` raised to the power of `e`.
    #
    #     assert(3.to_bi ** 2 == 9.to_bi)
    fun **(e: Int): BigInt do
        var res = new NativeMPZ
        var pow = new UInt64
        pow.set_si e
        val.pow_ui(res, pow)
        pow.free
        return new BigInt(res)
    end

    # The absolute value of `self`.
    #
    #     assert((-3).to_bi.abs == 3.to_bi)
    fun abs: BigInt do
        var res = new NativeMPZ
        val.abs res
        return new BigInt(res)
    end

    # Returns the greatest common divisor of `self` and `i`
    #
    #     assert(15.to_bi.gcd(10.to_bi) == 5.to_bi)
    fun gcd(i: BigInt): BigInt do
        var res = new NativeMPZ
        val.gcd(res, i.val)
        return new BigInt(res)
    end

    # Determine if `self` is a prime number.
    # Return 2 if `self` is prime, return 1 if `self` is probably prime and
    # return 0 if `self` is definitely not a prime number.
    #
    # This function begins by trying some divisions with small number to find if
    # there is other factors then `self` and one. After that, it uses the
    # Miller-Rabin probabilistic primality tests. The probability of a non-prime
    # being identified as probably prime with that test is less than
    # `4^(-reps)`. It is recommended to use a `reps` value between 15 and 50.
    #
    #     assert((0x10001).to_bi.probab_prime(15) == 2)
    fun probab_prime(reps: Int): Int do
        return val.probab_prime_p(reps.to_i32)
    end

    # Return the next prime number greater than `self`.
    # This fonction uses a probabilistic algorithm.
    #
    #     assert(11.to_bi.next_prime == 13.to_bi)
    fun next_prime: BigInt do
        var res = new NativeMPZ
        val.nextprime res
        return new BigInt(res)
    end

    #     assert(11.to_bi.zero == 0.to_bi)
    redef fun zero do return new BigInt(new NativeMPZ)

    #     assert(11.to_bi.value_of(4) == 4.to_bi)
    redef fun value_of(i) do return i.to_bi

    #     assert(11.to_bi.to_i == 11)
    redef fun to_i do return val.get_si

    #     assert(11.to_bi.to_f == 11.0)
    redef fun to_f do return val.get_d

    #     assert(11.to_bi.to_s == "11")
    redef fun to_s do
        var cstr = val.get_str(10.to_i32)
        var str = cstr.to_s
        cstr.free
        return str
    end

    redef fun to_bi do return self

    #     assert(123.to_bi.to_r == 123.to_r)
    redef fun to_r do
        var tmp = new NativeMPQ
        tmp.set_z val
        return new Ratio(tmp)
    end

    #     assert(3.to_bi.distance(6.to_bi) == -3)
    redef fun distance(i) do return (self - i).to_i

    redef fun finalize_once do val.finalize
end
lib/gmp/gmp.nit:120,1--287,3