GUN多重精度运算库
维基百科,自由的百科全书
| 本条目没有列出任何参考或来源。(2012年3月23日) |
| 開發者 | GNU计划 |
|---|---|
| 初始版本 | 1991年The GNU Multiple Precision Arithmetic Library. [29 October 2011].</ref> |
| 程式語言 | C语言 |
| 作業系統 | 跨平台 |
| 類型 | 数学软件 |
| 許可協議 | LGPL |
| 網站 | gmplib.org |
GUN多重精度运算库(英语:GNU Multiple Precision Arithmetic Library,简称GMP或gmpal)是一个开源的任意精度运算库,支持正负数的整数、有理数、浮点数。它没有任何任何精度限制,只受限于可用内存。GMP有很多函数,它们都有一个规则的接口。它是C语言写成的,但用为其他很多语言做包装,包括Ada,C++,C#,OCaml,Perl,PHP和python。 GMP主要运用于密码程序和搜索,和计算机代数系统。 GMP的目标是成为最快的大数运算库 GMP是GNU项目的一部分,它在GNU宽通用公共许可证下发表。 GMP在许多计算机辅助代数系统中用于整数运算,如Mathematica和Maple。 GMP需要使用GNU(GCC编译器套装)编译。
示例 [编辑]
这是一个C语言示例,它展示了如何使用GMP做乘法运算并输出。
#include <stdio.h> #include <stdlib.h> #include <gmp.h> int main(void) { mpz_t x; mpz_t y; mpz_t result; mpz_init(x); mpz_init(y); mpz_init(result); mpz_set_str(x, "7612058254738945", 10); mpz_set_str(y, "9263591128439081", 10); mpz_mul(result, x, y); gmp_printf("\n %Zd\n*\n %Zd\n--------------------\n%Zd\n\n", x, y, result); /* free used memory 释放内存 */ mpz_clear(x); mpz_clear(y); mpz_clear(result); return EXIT_SUCCESS; }
这段代码计算 7612058254738945 和 9263591128439081 的乘积。
Compiling and running this program gives this result. (The -lgmp flag is used if compiling on Unix-type systems.)
7612058254738945
*
9263591128439081
--------------------
70514995317761165008628990709545
我们可以使用C++完成相同的运算。(如果在类Unix系统下编译,需要使用 -lgmpxx -lgmp)
#include <iostream> #include <gmpxx.h> int main() { mpz_class x("7612058254738945"); mpz_class y("9263591128439081"); std::cout << "\n " << x << "\n*\n " << y; std::cout << "\n--------------------\n" << x * y << "\n\n"; }
语言支持 [编辑]
| Library name | 语言 | 许可证 |
|---|---|---|
| GNU Multi-Precision Library | C, C++ | LGPL |
| Math::GMP | Perl | |
| GNU Multi-Precision Library for .NET | C#, .NET | LGPL |
| General Multiprecision Python Project | Python | |
| The RubyGems project | Ruby | |
| GNU Multi-Precision Library for PHP | PHP | PHP |
| GNU Multi-Precision Routines for SBCL | Common Lisp | |
| Ch GMP | Ch | |
| Glasgow Haskell Compiler (The implementation of Integeris basically a binding to GMP) |
Haskell | BSD |