downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

gmp_intval> <gmp_hamdist
Last updated: Fri, 20 May 2011

view this page in

gmp_init

(PHP 4 >= 4.0.4, PHP 5)

gmp_initCrea un número GMP

Descripción

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

Crea un número GMP de un entero o cadena.

Parámetros

number

Un entero o cadena. La representación de la cadena puede ser decimal, hexadecimal or octal.

base

La base.

La base puede variar de 2 a 36. Si la base es 0 (valor por defecto), la actual base es determinada de los caracteres lideres: si los primeros dos caracteres son 0x o 0X, el hexadecimal es asumido, sin embargo si el primer carácter es "0", el octal es asumido, sin embargo el decimal es asumido.

Valores devueltos

Un número de recurso GMP.

Historial de cambios

Versión Descripción
5.3.2 La base fue extendidad de 2 a 36, a 2 a 62 y -2 a -36.
4.1.0 El parámetro opcional de la base fue agregado.

Notas

Note:

Para usar la base extendidad introducida en PHP 5.3.2, entonces PHP tiene que ser compilado en contra de GMP 4.2.0 o superior.

Ejemplos

Example #1 Creación de un número GMP

<?php
$a 
gmp_init(123456);
$b gmp_init("0xFFFFDEBACDFEDF7200");
?>

Notas

Note:

No es necesario llamar ésta función si se desea usar un entero o cadena en lugar de un número GMP en una función GMP, como gmp_add(). Los argumentos de la función son automáticamente convertidos a números GMP, si tal conversión es posible y necesaria, usando las mismas reglas como gmp_init().



gmp_intval> <gmp_hamdist
Last updated: Fri, 20 May 2011
 
add a note add a note User Contributed Notes gmp_init
karl dot debisschop at pearson dot com 29-Mar-2011 08:15
Unless the base is 16, gpm_init will fail if the string begins with "0b".

> php -r '$v = gmp_init("b83", 17); print("$v\n");'

Resource id #4

> php -r '$v = gmp_init("0b83", 17); print("$v\n");'

[nothing prints]

In may case, where I am explicitly specifying the base, the solution is to apply ltrim first:

> php -r '$v = gmp_init(ltrim("0b83", "0"), 17); print("$v\n");'

Resource id #4
charlie at oblivion dot cz 18-Feb-2007 12:55
gmp_* functions don't accept strings with a leading '+':
<?php
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-Sep-2006 10: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.

     $input=ltrim($input,'0');   
     //Remove any leading zeros, or gmp_init will parse the number as octal.

     if ($input==''){    //Deal with "0.0" which would otherwise be ''.
          $input=0;
      }
      $integer=gmp_init($input);  
      $ns_exponent=-strlen($pieces[1]); 
     //exponent = (-)  the number of characters after the decimal point.
}
?>
php at richardneill dot org 20-Sep-2006 10: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-Aug-2005 05: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! :-)

 
show source | credits | stats | sitemap | contact | advertising | mirror sites