[php-src] PR #18068: ext/bcmath: Supports scientific notation
| From: | SakiTakamachi | Date: | Fri, 14 Mar 2025 14:25:15 +0000 |
| Subject: | [php-src] PR #18068: ext/bcmath: Supports scientific notation | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-31075@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18068
Author: SakiTakamachi
related #17876
This change is to support code like this:
```
var_dump(
new BcMath\Number('1.23e1'),
new BcMath\Number('4.567E4'),
new BcMath\Number('8.9e-2'),
new BcMath\Number('1.2345E-5'),
);
```
output:
```
object(BcMath\Number)#1 (2) {
["value"]=>
string(4) "12.3"
["scale"]=>
int(1)
}
object(BcMath\Number)#2 (2) {
["value"]=>
string(5) "45670"
["scale"]=>
int(0)
}
object(BcMath\Number)#3 (2) {
["value"]=>
string(5) "0.089"
["scale"]=>
int(3)
}
object(BcMath\Number)#4 (2) {
["value"]=>
string(11) "0.000012345"
["scale"]=>
int(9)
}
```
I've used the
Number class as an easy example to follow, but functions will accept
these values as well.
## Benchmark
This change will make it a bit slower, but that may be tolerable.
(This is just a prototype, so there is a possibility that it could be improved further.)
code:
```
for ($i = 0; $i < 4000000; $i++) {
$num = new BcMath\Number('1.2345678901234567890');
}
```
result:
```
Benchmark 1: /php-dev/sapi/cli/php /mount/bc/num/1.php
Time (mean ± σ): 352.8 ms ± 1.7 ms [User: 342.4 ms, System: 5.1 ms]
Range (min … max): 349.6 ms … 354.9 ms 10 runs
Benchmark 2: /master/sapi/cli/php /mount/bc/num/1.php
Time (mean ± σ): 345.0 ms ± 4.2 ms [User: 334.1 ms, System: 5.3 ms]
Range (min … max): 341.8 ms … 352.7 ms 10 runs
Summary
'/master/sapi/cli/php /mount/bc/num/1.php' ran
1.02 ± 0.01 times faster than '/php-dev/sapi/cli/php /mount/bc/num/1.php'
```