gmp_init

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

gmp_initGMP 数を作成する

説明

gmp_init(int|string $num, int $base = 0): GMP

整数または文字列から GMP 数を生成します。

パラメータ

num
整数値あるいは文字列。文字列表現には、 10進数か16進数か8進数、あるいは2進数を使用可能です。
base
文字列表現を変換するのに使う基数 明示的に基数を指定する場合、 2 から 62 までの値を指定できます。 36 までの場合、大文字小文字は無視されます。 つまり、大文字だろうと小文字だろうと、同じ値だということです。 37 から 62 までの場合、 大文字の値は 10 から 35 までの値を示し、小文字の値は 36 から 61 までの値を示します。 base0 にした場合、 実際の基数の解釈は num の先頭の文字によって決まります。 最初の2文字が 0x0X の場合、 文字列は16進数として解釈されます。 最初の2文字が 0b0B の場合、 文字列は2進数として解釈されます。 最初の2文字が 0o0o の場合、 文字列は8進数として解釈されます。 さらに、最初の文字が 0 の場合も、 文字列は8進数として解釈されます。 上記以外の場合は全て、文字列は10進数として解釈されます。

戻り値

GMP オブジェクトを返します。

変更履歴

バージョン 説明
8.1.0 num 文字列に、8進数を表すプレフィックス 0o0o を明示的に指定する機能をサポートしました。 base0 にした場合に、 こうしたプレフィックスをを解釈する機能も追加されています。

例1 GMP 数の作成

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

注意

注意:

gmp_add() のような GMP 関数において、GMP 数を指定するところに整数または文字列を使用したい場合には、 この関数をコールする必要はありません。この場合、変換が必要な場合には、 gmp_init() と同様の方法で関数の引数は自動的に GMP 数に変換されます。

参考

add a note

User Contributed Notes 7 notes

up
4
php at richardneill dot org
19 years ago
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.}?>
up
2
karl dot debisschop at pearson dot com
14 years ago
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
up
2
marcus at synchromedia dot co dot uk
14 years ago
I discovered that the gmp functions use [0-9a-f] up to base 16, but [0-9A-Za-z] (i.e. upper case first) from bases 17 to 62. This differs from most of the base-62 implementations I've found that tend to use lower case first.
up
1
php at richardneill dot org
19 years ago
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")); //8This behaviour is inconsistent: either $d should equal $b, or$b should equal $a.
up
1
Aurelien Marchand
3 years ago
Missing from the documentation: gmp_init() return false in case it failed to parse $num<?phpvar_dump(gmp_init(""));// returns "bool(false)" and not a GMP object as hinted by the documentation, it also triggers a Warning "PHP Warning:  gmp_init(): Unable to convert variable to GMP - string is not an integer in php shell code on line 1"?>
up
-1
charlie at oblivion dot cz
18 years ago
gmp_* functions don't accept strings with a leading '+':<?phpecho gmp_strval(gmp_init('+42'));      #0echo gmp_strval(gmp_add('42', '+42')); #42echo bcadd('+42', '+42');              #84?>
up
-3
thomas dot hebinck at digionline dot de
20 years ago
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! :-)
To Top