PHP: GMP Functions - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<textdomaingmp_abs>
view the version of this page
Last updated: Thu, 15 Jul 2004

XXXVII. GMP Functions

�vod

These functions allow you to work with arbitrary-length integers using the GNU MP library.

These functions have been added in PHP 4.0.4.

Pozn�mka: Most GMP functions accept GMP number arguments, defined as resource below. However, most of these functions will also accept numeric and string arguments, given that it is possible to convert the latter to a number. Also, if there is a faster function that can operate on integer arguments, it would be used instead of the slower function when the supplied arguments are integers. This is done transparently, so the bottom line is that you can use integers in every function that expects GMP number. See also the gmp_init() function.

Varov�n�

If you want to explicitly specify a large integer, specify it as a string. If you don't do that, PHP will interpret the integer-literal first, possibly resulting in loss of precision, even before GMP comes into play.

Pozn�mka: Toto roz���en� nen� k dispozici na platform�ch Windows.

Po�adavky

You can download the GMP library from . This site also has the GMP manual available.

You will need GMP version 2 or better to use these functions. Some functions may require more recent version of the GMP library.

Instalace

In order to have these functions available, you must compile PHP with GMP support by using the --with-gmp option.

Konfigurace b�hu

Toto roz���en� nem� definov�no ��dn� konfigura�n� direktivy.

Typy prost�edk�

Toto roz���en� nem� definov�n ��dn� typ prost�edku (resource).

P�eddefinovan� konstanty

Tyto konstanty jsou definov�ny t�mto roz���en�m a budou k dispozici pouze tehdy, bylo-li roz���en� zkompilov�no spole�n� s PHP nebo dynamicky zavedeno za b�hu.

GMP_ROUND_ZERO (integer)

GMP_ROUND_PLUSINF (integer)

GMP_ROUND_MINUSINF (integer)

P��klady

P��klad 1. Factorial function using GMP

<?php
function fact($x)
{
   if (
$x <= 1) {
       return
1;
   } else {
       return
gmp_mul($x, fact($x-1));
   }
}

echo
gmp_strval(fact(1000)) . "\n";
?>

This will calculate factorial of 1000 (pretty big number) very fast.

Viz tak�

More mathematical functions can be found in the sections BCMath Arbitrary Precision Mathematics Functions and Mathematical Functions.

Obsah
gmp_abs -- Absolute value
gmp_add -- Add numbers
gmp_and -- Logical AND
gmp_clrbit -- Clear bit
gmp_cmp -- Compare numbers
gmp_com -- Calculates one's complement
gmp_div_q -- Divide numbers
gmp_div_qr -- Divide numbers and get quotient and remainder
gmp_div_r -- Remainder of the division of numbers
gmp_div -- Alias of gmp_div_q()
gmp_divexact -- Exact division of numbers
gmp_fact -- Factorial
gmp_gcd -- Calculate GCD
gmp_gcdext -- Calculate GCD and multipliers
gmp_hamdist -- Hamming distance
gmp_init -- Create GMP number
gmp_intval -- Convert GMP number to integer
gmp_invert -- Inverse by modulo
gmp_jacobi -- Jacobi symbol
gmp_legendre -- Legendre symbol
gmp_mod -- Modulo operation
gmp_mul -- Multiply numbers
gmp_neg -- Negate number
gmp_or -- Logical OR
gmp_perfect_square -- Perfect square check
gmp_popcount -- Population count
gmp_pow -- Raise number into power
gmp_powm -- Raise number into power with modulo
gmp_prob_prime -- Check if number is "probably prime"
gmp_random -- Random number
gmp_scan0 -- Scan for 0
gmp_scan1 -- Scan for 1
gmp_setbit -- Set bit
gmp_sign -- Sign of number
gmp_sqrt -- Calculate square root
gmp_sqrtrem --  Square root with remainder
gmp_strval -- Convert GMP number to string
gmp_sub -- Subtract numbers
gmp_xor -- Logical XOR


add a note add a note User Contributed Notes
GMP Functions
richard-slater.co.uk
22-Feb-2004 09:03
For those (like me) who are trying to do bit masking with very large numbers, here is a useful function to do the work for you.

<?php
 
function isBitSet($bitMask, $bitMap)
  {
   return (bool)
gmp_intval(gmp_div(gmp_and($bitMask, $bitMap),$bitMask));
  }
?>
helvecio_oliveira at yahoo dot com dot br
15-Oct-2003 08:51
=============================================================
A set of very nice functions to handle IP Address with gmplib:

The best way to store a range into a database is store:
dNet ..... decimal representation of a Net
dMask .... decimal representation of a Mask

All another parameters can be calculated.

