Gmp
A clone of ruby-gmp, a library providing Ruby bindings to GMP library
Install / Use
/learn @srawlins/GmpREADME
gmp
gmp is a Rubygem providing Ruby bindings to the GMP library, and to the MPFR library (optional). Here is the introduction paragraph at GMP's homepage:
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.
The main target applications for GMP are cryptography applications and research, Internet security applications, algebra systems, computational algebra research, etc.
GMP is carefully designed to be as fast as possible, both for small operands and for huge operands. The speed is achieved by using fullwords as the basic arithmetic type, by using fast algorithms, with highly optimised assembly code for the most common inner loops for a lot of CPUs, and by a general emphasis on speed.
GMP is faster than any other bignum library. The advantage for GMP increases with the operand sizes for many operations, since GMP uses asymptotically faster algorithms.
The first GMP release was made in 1991. It is continually developed and maintained, with a new release about once a year.
GMP is distributed under the GNU LGPL. This license makes the library free to use, share, and improve, and allows you to pass on the result. The license gives freedoms, but also sets firm restrictions on the use with non-free programs.
GMP is part of the GNU project. For more information about the GNU project, please see the official GNU web site.
GMP's main target platforms are Unix-type systems, such as GNU/Linux, Solaris, HP-UX, Mac OS X/Darwin, BSD, AIX, etc. It also is known to work on Windoze in 32-bit mode.
GMP is brought to you by a team listed in the manual.
GMP is carefully developed and maintained, both technically and legally. We of course inspect and test contributed code carefully, but equally importantly we make sure we have the legal right to distribute the contributions, meaning users can safely use GMP. To achieve this, we will ask contributors to sign paperwork where they allow us to distribute their work."
Support
- GMP 4.3.x and GMP 5.x are each supported against Ruby (MRI) 1.9.x, 2.0.0, and 2.1.0. GMP 5.1.x is the only version of GMP that has been tested recently.
- MPFR 3.x is supported.
- Other Ruby platforms (such as Rubinius and JRuby) should be supported, but have not been tested recently.
Previously
Previously, on version 0.5.47, this gem was verified as functioning in the following combinations:
<table border="0"> <tr> <th>Platform</th> <th>Ruby</th> <th>GMP (MPFR)</th> </tr> <tr> <td rowspan="3">Linux (Ubuntu NR 10.04) on x86 (32-bit)</td> <td>(MRI) Ruby 1.8.7</td> <td>GMP 4.3.1 (2.4.2)<br /> GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td>(MRI) Ruby 1.9.1</td> <td>GMP 4.3.1 (2.4.2)<br /> GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td>(MRI) Ruby 1.9.2</td> <td>GMP 4.3.1 (2.4.2)<br /> GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td rowspan="4">Linux (Ubuntu 10.04) on x86_64 (64-bit)</td> <td>(MRI) Ruby 1.8.7</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td>(MRI) Ruby 1.9.1</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td>(MRI) Ruby 1.9.2</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td>(RBX) Rubinius 1.1.0</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td rowspan="3">Mac OS X 10.6.8 on x86_64 (64-bit)</td> <td>(MRI) Ruby 1.8.7</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.5 (3.1.1)</td> </tr> <tr> <td>(MRI) Ruby 1.9.3</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.5 (3.1.1)</td> </tr> <tr> <td>(RBX) Rubinius 1.1.0</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td rowspan="3">Windows 7 on x86_64 (64-bit)</td> <td>(MRI) Ruby 1.8.7</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td>(MRI) Ruby 1.9.1</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td>(MRI) Ruby 1.9.2</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> <tr> <td>Windows XP on x86 (32-bit)</td> <td>(MRI) Ruby 1.9.1</td> <td>GMP 4.3.2 (2.4.2)<br /> GMP 5.0.1 (3.0.0)</td> </tr> </table>Authors
- Tomasz Wegrzanowski
- srawlins
Constants
The GMP module includes the following constants. Mathematical constants, such as pi, are defined under class methods of GMP::F, listed below.
GMP::GMP_VERSION- A string like "5.0.1"GMP::GMP_CC- The compiler used to compile GMPGMP::GMP_CFLAGS- The CFLAGS used to compile GMPGMP::GMP_BITS_PER_LIMBThe number of bits per limbGMP::GMP_NUMB_MAX- The maximum value that can be stored in the number part of a limb
If MPFR is available:
GMP::MPFR_VERSION- A string like "2.4.2"GMP::MPFR_PREC_MIN- The minimum precision availableGMP::MPFR_PREC_MAX- The maximum precision availableGMP::GMP_RNDN- The constant representing "round to nearest"GMP::GMP_RNDZ- The constant representing "round toward zero"GMP::GMP_RNDU- The constant representing "round toward plus infinity"GMP::GMP_RNDD- The constant representing "round toward minus infinity"
New in MPFR 3.0.0:
GMP::MPFR_RNDNGMP::MPFR_RNDZGMP::MPFR_RNDUGMP::MPFR_RNDDGMP::MPFR_RNDA- The constant representing "round away from zero"
Classes
The GMP module is provided with following classes:
GMP::Z- infinite precision integer numbersGMP::Q- infinite precision rational numbersGMP::F- arbitrary precision floating point numbersGMP::RandState- states of individual random number generators
Numbers are created by using new(). Constructors can take following arguments:
GMP::Z.new()
GMP::Z.new(Fixnum)
GMP::Z.new(Bignum)
GMP::Z.new(GMP::Z)
GMP::Z.new(Float)
GMP::Z.new(GMP::F)
GMP::Z.new(String)
GMP::Q.new()
GMP::Q.new(Fixnum)
GMP::Q.new(GMP::Z)
GMP::Q.new(GMP::Q)
GMP::Q.new(String)
GMP::Q.new(any GMP::Z initializer)
GMP::Q.new(Fixnum, Fixnum)
GMP::Q.new(any GMP::Z initializer, any GMP::Z initializer)
GMP::F.new()
GMP::F.new(GMP::Z, precision=0, rounding_mode=default)
GMP::F.new(GMP::Q, precision=0, rounding_mode=default)
GMP::F.new(GMP::F, precision=0, rounding_mode=default)
GMP::F.new(String, precision=0)
GMP::F.new(String, precision=0, base=10)
GMP::F.new(String, precision=0, base=10, rounding_mode=default)
GMP::F.new(Fixnum, precision=0, rounding_mode=default)
GMP::F.new(Bignum, precision=0, rounding_mode=default)
GMP::F.new(Float, precision=0, roundung_mode=default)
GMP::F.new_2exp(Fixnum, exp, precision=0, rounding_mode=default)
GMP::F.new_2exp(Bignum, exp, precision=0, rounding_mode=default)
GMP::F.new_2exp(GMP::Z, exp, precision=0, rounding_mode=default)
GMP::RandState.new(\[algorithm\] \[, algorithm_args\])
You can also call them as:
GMP.Z(args)
GMP.Q(args)
GMP.F(args)
GMP.RandState()
Methods
GMP::Z, GMP::Q and GMP::F
+ addition
- substraction
* multiplication
/ division
to_s convert to string. For GMP::Z and GMP::F, this
method takes one optional argument, a base. The
base can be a Fixnum in the ranges \[2, 62\] or
\[-36, -2\] or a Symbol: one of :bin, :oct,
:dec, or :hex.
inspect alias for #to_s
-@ negation
neg! in-place negation
abs absolute value
abs! in-place absolute value
coerce promotion of arguments
== equality test
<=>, >=, >, <=, < comparisons
class methods of GMP::Z
fac(n) factorial of n
2fac(n), double_fac(n) double factorial of n
mfac(n) m-multi-factorial of n
primorial(n) primorial of n
fib(n) nth Fibonacci number
fib2(n) nth and (n-1)th Fibonacci numbers
pow(n,m) n to mth power
GMP::Z and GMP::Q
swap efficiently swap contents of two objects, there
is no GMP::F.swap because various GMP::F objects
may have different precisions, which would make
them unswapable
GMP::Z
to_i convert to Fixnum or Bignum
add! in-place addition
sub! in-place subtraction
addmul!(b,c) in-place += b*c
submul!(b,c) in-place -= b*c
tdiv,fdiv,cdiv truncate, floor and ceil division
tmod,fmod,cmod truncate, floor and ceil modulus
>> shift right, floor
divisible?(b) true if divisible by b
congruent?(c,d) true if congruent to c modulus d
** power
powmod power modulo
Related Skills
node-connect
353.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
353.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
353.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。

