Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Remove scale changing side effect from Bcmath
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsusu authored and weierophinney committed Apr 26, 2018
1 parent ea0f8c8 commit 01853aa
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/BigInteger/Adapter/Bcmath.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@
*/
class Bcmath implements AdapterInterface
{
/**
* Constructor
* Sets Bcmath scale factor to zero
*/
public function __construct()
{
bcscale(0);
}


/**
* Create string representing big integer in decimal form from arbitrary integer format
*
Expand Down Expand Up @@ -80,7 +72,7 @@ public function init($operand, $base = null)
*/
public function add($leftOperand, $rightOperand)
{
return bcadd($leftOperand, $rightOperand);
return bcadd($leftOperand, $rightOperand, 0);
}

/**
Expand All @@ -92,7 +84,7 @@ public function add($leftOperand, $rightOperand)
*/
public function sub($leftOperand, $rightOperand)
{
return bcsub($leftOperand, $rightOperand);
return bcsub($leftOperand, $rightOperand, 0);
}

/**
Expand All @@ -104,7 +96,7 @@ public function sub($leftOperand, $rightOperand)
*/
public function mul($leftOperand, $rightOperand)
{
return bcmul($leftOperand, $rightOperand);
return bcmul($leftOperand, $rightOperand, 0);
}

/**
Expand All @@ -124,7 +116,7 @@ public function div($leftOperand, $rightOperand)
);
}

$result = bcdiv($leftOperand, $rightOperand);
$result = bcdiv($leftOperand, $rightOperand, 0);

return $result;
}
Expand All @@ -138,7 +130,7 @@ public function div($leftOperand, $rightOperand)
*/
public function pow($operand, $exp)
{
return bcpow($operand, $exp);
return bcpow($operand, $exp, 0);
}

/**
Expand All @@ -149,7 +141,7 @@ public function pow($operand, $exp)
*/
public function sqrt($operand)
{
return bcsqrt($operand);
return bcsqrt($operand, 0);
}

/**
Expand Down Expand Up @@ -185,7 +177,7 @@ public function mod($leftOperand, $rightOperand)
*/
public function powmod($leftOperand, $rightOperand, $modulus)
{
return bcpowmod($leftOperand, $rightOperand, $modulus);
return bcpowmod($leftOperand, $rightOperand, $modulus, 0);
}

/**
Expand All @@ -199,7 +191,7 @@ public function powmod($leftOperand, $rightOperand, $modulus)
*/
public function comp($leftOperand, $rightOperand)
{
return bccomp($leftOperand, $rightOperand);
return bccomp($leftOperand, $rightOperand, 0);
}

/**
Expand Down Expand Up @@ -227,7 +219,7 @@ public function intToBin($operand, $twoc = false)
while (bccomp($operand, '0', 0) > 0) {
$temp = bcmod($operand, '16777216');
$bytes = chr($temp >> 16) . chr($temp >> 8) . chr($temp) . $bytes;
$operand = bcdiv($operand, '16777216');
$operand = bcdiv($operand, '16777216', 0);
}
$bytes = ltrim($bytes, $nb);

Expand Down Expand Up @@ -318,7 +310,7 @@ public function baseConvert($operand, $fromBase, $toBase = 10)
? base_convert($operand[$i], $fromBase, 10)
: strpos($chars, $operand[$i]);

$decimal = bcadd($decimal, $remainder);
$decimal = bcadd($decimal, $remainder, 0);
}
}

Expand Down

0 comments on commit 01853aa

Please sign in to comment.