PHP Doku:: Create GMP number - function.gmp-init.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzMathematische ErweiterungenGNU Multiple PrecisionGMP Funktionengmp_init

Ein Service von Reinhard Neidl - Webprogrammierung.

GMP Funktionen




(PHP 4 >= 4.0.4, PHP 5)

gmp_initCreate GMP number


resource gmp_init ( mixed $number [, int $base = 0 ] )

Creates a GMP number from an integer or string.



An integer or a string. The string representation can be decimal, hexadecimal or octal.


The base.

The base may vary from 2 to 36. If base is 0 (default value), the actual base is determined from the leading characters: if the first two characters are 0x or 0X, hexadecimal is assumed, otherwise if the first character is "0", octal is assumed, otherwise decimal is assumed.


Eine resource für einen GMP-Wert.


Version Beschreibung
5.3.2 The base was extended from 2 to 36, to 2 to 62 and -2 to -36.
4.1.0 The optional base parameter was added.



To use the extended base introduced in PHP 5.3.2, then PHP must be compiled against GMP 4.2.0 or greater.


Beispiel #1 Creating GMP number

$b gmp_init("0xFFFFDEBACDFEDF7200");



It is not necessary to call this function if you want to use integer or string in place of GMP number in GMP functions, like gmp_add(). Function arguments are automatically converted to GMP numbers, if such conversion is possible and needed, using the same rules as gmp_init().

4 BenutzerBeiträge:
- Beiträge aktualisieren...
charlie at oblivion dot cz
18.02.2007 13:55
gmp_* functions don't accept strings with a leading '+':
echo gmp_strval(gmp_init('+42'));      #0
echo gmp_strval(gmp_add('42', '+42')); #42
echo bcadd('+42', '+42');              #84
php at richardneill dot org
20.09.2006 12:30
Here's a way to parse a decimal (eg 3.25) into an integer and exponent:

if (preg_match("/^[0-9]+\.[0-9]+$/",$input)){
//Input is a base-10 decimal. Multiply as necessary to remove the decimal
     //point. Convert that to a gmp_resource, then decrement the exponent
     //to compensate.

$pieces=explode(".", $input);     //Split at the d.p.
$input="$pieces[0]$pieces[1]"//Remove the decimal point.

//Remove any leading zeros, or gmp_init will parse the number as octal.

if ($input==''){    //Deal with "0.0" which would otherwise be ''.
//exponent = (-)  the number of characters after the decimal point.
php at richardneill dot org
20.09.2006 12:14
Note: Leading zeros will make gmp_init parse this as octal.
Thus gmp_init(010) becomes 8. 

$a=010;              //8
$b="010" + 0;     //10
$c=gmp_strval(gmp_init(010));    //8
$d=gmp_strval(gmp_init("010")); //8

This behaviour is inconsistent: either $d should equal $b, or
$b should equal $a.
thomas dot hebinck at digionline dot de
18.08.2005 19:23
If you call a gmp_* function directly with an interger as parameter, this integer MUST NOT be 0:

for($i=-1;$i<=1;$i++) {  echo gmp_strval(gmp_add(2,gmp_mul(1,$i))) . ' ';  }

The result is 1 0 3 (wrong)

In this case you have to use gmp_init():

for($i=-1;$i<=1;$i++) {  echo gmp_strval(gmp_add(2,gmp_mul(1,gmp_init($i)))) . ' ';  }

The result is 1 2 3 (right)

Happy number crunching! :-)

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)