本文整理汇总了PHP中gmp_abs函数的典型用法代码示例。如果您正苦于以下问题:PHP gmp_abs函数的具体用法?PHP gmp_abs怎么用?PHP gmp_abs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了gmp_abs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: packLong
/**
* Pack a long.
*
* If it is a 32bit PHP we suppose that this log is treated by bcmath
* TODO 32bit
*
* @param int|string $value
*
* @return string the packed long
*/
public static function packLong($value)
{
if (PHP_INT_SIZE > 4) {
$value = (int) $value;
$binaryString = chr($value >> 56 & 0xff) . chr($value >> 48 & 0xff) . chr($value >> 40 & 0xff) . chr($value >> 32 & 0xff) . chr($value >> 24 & 0xff) . chr($value >> 16 & 0xff) . chr($value >> 8 & 0xff) . chr($value & 0xff);
} else {
/*
* To get the two's complement of a binary number,
* the bits are inverted, or "flipped",
* by using the bitwise NOT operation;
* the value of 1 is then added to the resulting value
*/
$bitString = '';
$isNegative = $value[0] == '-';
if (function_exists("bcmod")) {
//add 1 for the two's complement
if ($isNegative) {
$value = bcadd($value, '1');
}
while ($value !== '0') {
$bitString = (string) abs((int) bcmod($value, '2')) . $bitString;
$value = bcdiv($value, '2');
}
} elseif (function_exists("gmp_mod")) {
//add 1 for the two's complement
if ($isNegative) {
$value = gmp_strval(gmp_add($value, '1'));
}
while ($value !== '0') {
$bitString = gmp_strval(gmp_abs(gmp_mod($value, '2'))) . $bitString;
$value = gmp_strval(gmp_div_q($value, '2'));
}
} else {
while ($value != 0) {
list($value, $remainder) = self::str2bin((string) $value);
$bitString = $remainder . $bitString;
}
}
//Now do the logical not for the two's complement last phase
if ($isNegative) {
$len = strlen($bitString);
for ($x = 0; $x < $len; $x++) {
$bitString[$x] = $bitString[$x] == '1' ? '0' : '1';
}
}
//pad to have 64 bit
if ($bitString != '' && $isNegative) {
$bitString = str_pad($bitString, 64, '1', STR_PAD_LEFT);
} else {
$bitString = str_pad($bitString, 64, '0', STR_PAD_LEFT);
}
$hi = substr($bitString, 0, 32);
$lo = substr($bitString, 32, 32);
$hiBin = pack('H*', str_pad(base_convert($hi, 2, 16), 8, 0, STR_PAD_LEFT));
$loBin = pack('H*', str_pad(base_convert($lo, 2, 16), 8, 0, STR_PAD_LEFT));
$binaryString = $hiBin . $loBin;
}
return $binaryString;
}
示例2: deposited_or_withdrawn
function deposited_or_withdrawn($deposit, $withdraw)
{
$net = gmp_sub($deposit, $withdraw);
$abs = gmp_abs($net);
if (gmp_cmp($net, 0) < 0) {
$word = _("withdrawn");
} else {
$word = _("deposited");
}
return array($abs, $word);
}
示例3: calculateContentLength
protected function calculateContentLength()
{
$nrOfOctets = 1;
// we need at least one octet
$tmpValue = gmp_abs(gmp_init($this->value, 10));
while (gmp_cmp($tmpValue, 127) > 0) {
$tmpValue = $this->rightShift($tmpValue, 8);
$nrOfOctets++;
}
return $nrOfOctets;
}
示例4: _encodeNegativeInteger
/**
* Encode negative integer to DER content.
*
* @param \GMP|resource $num
* @return string
*/
private static function _encodeNegativeInteger($num)
{
$num = gmp_abs($num);
// compute number of bytes required
$width = 1;
if ($num > 128) {
$tmp = $num;
do {
$width++;
$tmp >>= 8;
} while ($tmp > 128);
}
// compute two's complement 2^n - x
$num = gmp_pow("2", 8 * $width) - $num;
$bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
// if first bit is 0, prepend full inverted byte
// to represent negative two's complement
if (!(ord($bin[0]) & 0x80)) {
$bin = chr(0xff) . $bin;
}
return $bin;
}
示例5: abs
/**
* Get the absolute value
*
* @access public
* @return self
*/
public function abs()
{
$result = gmp_abs($this->getRawValue());
return static::factory($result);
}
示例6: toBytes
/**
* Converts a BigInteger to a binary string.
*
* @return string
*/
public function toBytes()
{
if (gmp_cmp($this->value, gmp_init(0)) === 0) {
return '';
}
$temp = gmp_strval(gmp_abs($this->value), 16);
$temp = mb_strlen($temp, '8bit') & 1 ? '0' . $temp : $temp;
$temp = hex2bin($temp);
return ltrim($temp, chr(0));
}
示例7: div
/**
* Creates a new decimal which is a result of a division of $a by $b.
*
* @param Decimal2 $a
* @param Decimal2 $b
* @return Decimal2
*/
public static function div(Decimal2 $a, Decimal2 $b)
{
$strA = strval($a);
$strB = strval($b);
$sign_a = '-' === $strA[0] ? -1 : 1;
$sign_b = '-' === $strB[0] ? -1 : 1;
$ret = new Decimal2('0');
$ret->cents = gmp_strval(gmp_div_q(gmp_add(gmp_mul(gmp_abs(gmp_init($a->cents, 10)), 100), 50), gmp_abs(gmp_init($b->cents, 10)), GMP_ROUND_ZERO));
if ($sign_a * $sign_b < 0) {
$ret->cents = gmp_strval(gmp_neg(gmp_init($ret->cents, 10)));
}
return $ret;
}
示例8: abs
/**
* This method returns the absolute value of this object's value.
*
* @access public
* @static
* @param IInteger\Type $x the operand
* @return IInteger\Type the result
*/
public static function abs(IInteger\Type $x) : IInteger\Type
{
return IInteger\Type::box(gmp_strval(gmp_abs($x->unbox())));
}
示例9: abs
/**
* Absolute value.
*
* @access public
* @return \Topxia\Service\Util\Phpsec\Math\BigInteger
*/
public function abs()
{
$temp = new static();
switch (MATH_BIGINTEGER_MODE) {
case self::MODE_GMP:
$temp->value = gmp_abs($this->value);
break;
case self::MODE_BCMATH:
$temp->value = bccomp($this->value, '0', 0) < 0 ? substr($this->value, 1) : $this->value;
break;
default:
$temp->value = $this->value;
}
return $temp;
}
示例10: switch
/**
* Returns the absolute value of a Math_Integer number
*
* @param object Math_Integer $int1
* @return object Math_Integer on success, PEAR_Error otherwise
* @access public
*/
function &abs(&$int1)
{
/*{{{*/
if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) {
return $err;
}
switch (MATH_INTLIB) {
/*{{{*/
case 'gmp':
$tmp = gmp_strval(gmp_abs($int1->getValue()));
break;
case 'bcmath':
if ($int1->getValue() < 0) {
$tmp = bcmul(-1, $int1->getValue());
} else {
$tmp = $int1->getValue();
}
break;
case 'std':
$tmp = abs($int1->getValue());
break;
}
/*}}}*/
return new Math_Integer($tmp);
}
示例12: var_dump
<?php
var_dump(gmp_strval(gmp_abs("")));
var_dump(gmp_strval(gmp_abs("0")));
var_dump(gmp_strval(gmp_abs(0)));
var_dump(gmp_strval(gmp_abs(-1.1111111111111111E+20)));
var_dump(gmp_strval(gmp_abs("111111111111111111111")));
var_dump(gmp_strval(gmp_abs("-111111111111111111111")));
var_dump(gmp_strval(gmp_abs("0000")));
var_dump(gmp_strval(gmp_abs("09876543")));
var_dump(gmp_strval(gmp_abs("-099987654")));
var_dump(gmp_abs());
var_dump(gmp_abs(1, 2));
var_dump(gmp_abs(array()));
echo "Done\n";
示例13: abs
/**
* Get absolute value of a big integer
*
* @param string $operand
* @return string
*/
public function abs($operand)
{
$result = gmp_abs($operand);
return gmp_strval($result);
}
示例14: abs
/**
* Absolute value.
*
* @return Math_BigInteger
* @access public
*/
public function abs()
{
$temp = new Math_BigInteger();
switch (self::$mode) {
case Math_BigInteger::MODE_GMP:
$temp->value = gmp_abs($this->value);
break;
case Math_BigInteger::MODE_BCMATH:
$temp->value = bccomp($this->value, '0', 0) < 0 ? substr($this->value, 1) : $this->value;
break;
default:
$temp->value = $this->value;
}
return $temp;
}
示例15: abs
/**
* Converts the value to an absolute number.
*
* @return BigInteger
*/
public function abs() : BigInteger
{
$value = gmp_abs($this->value);
return $this->assignValue($value);
}