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.
(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
gmp_init — Создание GMP числа
Создаёт GMP число из целого числа или строки.
num
Целое число или строка. Число, заданное строкой может быть десятичным, шестнадцатеричным или восьмеричным.
base
Основание системы счисления.
Основание может варьироваться от 2 до 62. Если основание 0
(по умолчанию), основание будет определено, исходя из первых символов:
если первые два символа 0x
или 0X
,
число будет интерпретировано как шестнадцатеричное, если первые два символа - 0b
or 0B
,
предполагается двоичный, если первый символ
"0", то как восьмеричное, в остальных случаях числа будут восприниматься
как десятичные.
Для оснований до 36 регистр игнорируется; буквы верхнего и нижнего регистра имеют одинаковое значение.
Для оснований от 37 до 62 заглавные буквы представляют обычные от 10 до 35, а строчные буквы - от 36 до 61.
Объект класса GMPю
Пример #1 Создание GMP числа
<?php
$a = gmp_init(123456);
$b = gmp_init("0xFFFFDEBACDFEDF7200");
?>
Замечание:
Нет необходимости явно вызывать эту функцию, если требуется вставить целое число или строку на место GMP числа в GMP функциях, как например gmp_add(). Аргументы функций автоматически преобразуются в GMP числа, если такое преобразование требуется и оно возможно, по тем же правилам, что и в gmp_init().
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.
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.
}
?>
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.
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
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
?>
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! :-)