diff --git a/ChangeLog.md b/ChangeLog.md index c82792b..d775622 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,12 @@ XP Math changelog ## ?.?.? / ????-??-?? +## 10.0.0 / ????-??-?? + +* Merged PR #6: Make `Big(Int|Float)::power()` consistent with IEEE 754 + rules, see issue #5. + (@thekid) + ## 9.3.0 / 2024-03-24 * Made compatible with XP 12 - @thekid diff --git a/src/main/php/math/BigFloat.class.php b/src/main/php/math/BigFloat.class.php index 14cdff5..266326a 100755 --- a/src/main/php/math/BigFloat.class.php +++ b/src/main/php/math/BigFloat.class.php @@ -78,6 +78,10 @@ public function divide($other) { * @return math.BigNum */ public function power($other) { + if (0 === bccomp($this->num, 0) && -1 === bccomp($other instanceof self ? $other->num : $other, 0)) { + throw new IllegalArgumentException('Negative power of zero'); + } + return new self(bcpow($this->num, $other instanceof self ? $other->num : $other)); } diff --git a/src/main/php/math/BigInt.class.php b/src/main/php/math/BigInt.class.php index c7bddf8..fd1d4fa 100755 --- a/src/main/php/math/BigInt.class.php +++ b/src/main/php/math/BigInt.class.php @@ -169,11 +169,15 @@ public function divide0($other) { /** * ^ * - * @see http://en.wikipedia.org/wiki/Exponentiation + * @see http://en.wikipedia.org/wiki/Exponentiation * @param math.BigNum|int|float|string $other * @return math.BigNum */ public function power($other) { + if (0 === bccomp($this->num, 0) && -1 === bccomp($other instanceof self ? $other->num : $other, 0)) { + throw new IllegalArgumentException('Negative power of zero'); + } + if ($other instanceof self) { return new self(bcpow($this->num, $other->num)); } else if (is_int($other)) { diff --git a/src/test/php/math/unittest/BigFloatTest.class.php b/src/test/php/math/unittest/BigFloatTest.class.php index 06c5866..d00d97f 100755 --- a/src/test/php/math/unittest/BigFloatTest.class.php +++ b/src/test/php/math/unittest/BigFloatTest.class.php @@ -184,9 +184,9 @@ public function powerOfZeroZero() { Assert::equals(new BigFloat(1.0), (new BigFloat(0.0))->power(new BigFloat(0.0))); } - #[Test] + #[Test, Expect(IllegalArgumentException::class)] public function powerOfZeroNegative() { - Assert::equals(new BigFloat(0.0), (new BigFloat(0.0))->power(new BigFloat(-2))); + (new BigFloat(0.0))->power(new BigFloat(-2)); } #[Test] diff --git a/src/test/php/math/unittest/BigIntTest.class.php b/src/test/php/math/unittest/BigIntTest.class.php index 14483f9..4f914d2 100755 --- a/src/test/php/math/unittest/BigIntTest.class.php +++ b/src/test/php/math/unittest/BigIntTest.class.php @@ -239,9 +239,9 @@ public function powerOfZeroZero() { Assert::equals(new BigInt(1), (new BigInt(0))->power(new BigInt(0))); } - #[Test] + #[Test, Expect(IllegalArgumentException::class)] public function powerOfZeroNegative() { - Assert::equals(new BigInt(0), (new BigInt(0))->power(new BigInt(-2))); + (new BigInt(0))->power(new BigInt(-2)); } #[Test]