Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : GMP Functions : gmp_init

gmp_init

Create GMP number (PHP 4 >= 4.0.4, PHP 5)
resource gmp_init ( mixed number [, int base] )

Example 794. Creating GMP number

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

Code Examples / Notes » gmp_init

php

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

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

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.
}
?>


charlie

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
?>


Change Language


Follow Navioo On Twitter
gmp_abs
gmp_add
gmp_and
gmp_clrbit
gmp_cmp
gmp_com
gmp_div_q
gmp_div_qr
gmp_div_r
gmp_div
gmp_divexact
gmp_fact
gmp_gcd
gmp_gcdext
gmp_hamdist
gmp_init
gmp_intval
gmp_invert
gmp_jacobi
gmp_legendre
gmp_mod
gmp_mul
gmp_neg
gmp_nextprime
gmp_or
gmp_perfect_square
gmp_popcount
gmp_pow
gmp_powm
gmp_prob_prime
gmp_random
gmp_scan0
gmp_scan1
gmp_setbit
gmp_sign
gmp_sqrt
gmp_sqrtrem
gmp_strval
gmp_sub
gmp_testbit
gmp_xor
eXTReMe Tracker