On 25/08/2022 12:33, Peter Bowyer wrote:
  Is this not one time where using a flag to change a function's behaviour
makes sense? So rather than fdiv, floor_div, int_div (etc) we have div($1,
$2, DIV_FLOAT), div($1, $2, DIV_FLOOR), div($1, $2, DIV_INT).

I'm not proposing div (non namespaced) as the name as I expect it clashes
with a lot of UDF's, but it works well in this example.


That probably makes more sense than a dedicated function - it's similar to the $mode parameter of round(), which has different strategies for what to do with halves.

The obvious place would be on the existing intdiv() function - it's not a choice of "integer or floor", it's a choice of strategy within integer division.

But then we face the awkward asymmetry of the current functionality:

intdiv() - Integer division
/ - Floating-point division
% - Integer modulus
fmod() - Floating-point modulo

Apparently as of PHP 8.0, there is also an fdiv() function, but the manual doesn't currently explain why it exists, and whether it will always give the same result as / (and if not, why not?)

So, it *might* make sense to have the following:

intdiv(int $num1, int $num2, int $mode = PHP_DIV_TRUNCATE): int
intmod(int $num1, int $num2, int $mode = PHP_DIV_TRUNCATE): int

Where $mode can also be PHP_DIV_FLOOR, and possibly additional algorithms in future - the paper linked to on microsoft.com discusses another three, and implies that there are many more.

Regards,

--
Rowan Tommins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to