Skip to content

Commit

Permalink
Minor fixes and typos
Browse files Browse the repository at this point in the history
  • Loading branch information
fab2s committed Aug 10, 2019
1 parent 56ff8c4 commit bb8d3ca
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function fromBase($number, int $base): self
}

/**
* @param string|static $number
* @param string|int|static $number
*
* @throws \InvalidArgumentException
*
Expand All @@ -94,7 +94,7 @@ public function gte($number): bool
}

/**
* @param string|static $number
* @param string|int|static $number
*
* @throws \InvalidArgumentException
*
Expand All @@ -106,7 +106,7 @@ public function gt($number): bool
}

/**
* @param string|static $number
* @param string|int|static $number
*
* @throws \InvalidArgumentException
*
Expand All @@ -118,7 +118,7 @@ public function lte($number): bool
}

/**
* @param string|static $number
* @param string|int|static $number
*
* @throws \InvalidArgumentException
*
Expand All @@ -130,7 +130,7 @@ public function lt($number): bool
}

/**
* @param string|static $number
* @param string|int|static $number
*
* @throws \InvalidArgumentException
*
Expand Down Expand Up @@ -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 = '';
Expand Down
5 changes: 3 additions & 2 deletions src/MathBaseAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
38 changes: 32 additions & 6 deletions tests/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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,
],
];
}

Expand Down

0 comments on commit bb8d3ca

Please sign in to comment.