Top Documents Download Gallery Repository
Are you looking for a Software Engineer? Check Here

24gmp Module

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.

24.1Operator

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

24.2Module Function

gmp.gcd
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
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
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.

24.3gmp.mpf Class

24.3.1Constructor

gmp.mpf
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.

24.3.2Method

gmp.mpf.get_default_prec
gmp.mpf.get_default_prec():static
Gets the default precision for gmp.mpf.

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

24.4gmp.mpq Class

24.4.1Constructor

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

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

24.4.2Method

gmp.mpq#cast@mpf
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.

24.5gmp.mpz Class

24.5.1Constructor

gmp.mpz
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.

24.6Extention to string Class

This module extends the string class with methods described here.

string#cast@mpf
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
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
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.

24.7Thanks

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


Copyright (C) 2011-2017 ypsitau