<?
/*
f_ip2dec($a) ................... IP string to decimal
f_dec2ip($a) ................... decimal to IP string

f_dec2ipall($dNet,$dMask) ...... decimal Net and Mask to an Array with several IP parameters

f_dec2cidr($a) ................. decimal Mask to CIDR

f_and($a,$b) ................... and
f_or($a,$b) .................... or
f_xor($a,$b) ................... xor
f_not($a) ...................... not
f_dec2bin($a) .................. decimal to binary string
f_bin2dec($a) .................. binary string to decimal
*/

function f_and($a,$b){
$a=gmp_init(strval($a));
$b=gmp_init(strval($b));
$d=gmp_and($a,$b);
return
floatval(gmp_strval($d));
}

function
f_or($a,$b){
$a=gmp_init(strval($a));
$b=gmp_init(strval($b));
$d=gmp_or($a,$b);
return
floatval(gmp_strval($d));
}

function
f_xor($a,$b){
$a=gmp_init(strval($a));
$b=gmp_init(strval($b));
$d=gmp_xor($a,$b);
return
floatval(gmp_strval($d));
}

function
f_not($a){
$a=gmp_init(strval($a));
$d=gmp_strval($a,2);
$d=str_replace("1","x",$d);
$d=str_replace("0","1",$d);
$d=str_replace("x","0",$d);
$d=gmp_init($d,2);
return
floatval(gmp_strval($d,10));
}

function
f_dec2bin($a){
$a=gmp_init(strval($a));
return
gmp_strval($a,2);
}

function
f_bin2dec($a){
$a=gmp_init(strval($a),2);
return
floatval(gmp_strval($a,10));
}

function
f_ip2dec($a){
$d = 0.0;
$b = explode(".", $a,4);
for (
$i = 0; $i < 4; $i++) {
      
$d *= 256.0;
      
$d += $b[$i];
   };
return
$d;
}

function
f_dec2ip($a){
  
$b=array(0,0,0,0);
  
$c = 16777216.0;
  
$a += 0.0;
   for (
$i = 0; $i < 4; $i++) {
      
$k = (int) ($a / $c);
      
$a -= $c * $k;
      
$b[$i]= $k;
      
$c /=256.0;
   };
  
$d=join('.', $b);
   return(
$d);
}

function
f_dec2cidr($a){
$a=gmp_init(strval($a));
$d=strlen(str_replace("0","",gmp_strval($a,2)));
return
$d;
}

function
f_dec2ipall($dNet,$dMask){
 
$dWildCard=f_not($dMask);
 
$IpAll["Net"]=f_dec2ip($dNet);
 
$IpAll["Mask"]=f_dec2ip($dMask);
 
$IpAll["WildCard"]=f_dec2ip($dWildCard);
 
$IpAll["Cidr"]=f_dec2cidr($dMask);
 
$IpAll["Bcast"]=f_dec2ip(f_or($dNet,$dWildCard));
 
$IpAll["nIp"]=$dWildCard+1;
 
$IpAll["nIpUtil"]=$dWildCard-1;
  if(
$IpAll["nIp"] > 2){
      
$IpAll["IpFrom"]=f_dec2ip($dNet+1);
      
$IpAll["IpTo"]=f_dec2ip($dNet+$dWildCard-1);
  }
  else
  {
      
$IpAll["IpFrom"]="-";
      
$IpAll["IpTo"]="-";
      
$IpAll["nIpUtil"]=0;
  }
  return
$IpAll;
}

?>

=============================================================
GMP install steps in Mandrake 9.1:
----------------------------------------------------------
cp -r /usr/src/php-devel/extensions/gmp /tmp/gmp
cd /tmp/gmp
phpize
./configure
make install
echo "extension = gmp.so" > /etc/php/90_gmp.ini

Restart apache web server.
----------------------------------------------------------
gmp.so is in:
/usr/lib/php/extensions/

look in phpinfo, the string:
/etc/php/90_gmp.ini

Needs these tools:
autoconf
automake
libtool
m4
php430-devel-430-11mdk.rpm
all rpm�s that are envolved to run and compile gmp (*gmp*.rpm)

Some docs about self contained extensions:
/usr/share/doc/php430-devel-430/SELF-CONTAINED-EXTENSIONS
=============================================================
php at prezent dot nl
07-Jul-2003 11:57
Notice there are some broken gmp libs out there ..
if our code is not working try the following.
<code>
$BIT=gmp_init(0,10);
for( $i=0; $i<=50; $i++) {
     echo $i." ".gmp_scan1($BIT,$i).'<BR />';
}
</code>
if only one of them returns something other than -1
your libgmp is broken

<textdomaingmp_abs>
 Last updated: Thu, 15 Jul 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Sun Nov 14 23:09:54 2004 Local time zone must be set--see zic manual page