Skip to content

Commit

Permalink
Fix parse() negative number bug, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jul 21, 2018
1 parent a66c271 commit 096df6e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ public static function parse(string $number, int $base = 10) : BigInteger
return new BigInteger($sign . $number);
}

$result = Calculator::get()->fromBase($sign . $number, $base);
$result = Calculator::get()->fromBase($number, $base);

return new BigInteger($result);
return new BigInteger($sign . $result);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Internal/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ public function gcd(string $a, string $b) : string
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for base conversion.
*
* @param string $number The number, non-empty, case-insensitively validated for the given base.
* The number may contain an optional leading + or - sign, and optional leading zeros.
* @param string $number The number, positive or zero, non-empty, case-insensitively validated for the given base.
* @param int $base The base of the number, validated from 2 to 36.
*
* @return string The converted number, following the Calculator conventions.
Expand Down
7 changes: 7 additions & 0 deletions tests/BigIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ public function providerParse()
['+0456', 10, '456'],
['-0789', 10, '-789'],

['123', 11, '146'],
['+456', 11, '545'],
['-789', 11, '-944'],
['0123', 11, '146'],
['+0456', 11, '545'],
['-0789', 11, '-944'],

['110011001100110011001111', 36, '640998479760579495168036691627608949'],
['110011001100110011001111', 35, '335582856048758779730579523833856636'],
['110011001100110011001111', 34, '172426711023004493064981145981549295'],
Expand Down

0 comments on commit 096df6e

Please sign in to comment.