Skip to content

Commit

Permalink
Handle strings without decimal sign like integers
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 25, 2022
1 parent acdd5dc commit ed857ce
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/main/php/math/BigInt.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function __construct($in) {
*/
public function add($other) {
if ($other instanceof self) {
return new self(bcadd($this->num, $other->num));
} else if (is_int($other)) {
return new self(bcadd($this->num, $other));
return new self(bcadd($this->num, $other->num, 0));
} else if (is_int($other) || is_string($other) && false === strpos($other, '.')) {
return new self(bcadd($this->num, $other, 0));
} else if ($other instanceof BigFloat) {
return new BigFloat(bcadd($this->num, $other->num));
} else {
Expand All @@ -47,9 +47,9 @@ public function add($other) {
*/
public function subtract($other) {
if ($other instanceof self) {
return new self(bcsub($this->num, $other->num));
} else if (is_int($other)) {
return new self(bcsub($this->num, $other));
return new self(bcsub($this->num, $other->num, 0));
} else if (is_int($other) || is_string($other) && false === strpos($other, '.')) {
return new self(bcsub($this->num, $other, 0));
} else if ($other instanceof BigFloat) {
return new BigFloat(bcsub($this->num, $other->num));
} else {
Expand All @@ -65,9 +65,9 @@ public function subtract($other) {
*/
public function multiply($other) {
if ($other instanceof self) {
return new self(bcmul($this->num, $other->num));
} else if (is_int($other)) {
return new self(bcmul($this->num, $other));
return new self(bcmul($this->num, $other->num, 0));
} else if (is_int($other) || is_string($other) && false === strpos($other, '.')) {
return new self(bcmul($this->num, $other, 0));
} else if ($other instanceof BigFloat) {
return new BigFloat(bcmul($this->num, $other->num));
} else {
Expand All @@ -90,7 +90,7 @@ public function divide($other) {
throw new IllegalArgumentException($e);
}
return new self($r);
} else if (is_int($other)) {
} else if (is_int($other) || is_string($other) && false === strpos($other, '.')) {
if (null === ($r= bcdiv($this->num, $other, 0))) { // inlined
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
\xp::gc(__FILE__);
Expand Down

0 comments on commit ed857ce

Please sign in to comment.