Hi internals!

I would like to propose adding three static methods — `sum()`, `max()`, and 
`min()` — to BcMath\Number.
Before moving forward, I’d like to gauge whether there is general interest or 
demand for such functionality.

Of course, I'm aware that these operations can be implemented easily in 
userland code. However, having them as built-in methods provides a clear 
performance advantage.

In a quick benchmark using a prototype implementation, I observed that `sum()` 
was approximately 5x faster than a userland implementation, while `max()` and 
`min()` were roughly 2x faster.

I also experimented with an `avg()` method, but found that the performance 
difference between built-in and userland implementations was negligible. For 
that reason, I'm not including it in this proposal.

The proposed method signatures are as follows:
```
public static function sum(array $nums, ?int $scale = null): Number {}
public static function max(array $nums, ?int $scale = null): Number {}
public static function min(array $nums, ?int $scale = null): Number {}
```

Each element in `$nums` should be of type `Number`, `string`, or `int`.

For reference, here are the userland implementations I used for comparison:

sum:
```
$nums = [/* omit */];
$num_0 = new BcMath\Number(0);
foreach ($nums as $num) {
   $num_0 += $num;
}
```

max (and min with slight modification):
```
$nums = [/* omit */];
$num_0 = array_shift($nums);
foreach ($nums as $num) {
   $num_0 = $num_0 >= $num ? $num_0 : $num;
}
```

Regards,

Saki

Reply via email to