Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure non-negative values for bcmath #68

Merged
merged 1 commit into from
Mar 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions packages/zend-locale/library/Zend/Locale/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,12 @@ public static function localize($value)
* Fixes a problem of BCMath with numbers containing exponents
*
* @param integer $value Value to erase the exponent
* @param integer $scale (Optional) Scale to use
* @param int|null $scale (Optional) Scale to use
* @return string
*/
public static function exponent($value, $scale = null)
{
$scale = max(0, $scale);
if (!extension_loaded('bcmath')) {
return $value;
}
Expand All @@ -242,11 +243,18 @@ public static function exponent($value, $scale = null)
*
* @param string $op1
* @param string $op2
* @param integer $scale
* @param int|null $scale
* @return string
*/
public static function Add($op1, $op2, $scale = null)
{
/**
* Before PHP8 bcmath functions could be called with a negative
* scale factor which was handled as if $scale were 0
*
* With PHP8 this would emit a ValueError
*/
$scale = max(0, $scale);
$op1 = self::exponent($op1, $scale);
$op2 = self::exponent($op2, $scale);

Expand All @@ -258,11 +266,12 @@ public static function Add($op1, $op2, $scale = null)
*
* @param string $op1
* @param string $op2
* @param integer $scale
* @param int|null $scale
* @return string
*/
public static function Sub($op1, $op2, $scale = null)
{
$scale = max(0, $scale);
$op1 = self::exponent($op1, $scale);
$op2 = self::exponent($op2, $scale);
return bcsub($op1, $op2, $scale);
Expand All @@ -273,11 +282,12 @@ public static function Sub($op1, $op2, $scale = null)
*
* @param string $op1
* @param string $op2
* @param integer $scale
* @param int|null $scale
* @return string
*/
public static function Pow($op1, $op2, $scale = null)
{
$scale = max(0, $scale);
$op1 = self::exponent($op1, $scale);
$op2 = self::exponent($op2, $scale);
return bcpow($op1, $op2, $scale);
Expand All @@ -288,11 +298,12 @@ public static function Pow($op1, $op2, $scale = null)
*
* @param string $op1
* @param string $op2
* @param integer $scale
* @param int|null $scale
* @return string
*/
public static function Mul($op1, $op2, $scale = null)
{
$scale = max(0, $scale);
$op1 = self::exponent($op1, $scale);
$op2 = self::exponent($op2, $scale);
return bcmul($op1, $op2, $scale);
Expand All @@ -303,11 +314,12 @@ public static function Mul($op1, $op2, $scale = null)
*
* @param string $op1
* @param string $op2
* @param integer $scale
* @param int|null $scale
* @return string
*/
public static function Div($op1, $op2, $scale = null)
{
$scale = max(0, $scale);
$op1 = self::exponent($op1, $scale);
$op2 = self::exponent($op2, $scale);
return bcdiv($op1, $op2, $scale);
Expand All @@ -317,11 +329,12 @@ public static function Div($op1, $op2, $scale = null)
* BCSqrt - fixes a problem of BCMath and exponential numbers
*
* @param string $op1
* @param integer $scale
* @param int|null $scale
* @return string
*/
public static function Sqrt($op1, $scale = null)
{
$scale = max(0, $scale);
$op1 = self::exponent($op1, $scale);
return bcsqrt($op1, $scale);
}
Expand All @@ -345,11 +358,12 @@ public static function Mod($op1, $op2)
*
* @param string $op1
* @param string $op2
* @param integer $scale
* @param int|null $scale
* @return string
*/
public static function Comp($op1, $op2, $scale = null)
{
$scale = max(0, $scale);
$op1 = self::exponent($op1, $scale);
$op2 = self::exponent($op2, $scale);
return bccomp($op1, $op2, $scale);
Expand Down