diff --git a/README.md b/README.md index 2f4e6cd..62c0fe3 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ composer require "fab2s/math" ## In practice -As `Math` is meant to be used where precision matters, it is pretty strict with input numbers : it will throw an exception whenever an input number does not match `^([+-]{1})?([0-9]+(\.[0-9]+)?|\.[0-9]+)$` after passing though `trim()`. +As `Math` is meant to be used where precision matters, it is pretty strict with input numbers : it will throw an exception whenever an input number does not match `^[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+)$` after passing though `trim()`. In practice this means that "-.0051" and "00028.34" are ok, but "1E12", "3,14" or "1.1.1" will throw an exception. This is done so because in `bcmath` world, "1E12", "1.1.1" and "abc" are all "0", which could result in some disaster if you where to do nothing. @@ -116,7 +116,7 @@ The way floats are handled in general and by PHP in particular is the very the r Precision handling does not rely on [bcscale](https://php.net/bcscale) as it is not so reliable IRL. As it is a global setup, it may affect or be affected by far away/unrelated code (with fpm it can actually spread to all PHP processes). -`Math` handle precisions at both instance and global (limited to the current PHP process) precision. The global precision is stored in a static variable. When set, each new instance will start with this global precision as its own precision (you can still set the instance precision after instantiation). When no global precision is set, initial instance precision defaults to `Math::PRECISION` (currently 9, or 9 digit after the dot) +`Math` handle precisions at both instance and global (limited to the current PHP process) precision. The global precision is stored in a static variable. When set, each new instance will start with this global precision as its own precision (you can still set the instance precision after instantiation). When no global precision is set, initial instance precision defaults to `Math::PRECISION` (currently 9, or 9 digits after the dot) ```php // set global precision diff --git a/src/Math.php b/src/Math.php index 67f9b8b..877b5e8 100644 --- a/src/Math.php +++ b/src/Math.php @@ -82,7 +82,7 @@ public static function fromBase($number, int $base): self } /** - * @param string|static $number + * @param string|int|static $number * * @throws \InvalidArgumentException * @@ -94,7 +94,7 @@ public function gte($number): bool } /** - * @param string|static $number + * @param string|int|static $number * * @throws \InvalidArgumentException * @@ -106,7 +106,7 @@ public function gt($number): bool } /** - * @param string|static $number + * @param string|int|static $number * * @throws \InvalidArgumentException * @@ -118,7 +118,7 @@ public function lte($number): bool } /** - * @param string|static $number + * @param string|int|static $number * * @throws \InvalidArgumentException * @@ -130,7 +130,7 @@ public function lt($number): bool } /** - * @param string|static $number + * @param string|int|static $number * * @throws \InvalidArgumentException * @@ -182,7 +182,7 @@ public function toBase($base): string * * @return string */ - public function format(int $decimals = 0, string $decPoint = '.', string $thousandsSep = ' '): string + public function format($decimals = 0, string $decPoint = '.', string $thousandsSep = ' '): string { $decimals = max(0, (int) $decimals); $dec = ''; diff --git a/src/MathBaseAbstract.php b/src/MathBaseAbstract.php index e029f96..ca2db3d 100644 --- a/src/MathBaseAbstract.php +++ b/src/MathBaseAbstract.php @@ -163,7 +163,7 @@ public static function gmpSupport(bool $disable = false): bool */ public static function isNumber($number): bool { - return (bool) preg_match('`^([+-]{1})?([0-9]+(\.[0-9]+)?|\.[0-9]+)$`', $number); + return (bool) preg_match('`^[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+)$`', $number); } /** @@ -310,7 +310,8 @@ protected static function validateInputNumber($number): string } /** - * @param string|int $integer + * @param string|int $integer up to INT_32|64 since it's only used for things + * like exponents, it should be enough * * @throws \InvalidArgumentException * diff --git a/tests/MathTest.php b/tests/MathTest.php index 88c87da..a068e3a 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -373,9 +373,9 @@ public function maxMinData() /** * @dataProvider maxMinData * - * @param string|int|Math[] $param - * @param string|int|Math $min - * @param string|int|Math $max + * @param (string|int|Math)[] $param + * @param string|int|Math $min + * @param string|int|Math $max */ public function testMax(array $param, $min, $max) { @@ -390,9 +390,9 @@ public function testMax(array $param, $min, $max) /** * @dataProvider maxMinData * - * @param string|int|Math[] $param - * @param string $min - * @param string $max + * @param (string|int|Math)[] $param + * @param string $min + * @param string $max */ public function testMin(array $param, $min, $max) { @@ -996,14 +996,40 @@ public function isNumberData() 'number' => '42 ', 'expected' => false, ], + [ + 'number' => new Math('42 '), + 'expected' => true, + ], [ 'number' => '000', 'expected' => true, ], + [ + 'number' => '000.', + 'expected' => false, + ], [ 'number' => '.000', 'expected' => true, ], + [ + 'number' => '-.000', + 'expected' => true, + ], + [ + 'number' => '+.000', + 'expected' => true, + ], + + [ + 'number' => '--27', + 'expected' => false, + ], + + [ + 'number' => '++27', + 'expected' => false, + ], ]; }