Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: GMP - Manual
[go: Go Back, main page]

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

search for in the

gmp_abs" width="11" height="7"/> <textdomain
Last updated: Sun, 25 Nov 2007

view this page in

GMP 関数

導入

以下の関数により、GNU MP ライブラリを使用して 任意長の整数を使用することが可能になります。

これらの関数は、PHP 4.0.4 で追加されました。

注意: 多くの GMP 関数は、resource で定義された GMP 数を 引数としてとります。しかし、これらの関数の多くは、数値と文字列の 両方を引数として指定可能で、後者は数値に変換することが可能です。 また、整数引数を使用して処理を行うより高速な関数がある場合には、 指定された引数が整数である場合により低速となる関数の代わりに 使用されます。これは透過的に行われるため、結果的に、GMP 数値を 引数とする全ての関数について整数を使用することが可能です。関数 gmp_init() も参照ください。

警告

より大きな整数を明示的に指定するには、文字列として指定してください。 そうしない場合、PHP は値ををまず整数リテラルとして解釈し、 GMP にわたるまでに精度の劣化を生じる可能性が あります。

注意: PHP 5.1.0 以降、この拡張モジュールは Windows でも使用可能です。

要件

» http://www.swox.com/gmp/ から GMP ライブラリをダウンロード可能です。 このサイトでは、GMP のマニュアルも入手可能です。

これらの関数を使用するには、GMP バージョン 2 以降が必要です。 中には、より新しいバージョンの GMP ライブラリを必要とする関数も あります。

インストール手順

これらの関数を利用可能とするには、オプション --with-gmp を使用することにより GMP サポートを有効にして PHP をコンパイルする必要が あります。

実行時設定

設定ディレクティブは定義されていません。

リソース型

ほとんどの GPM 関数は、GMP 数のリソースを使用するか、それを返します。

定義済み定数

以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。

GMP_ROUND_ZERO (integer)
GMP_ROUND_PLUSINF (integer)
GMP_ROUND_MINUSINF (integer)
GMP_VERSION (string)
GMP ライブラリのバージョン。

Example#1 GMP を使用した階乗関数

<?php
function fact($x
{
    
$return 1;
    for (
$i=2$i $x$i++) {
        
$return gmp_mul($return$i);
    }
    return 
$return;
}

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

この例は、1000 の階乗(非常に大きな数です)を非常に高速に計算します。

参考

より数学的な関数が、 BCMath 任意精度関数 および 数学関数 の節にあります。

目次



gmp_abs" width="11" height="7"/> <textdomain
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
GMP
john at worldmapad dot com
23-Jul-2006 03:24
Here's a quick and dirty way to use simple GMP functions with PHP without recompiling. It is dependent upon the use of the exec() function, so make sure you can use exec(). While in safe mode you must consider the safe_mode_exec_dir directive. And don't simply pass user input to the exec function without validating the input first!

Download and Install GMP as instructed in README and INSTALL files.
On my MAC OS X Server, I just did the following:
./configure
make
make check
make install
This installed it in the /usr/local directory. There were some errors, but not with any functions I needed.
Within the gmp-4.#.# cd into the demos directory. Then compile pexpr.c by typing:
make pexpr
This is a simple expressions parser which serves as a simple interface to some of the basic GMP functions.
You can test it then like:
./pexpr "102394874783 * 23498748";
Now you may interface with it using PHP's exec() function.
richard-slater.co.uk
22-Feb-2004 06: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
16-Oct-2003 05: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 rpms 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
=============================================================

gmp_abs" width="11" height="7"/> <textdomain
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites