25gmp Module

25.1Overview

The gmp module provides measures to calculate numbers with multiple precision using GMP library. To utilize it, import the gmp module using import function.

It expands features of operators like addition and multiplier so that they can calculate such numbers.

25.2Operator

Following tables show values types of operands and returned value for each operator:

+x gmp.mpz gmp.mpq gmp.mpf
-x gmp.mpz gmp.mpq gmp.mpf
~x gmp.mpz gmp.mpq gmp.mpf
x + y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz gmp.mpz gmp.mpq gmp.mpf gmp.mpf gmp.mpq
gmp.mpq gmp.mpz gmp.mpq gmp.mpf gmp.mpf gmp.mpq
gmp.mpf gmp.mpz gmp.mpq gmp.mpf gmp.mpf gmp.mpq
number gmp.mpz gmp.mpq gmp.mpf number rational
rational gmp.mpz gmp.mpq gmp.mpf rational rational
x - y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational
x * y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational
x / y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational
x % y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational

x == y; x != y; x > y; x < y; x >= y; x <= y; x <=> y

comparator gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational
x & y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational
x | y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational
x ^ y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational
x << y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational
x >> y gmp.mpz gmp.mpq gmp.mpf number rational
gmp.mpz
gmp.mpq
gmp.mpf
number
rational

x..; x .. y

25.3Module Function

gmp.gcd(num1:gmp.mpz, num2:gmp.mpz):map
Calculates the greatest common divisor, GCD, between num1 and num2 and returns the result as gmp.mpz.
gmp.lcm(num1:gmp.mpz, num2:gmp.mpz):map
Calculates the least common multiple, LCM, between num1 and num2 and returns the result as gmp.mpz.
gmp.sqrt(num):map

Calculates the square root of num.

The type of the argument num must be gmp.mpz, gmp.mpq, gmp.mpf or number.

25.4gmp.mpf Class

25.4.1Constructor

gmp.mpf(value?, prec?:number):map {block?}

Creates a gmp.mpf instance.

If the argument value is specified, it would be casted to gmp.mpf. Acceptable types for value are: number, string, gmp.mpf, gmp.mpz and gmp.mpq.

You can specify the precision of the number by the argument prec. If it's omitted, a default precision would be applied.

25.4.2Method

gmp.mpf.get_default_prec():static
Gets the default precision for gmp.mpf.
gmp.mpf.set_default_prec(prec:number):static:void
Sets the default precision for gmp.mpf.

25.5gmp.mpq Class

25.5.1Constructor

gmp.mpq(numer?, denom?:number):map {block?}

Creates a gmp.mpq instance.

You can call this function with one of the following form.

  • gmp.mpq(numer:number)
  • gmp.mpq(numer:number, denom:number)
  • gmp.mpq(str:string)
  • gmp.mpq(num:gmp.mpq)

25.5.2Method

gmp.mpq#cast@mpf() {block?}

Casts the value to gmp.mpf.

If block is specified, it would be evaluated with a block parameter |num:gmp.mpf|, where num is the created instance. In this case, the block's result would become the function's returned value.

25.6gmp.mpz Class

25.6.1Constructor

gmp.mpz(value?):map {block?}

Creates a gmp.mpz instance.

If the argument value is specified, it would be casted to gmp.mpz. Acceptable types for value are: number, string, gmp.mpf and gmp.mpz.

25.7Extention to string Class

This module extends the string class with methods described here.

string#cast@mpf(prec?:number):map

Casts the string to gmp.mpf.

You can specify the precision of the number by the argument prec. If it's omitted, a default precision would be applied.

If block is specified, it would be evaluated with a block parameter |num:gmp.mpf|, where num is the created instance. In this case, the block's result would become the function's returned value.

string#cast@mpq():map {block?}

Casts the string to gmp.mpq.

If block is specified, it would be evaluated with a block parameter |num:gmp.mpq|, where num is the created instance. In this case, the block's result would become the function's returned value.

string#cast@mpz(base?:number):map

Casts the string to gmp.mpz.

You can specify the basement of the number format by the argument base. If it's omitted, the basement would be decided by the prefix described in the string such as "0" and "0x".

If block is specified, it would be evaluated with a block parameter |num:gmp.mpz|, where num is the created instance. In this case, the block's result would become the function's returned value.

25.8Thanks

This module uses GMP and its forked project MPIR which are distributed in the following sites: