PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<textdomaingmp_abs>
view the version of this page
Last updated: Sun, 02 May 2004

XXXVI. Funzioni GMP

Queste funzioni permettono di lavorare con numeri interi di lunghezza arbitraria usando le librerie GNU MP. In pratica per poter usufruire di queste funzioni, bisogna installare il supporto GMP usando la seguente opzione --with-gmp.

Puoi scaricare la libreria GMP dal sito http://www.swox.com/gmp/. Dove è possibile anche scaricare il manuale GMP.

Per usare queste funzioni è necessaria la versione 2 o superiore delle librerie GMP.

Queste funzioni sono state aggiunte in PHP 4.0.4.

Nota: Molte funzioni accettano argomenti numerici GMP, definiti come risorsepiù in basso. Comunque, molte di queste funzioni accetteranno anche normali argomenti numerici e stringhe, considerato ciò è quindi possibile convertire queste ultime in numero. Inoltre, se c'è una funzione che può operare velocemente su argomenti interi, questa potrebbe essere usata al posto della più lenta quando l'argomento fornito è un intero. Questo è fatto con chiarezza, così la logica vuole che tu possa utilizzare numeri interi in ogni funzione che richieda un numero GMP. Vedere anche la funzione gmp_init().

Attenzione

Se desideri specificare un "large integer" come costante, scrivilo tra virgolette come stringa. Se non lo fai, PHP interpreterà l'"integer literal" immediatamente, con una possibile perdita di precisione, ancora prima che la libreria GMP venga richiamata.

Esempio 1. Funzione fattoriale usando GMP

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

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

Questo calcolerà il fattoriale di 1000 (numero abbastanza grande) molto velocemente.

Sommario
gmp_abs -- Valore assoluto
gmp_add -- Somma di numeri
gmp_and -- AND logico
gmp_clrbit -- Pulisce bit
gmp_cmp -- Confronto di numeri
gmp_com -- Calcola il complemento a uno di 'a'
gmp_div_q -- Divide due numberi
gmp_div_qr -- Divide due numeri e restituisce quoziente e resto
gmp_div_r -- Resto di una divisione
gmp_div -- Divisione di numberi
gmp_divexact -- Divisione intera di numeri
gmp_fact -- Fattoriale
gmp_gcd -- Calcola il MCD
gmp_gcdext -- Calcola il MCD e moltiplicatori
gmp_hamdist -- Distanza dell'hamming
gmp_init -- Crea un numero GMP
gmp_intval -- Converte un numero GMP in un intero
gmp_invert -- Inversione di modulo
gmp_jacobi -- Simbolo di Jacobi
gmp_legendre -- Simbolo di Legendre
gmp_mod -- Modulo
gmp_mul -- Prodotto di numeri
gmp_neg -- Rende un numero negativo
gmp_or -- OR logico
gmp_perfect_square -- Controllo quadrato perfetto
gmp_popcount -- Conteggio della popolazione
gmp_pow -- Eleva un numero a potenza
gmp_powm -- Modulo di un elevamento a potenza.
gmp_prob_prime -- Controlla se il numero è "probabilmente primo"
gmp_random -- Generatore di numeri casuali
gmp_scan0 -- Ricerca per 0
gmp_scan1 -- Ricerca per 1
gmp_setbit -- Imposta bit
gmp_sign -- Segno di un numero
gmp_sqrt -- Radice quadrata
gmp_sqrtrm -- Radice quadrata con resto
gmp_strval -- Converte un numero GMP in una stringa
gmp_sub -- Sottrazione di numeri
gmp_xor -- XOR logico


add a note add a note User Contributed Notes
Funzioni GMP
richard-slater.co.uk
22-Feb-2004 07: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 07: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 10: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: Sun, 02 May 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Italia OnLine S.p.a.
Last updated: Fri May 21 04:11:23 2004 CEST