Skip to content

Commit ed857ce

Browse files
committed
Handle strings without decimal sign like integers
1 parent acdd5dc commit ed857ce

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

src/main/php/math/BigInt.class.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public function __construct($in) {
2929
*/
3030
public function add($other) {
3131
if ($other instanceof self) {
32-
return new self(bcadd($this->num, $other->num));
33-
} else if (is_int($other)) {
34-
return new self(bcadd($this->num, $other));
32+
return new self(bcadd($this->num, $other->num, 0));
33+
} else if (is_int($other) || is_string($other) && false === strpos($other, '.')) {
34+
return new self(bcadd($this->num, $other, 0));
3535
} else if ($other instanceof BigFloat) {
3636
return new BigFloat(bcadd($this->num, $other->num));
3737
} else {
@@ -47,9 +47,9 @@ public function add($other) {
4747
*/
4848
public function subtract($other) {
4949
if ($other instanceof self) {
50-
return new self(bcsub($this->num, $other->num));
51-
} else if (is_int($other)) {
52-
return new self(bcsub($this->num, $other));
50+
return new self(bcsub($this->num, $other->num, 0));
51+
} else if (is_int($other) || is_string($other) && false === strpos($other, '.')) {
52+
return new self(bcsub($this->num, $other, 0));
5353
} else if ($other instanceof BigFloat) {
5454
return new BigFloat(bcsub($this->num, $other->num));
5555
} else {
@@ -65,9 +65,9 @@ public function subtract($other) {
6565
*/
6666
public function multiply($other) {
6767
if ($other instanceof self) {
68-
return new self(bcmul($this->num, $other->num));
69-
} else if (is_int($other)) {
70-
return new self(bcmul($this->num, $other));
68+
return new self(bcmul($this->num, $other->num, 0));
69+
} else if (is_int($other) || is_string($other) && false === strpos($other, '.')) {
70+
return new self(bcmul($this->num, $other, 0));
7171
} else if ($other instanceof BigFloat) {
7272
return new BigFloat(bcmul($this->num, $other->num));
7373
} else {
@@ -90,7 +90,7 @@ public function divide($other) {
9090
throw new IllegalArgumentException($e);
9191
}
9292
return new self($r);
93-
} else if (is_int($other)) {
93+
} else if (is_int($other) || is_string($other) && false === strpos($other, '.')) {
9494
if (null === ($r= bcdiv($this->num, $other, 0))) { // inlined
9595
$e= key(\xp::$errors[__FILE__][__LINE__- 1]);
9696
\xp::gc(__FILE__);

0 commit comments

Comments
 (0)