diff --git a/src/BigDecimal.php b/src/BigDecimal.php index fa60718..ebd4515 100644 --- a/src/BigDecimal.php +++ b/src/BigDecimal.php @@ -148,6 +148,10 @@ public function plus($that) : BigDecimal return $this; } + if ($this->value === '0' && $this->scale <= $that->scale) { + return $that; + } + $this->scaleValues($this, $that, $a, $b); $value = Calculator::get()->add($a, $b); @@ -202,6 +206,10 @@ public function multipliedBy($that) : BigDecimal return $this; } + if ($this->value === '1' && $this->scale === 0) { + return $that; + } + $value = Calculator::get()->mul($this->value, $that->value); $scale = $this->scale + $that->scale; diff --git a/src/BigInteger.php b/src/BigInteger.php index fe7825f..93c1cd4 100644 --- a/src/BigInteger.php +++ b/src/BigInteger.php @@ -225,6 +225,10 @@ public function plus($that) : BigInteger return $this; } + if ($this->value === '0') { + return $that; + } + $value = Calculator::get()->add($this->value, $that->value); return new BigInteger($value); @@ -269,6 +273,10 @@ public function multipliedBy($that) : BigInteger return $this; } + if ($this->value === '1') { + return $that; + } + $value = Calculator::get()->mul($this->value, $that->value); return new BigInteger($value); diff --git a/tests/BigDecimalTest.php b/tests/BigDecimalTest.php index 9f5a437..17709f9 100644 --- a/tests/BigDecimalTest.php +++ b/tests/BigDecimalTest.php @@ -501,6 +501,16 @@ public function providerPlus() ['123.00', '999.0', '112200', 2], ['123.00', '999.00', '112200', 2], + ['0', '999', '999', 0], + ['0', '999.0', '9990', 1], + ['0', '999.00', '99900', 2], + ['0.0', '999', '9990', 1], + ['0.0', '999.0', '9990', 1], + ['0.0', '999.00', '99900', 2], + ['0.00', '999', '99900', 2], + ['0.00', '999.0', '99900', 2], + ['0.00', '999.00', '99900', 2], + ['123', '-999', '-876', 0], ['123', '-999.0', '-8760', 1], ['123', '-999.00', '-87600', 2], @@ -569,6 +579,8 @@ public function providerMinus() ['123.00', '999', '-87600', 2], ['123.00', '999.0', '-87600', 2], ['123.00', '999.00', '-87600', 2], + ['0', '999', '-999', 0], + ['0', '999.0', '-9990', 1], ['123', '-999', '1122', 0], ['123', '-999.0', '11220', 1], @@ -606,6 +618,7 @@ public function providerMinus() ['-2348783784774335.3232342791', '-309049304233536.556172', '-20397344805407987670622791', 10], ['1234568798347983.2334899238921', '0', '12345687983479832334899238921', 13], + ['0', '1234568798347983.2334899238921', '-12345687983479832334899238921', 13], ['-0.00223287647368738736428467863784', '0.000', '-223287647368738736428467863784', 32], ]; } @@ -674,6 +687,46 @@ public function providerMultipliedBy() ['-123.00', '-999.0', '122877000', 3], ['-123.00', '-999.00', '1228770000', 4], + ['1', '999', '999', 0], + ['1', '999.0', '9990', 1], + ['1', '999.00', '99900', 2], + ['1.0', '999', '9990', 1], + ['1.0', '999.0', '99900', 2], + ['1.0', '999.00', '999000', 3], + ['1.00', '999', '99900', 2], + ['1.00', '999.0', '999000', 3], + ['1.00', '999.00', '9990000', 4], + + ['123', '1', '123', 0], + ['123', '1.0', '1230', 1], + ['123', '1.00', '12300', 2], + ['123.0', '1', '1230', 1], + ['123.0', '1.0', '12300', 2], + ['123.0', '1.00', '123000', 3], + ['123.00', '1', '12300', 2], + ['123.00', '1.0', '123000', 3], + ['123.00', '1.00', '1230000', 4], + + ['0', '999', '0', 0], + ['0', '999.0', '0', 1], + ['0', '999.00', '0', 2], + ['0.0', '999', '0', 1], + ['0.0', '999.0', '0', 2], + ['0.0', '999.00', '0', 3], + ['0.00', '999', '0', 2], + ['0.00', '999.0', '0', 3], + ['0.00', '999.00', '0', 4], + + ['123', '0', '0', 0], + ['123', '0.0', '0', 1], + ['123', '0.00', '0', 2], + ['123.0', '0', '0', 1], + ['123.0', '0.0', '0', 2], + ['123.0', '0.00', '0', 3], + ['123.00', '0', '0', 2], + ['123.00', '0.0', '0', 3], + ['123.00', '0.00', '0', 4], + ['589252.156111130', '999.2563989942545241223454', '5888139876152080735720775399923986443020', 31], ['-589252.15611130', '999.256398994254524122354', '-58881398761537794715991163083004200020', 29], ['589252.1561113', '-99.256398994254524122354', '-584870471152079471599116308300420002', 28], diff --git a/tests/BigIntegerTest.php b/tests/BigIntegerTest.php index 4922f8e..80c3f96 100644 --- a/tests/BigIntegerTest.php +++ b/tests/BigIntegerTest.php @@ -526,6 +526,8 @@ public function providerPlus() return [ ['5165450198704521651351654564564089798441', '0', '5165450198704521651351654564564089798441'], ['-5165450198704521651351654564564089798441', '0', '-5165450198704521651351654564564089798441'], + ['0', '5165450198704521651351654564564089798441', '5165450198704521651351654564564089798441'], + ['0', '-5165450198704521651351654564564089798441', '-5165450198704521651351654564564089798441'], ['5165450198704521651351654564564089798441', '-5165450198704521651351654564564089798441', '0'], ['-5165450198704521651351654564564089798441', '5165450198704521651351654564564089798441', '0'], @@ -586,10 +588,14 @@ public function providerMultipliedBy() return [ ['123456789098765432101234567890987654321', '1', '123456789098765432101234567890987654321'], ['123456789098765432101234567890987654321', '-1', '-123456789098765432101234567890987654321'], + ['1', '123456789098765432101234567890987654321', '123456789098765432101234567890987654321'], ['15892588375910581333', '2485910409339228962451', '39507550875019745254366764864945838527183'], ['341581435989834012309', '-91050393818389238433', '-31101124267925302088072082300643257871797'], ['-1204902920503999920003', '1984389583950290232332', '-2390996805119422027350037939263960284136996'], ['-991230349304902390122', '-3483910549230593053437', '3453357870660875087266990729629471366949314'], + ['0', '-3483910549230593053437', '0'], + ['-991230349304902390122', '0', '0'], + ['1274837942798479387498237897498734984', 30, '38245138283954381624947136924962049520'], ['1274837942798479387498237897498734984', 30.0, '38245138283954381624947136924962049520'], @@ -639,6 +645,7 @@ public function providerDividedBy() ['123456789098765432101234567890987654322', '14/7', '61728394549382716050617283945493827161'], ['61728394549382716050617283945493827161', '0.5', RoundingNecessaryException::class], ['61728394549382716050617283945493827161', '1/2', RoundingNecessaryException::class], + ['0', '61728394549382716050617283945493827161', '0'], // overflowing native integer division ['-2147483648', '-1', '2147483648'], // 32-bit diff --git a/tests/Internal/Calculator/NativeCalculatorTest.php b/tests/Internal/Calculator/NativeCalculatorTest.php index cd66946..380a7e3 100644 --- a/tests/Internal/Calculator/NativeCalculatorTest.php +++ b/tests/Internal/Calculator/NativeCalculatorTest.php @@ -62,6 +62,9 @@ public function providerMul() return [ ['0', '0', '0'], + ['0', '1234567891234567889999999', '0'], + ['1234567891234567889999999', '0', '0'], + ['1', '1234567891234567889999999', '1234567891234567889999999'], ['1234567891234567889999999', '1', '1234567891234567889999999'],