Re: [RFC] [Discussion] Add clamp function
| From: | Larry Garfield | Date: | Thu, 28 Aug 2025 19:48:17 +0000 |
| Subject: | Re: [RFC] [Discussion] Add clamp function | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128590@lists.php.net to get a copy of this message | ||
On Thu, Aug 28, 2025, at 1:26 PM, Kyle Katarn wrote:
> Hi,
>
> 2025-08-28 at 09:37, Tim Düsterhus <tim@bastelstu.be> wrote :
>> There's precedent in ext/random with the Randomizer::getFloat() method
>> that takes a Random\IntervalBoundary enum to decide on the the
>> interpretation of the boundary values. Translated to k
>> ¡¯}r=[E5"O_
`;clamp() this
>> could mean:
>>
>> $value = 1.0;
>> clamp($value, 0.0, 1.0, IntervalBoundary::ClosedOpen); //
>> 0.999999999999999888978
>>
>> Best regards
>> Tim Düsterhus
>
> Just my opinion but the function aims to provide an alternative to
> min(max()) that would be:
> - easier to read
> - faster to execute
>
> If having many different behaviors depending on an option and so a lot
> of if-else path complexity, then it will tend to defeat the second
> goal. I would add it if there is a strong interest, otherwise I'd
> rather stick to a minimalist implementation (like seen in most
> languages where clamp() is available.
I would also favor the minimalist/traditional/like-everyone-else approach. I wondered at first if
open/closed boundaries made sense, but on consideration, that's not what clamp() traditionally
does. It's just min/max shortcutting. So let's just do min/max shortcutting and move on
with life.
--Larry Garfield