Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/main/php/math/BigFloat.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/php/math/BigInt.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/php/math/unittest/BigFloatTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/test/php/math/unittest/BigIntTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading