Newsgroups: php.internals,php.internals Path: news.php.net Xref: news.php.net php.internals:128546 php.internals:128547 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 0FB241A00BC for ; Mon, 25 Aug 2025 00:50:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1756082911; bh=NQpi1Lqsg5kVafaUMJtOjGh/z3zrk9Fp8eZl4oucTcQ=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=n/zzjyUEf1iL4OcJD8kmAJ/73YBEl838arhnf6qOIa4OTWNKRX51ZrGPuk4NuU5+w 55K7EoeaxrYY6snlN5L5miHD/K/t6NIesSEqjSBFIP3Ocv1mUMjQBMn19vbYJOpuhi PtLuO5Ld6x0/8B6/4KirlEuRSj7NOgiBBKTTS9QNNK2z7LmKLHwoOZmxAUH3W3x8tc qa44FCXaRxS/mqYMQfXfumsG299HBm/8rlSqNGb+zBzpckdUDdN5goHFUXR7p2LB8u 63symtvETBy23hmwDJzKoQndsmA1d6URTjEXKE8itcz7tS2fuWUt18zT0cv7026xP3 SBpwtnT/xu+XA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2176818003F for ; Mon, 25 Aug 2025 00:48:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,DMARC_MISSING, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from franklin.smtp.mailx.hosts.net.nz (franklin.smtp.mailx.hosts.net.nz [43.245.52.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 25 Aug 2025 00:48:28 +0000 (UTC) Received: from 122-57-11-211-adsl.sparkbb.co.nz ([122.57.11.211] helo=[192.168.1.67]) by franklin.smtp.mailx.hosts.net.nz with esmtpsa authed as varteg.nz (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_128_GCM:128) (Exim 4.96) (envelope-from ) id 1uqLPI-003P47-1f; Mon, 25 Aug 2025 12:49:56 +1200 Message-ID: <56f7a20a-553d-4ae2-8c48-3ae2b42043e0@varteg.nz> Date: Mon, 25 Aug 2025 12:49:51 +1200 Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] Add clamp function To: Kyle Katarn , Weedpacket@varteg.nz Cc: internals@lists.php.net References: <0027ab13-89fb-40b4-991b-2f88a35c2f31@gmx.de> <011001dc1358$287f1860$797d4920$@glaive.pro> <70b0d91d-c415-42b5-b02d-7e346efaea01@varteg.nz> Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Hosts-DKIM-Check: none From: Weedpacket@varteg.nz (Morgan) On 2025-08-25 02:27, Kyle Katarn wrote: > 2025-08-22 at 22:29, Morgan > wrote: > > On 2025-08-23 05:29, Kyle Katarn wrote: > > > > Also from my point of view,  min($max, max($min, $value)) has a > counter- > > intuitive reading when used to clamp a value. > > Well, that's trivial to deal with: > > min(max($min, $value), $max)); > > > It's not just about the order of parameters, it's still not intuitive to > call max() with $min and min() with $max, you still have to think about > what it does step by step to make sense out of it, while clamp($value, > min: $min, $max) or clamp(min: $min, value: $value, max: $max) will > provide a syntax that immediately make sense for next reader. > Well, clamp(min,value,max) isn't that far from minmax(min,value,max) and then chuck a couple more parentheses in there. Still, > Like the author of the RFC v1 also mentioned, min(max()) is inefficient > (see the inner complexity of those functions). > Yes, min/max are regarded as array functions, and doesn't special-case the situation where two arguments are passed; no particular reason why that couldn't be done as it seems to be a common use case. Otherwise, the bulk of the complexity comes from type handling and that won't go away in a robust implementation of clamp. Note that I am not against introducing clamp(). It's certainly not going to hurt to write clamp(min:$min, value:$value, max:$max) instead of min(max($min, $value), $max). I'm going to have to remember to put the parameter names in, because "$min <= $value <= $max" seems by far the most natural ordering (and is how "this value lies between these two extremes" is written in maths). There is also the use case of returning something other than $min if $value<$min (and other than $max if $value>$max), allowing clamp to filter values that are within the range or too high or too low. That would mean another one or two optional parameters. For numeric values there's also the closely related rescale() function, which is basically rescale($value, $min, $max) = ($value - $min) / ($max - $min) (it maps the interval [0,1] to [$min,$max] and finds where $value gets carried to by the mapping). Again, a couple more parameters would allow specifying initial intervals other than [0,1].