Skip to content

Commit bb8d3ca

Browse files
committed
Minor fixes and typos
1 parent 56ff8c4 commit bb8d3ca

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ composer require "fab2s/math"
2323

2424
## In practice
2525

26-
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()`.
26+
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()`.
2727

2828
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.
2929

@@ -116,7 +116,7 @@ The way floats are handled in general and by PHP in particular is the very the r
116116

117117
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).
118118

119-
`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)
119+
`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)
120120

121121
```php
122122
// set global precision

src/Math.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function fromBase($number, int $base): self
8282
}
8383

8484
/**
85-
* @param string|static $number
85+
* @param string|int|static $number
8686
*
8787
* @throws \InvalidArgumentException
8888
*
@@ -94,7 +94,7 @@ public function gte($number): bool
9494
}
9595

9696
/**
97-
* @param string|static $number
97+
* @param string|int|static $number
9898
*
9999
* @throws \InvalidArgumentException
100100
*
@@ -106,7 +106,7 @@ public function gt($number): bool
106106
}
107107

108108
/**
109-
* @param string|static $number
109+
* @param string|int|static $number
110110
*
111111
* @throws \InvalidArgumentException
112112
*
@@ -118,7 +118,7 @@ public function lte($number): bool
118118
}
119119

120120
/**
121-
* @param string|static $number
121+
* @param string|int|static $number
122122
*
123123
* @throws \InvalidArgumentException
124124
*
@@ -130,7 +130,7 @@ public function lt($number): bool
130130
}
131131

132132
/**
133-
* @param string|static $number
133+
* @param string|int|static $number
134134
*
135135
* @throws \InvalidArgumentException
136136
*
@@ -182,7 +182,7 @@ public function toBase($base): string
182182
*
183183
* @return string
184184
*/
185-
public function format(int $decimals = 0, string $decPoint = '.', string $thousandsSep = ' '): string
185+
public function format($decimals = 0, string $decPoint = '.', string $thousandsSep = ' '): string
186186
{
187187
$decimals = max(0, (int) $decimals);
188188
$dec = '';

src/MathBaseAbstract.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static function gmpSupport(bool $disable = false): bool
163163
*/
164164
public static function isNumber($number): bool
165165
{
166-
return (bool) preg_match('`^([+-]{1})?([0-9]+(\.[0-9]+)?|\.[0-9]+)$`', $number);
166+
return (bool) preg_match('`^[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+)$`', $number);
167167
}
168168

169169
/**
@@ -310,7 +310,8 @@ protected static function validateInputNumber($number): string
310310
}
311311

312312
/**
313-
* @param string|int $integer
313+
* @param string|int $integer up to INT_32|64 since it's only used for things
314+
* like exponents, it should be enough
314315
*
315316
* @throws \InvalidArgumentException
316317
*

tests/MathTest.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,9 @@ public function maxMinData()
373373
/**
374374
* @dataProvider maxMinData
375375
*
376-
* @param string|int|Math[] $param
377-
* @param string|int|Math $min
378-
* @param string|int|Math $max
376+
* @param (string|int|Math)[] $param
377+
* @param string|int|Math $min
378+
* @param string|int|Math $max
379379
*/
380380
public function testMax(array $param, $min, $max)
381381
{
@@ -390,9 +390,9 @@ public function testMax(array $param, $min, $max)
390390
/**
391391
* @dataProvider maxMinData
392392
*
393-
* @param string|int|Math[] $param
394-
* @param string $min
395-
* @param string $max
393+
* @param (string|int|Math)[] $param
394+
* @param string $min
395+
* @param string $max
396396
*/
397397
public function testMin(array $param, $min, $max)
398398
{
@@ -996,14 +996,40 @@ public function isNumberData()
996996
'number' => '42 ',
997997
'expected' => false,
998998
],
999+
[
1000+
'number' => new Math('42 '),
1001+
'expected' => true,
1002+
],
9991003
[
10001004
'number' => '000',
10011005
'expected' => true,
10021006
],
1007+
[
1008+
'number' => '000.',
1009+
'expected' => false,
1010+
],
10031011
[
10041012
'number' => '.000',
10051013
'expected' => true,
10061014
],
1015+
[
1016+
'number' => '-.000',
1017+
'expected' => true,
1018+
],
1019+
[
1020+
'number' => '+.000',
1021+
'expected' => true,
1022+
],
1023+
1024+
[
1025+
'number' => '--27',
1026+
'expected' => false,
1027+
],
1028+
1029+
[
1030+
'number' => '++27',
1031+
'expected' => false,
1032+
],
10071033
];
10081034
}
10091035

0 commit comments

Comments
 (0)