Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128588 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 66B941A00BC for ; Thu, 28 Aug 2025 18:26:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1756405493; bh=dWnZt6HdhQY6il/wKXiAON4W3S4SjcK/nyT14cq612Y=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=DPXjAWGnuO9PkV76PMDp/NthkHuPTCmTtcY6/ML+1jPx7Gb0cDvAf/yXvmTjCtiht /nRYYn7W5TlUqAvsiGthnrJi8u2+imXdLvgWBSzxR49l4pqzveCwsMxXNyD7kJGGk3 awciIT3W2gGNU6faEl9j/Z/b+MMqpDrK6Uu5gCXmGIDCFx71ARCNIfOCGoV4E9sS6v hC1X0yPlKWk9H8QZCSSHoggLfdukY1LGqwk+j2mfnGd9JyWc8mRNcKhVsWGwPeIq0s oMRTto4QJUWON13Yfgfnw2mqW9OYTm5FHdEmfza51XH0NMt+kKV+VEbv7vM5ZCRGov 42P8EBTmIrMig== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B67691801EE for ; Thu, 28 Aug 2025 18:24:51 +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.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 28 Aug 2025 18:24:50 +0000 (UTC) Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7704f3c46ceso1180396b3a.2 for ; Thu, 28 Aug 2025 11:26:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756405580; x=1757010380; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=HzP3XVdNAJvwUOs0IqWsoBSkA1uZ5BHk2IftysGllzo=; b=Ud5rDSo7jwWbHEiSja7UB7i3k9y1roZVTVcCmF6RsFSQZuBVEpTnYh2bM+lbIwim2y I6eAPBVC5ijT5YYh0y2PYe1iiiSKK/QCG5E/Lr1ccThEb71wbQFqm37Bo8oSfroGzBCD bW9qDi7ttBXkvi3NbLRcueVC2+D5rgehP6aYliYtvbWB/0sth4QjJNXt3CRXOTVCc41x NCpDnppBH7hvyh0zS2AZGVQk6iHiE+T5HnZZH+V2btMdIYQEnKfUyFWHvaL+KMsCGZVn prHJCyhysUCtbggGxYkntrv2y/log6VGXmrbsRfTc9OlW7gt2zCv4/hIx0Ym+rltHQh2 /i7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756405580; x=1757010380; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HzP3XVdNAJvwUOs0IqWsoBSkA1uZ5BHk2IftysGllzo=; b=rw9M2+oMZ/iVQ2hCZV4b4/2wp/6FqXwSFzm6hzYTK8hznuWTZ+sIPBNEaJtj93fBIz x+2/5nWGaB1iHdH1WvIQrvzKS97p1Ol6z8YWE5y81/oqMzq5LuHUqWCbVasnJHFfnsCF 03pvrLLz/rjkNGHSai2BbB8HpVwQDuDb5ekdIEmhNO920NzaDd2vfIu0x7jw3XkKTfXN ietMSgqly49Hq+rvQ0fjYSUrvXVfStU+j/bY9YM34JG49QbLwPmRqKhtT9hd756aWuo9 f0rcc8v+JPWrFifVZAl1vpiUk/+AZGXcnmWSjTvQtRAyfb9Y3hO+GFF69wxC+Lee1ZrQ Wcaw== X-Forwarded-Encrypted: i=1; AJvYcCUam5Je5EcqYGzcu5lGD5iA4UAiE4ewDydgWfZ64kbI0MosmD5rkJ40HFSSL8LCQS0C6OY3d5vPaBA=@lists.php.net X-Gm-Message-State: AOJu0YwIbDvHopN8qDTuP7r2bTPWovwDBdFCgNNlI27W/9yU7niRbK/T NfoCh4z207T+dlHD0xSmLVAUNP43/EMXA0o7VsvWqKKJtAAReyEZVyM+hV6NZAGFpgyl0neWJEY FTs1NkDf1or/DGpq5eb+V+oKHlBCl+NA= X-Gm-Gg: ASbGnculXEG7XpBt3sLjjK0ZkikPx2j3JTtGzqW9ETBeIvmgzwCgcqrAqkftJ8IUfiV gWjNZD2tBPCd3lC7O0v5KTSJbzkQvJyeLhl7Bu5f3+bJCyH08C1CzttynG5gJycoSNozahBpo9X /Gr3/lBuLN70x24GmUm8cUnqcCK90QNex1b57jrgSY4dzKVJV/fF0qlFMwPEvc+W3XQzSz8YiNB NAqO4E9 X-Google-Smtp-Source: AGHT+IG8kdVAqM9IHzV1i2kzqQuPkuR8T16GUMLGMLrM6Y93ITyDbUZ46k39/RXfpx8IgjAHIwPSiyKaUrAR0+9YRVw= X-Received: by 2002:a17:902:c946:b0:248:fc2d:3a21 with SMTP id d9443c01a7336-248fc2d3d40mr15376265ad.4.1756405580474; Thu, 28 Aug 2025 11:26:20 -0700 (PDT) Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <1756361767126.3727989781.4204927711@yahoo.de> <3b1bd29cd42074b499d38e10cb97bf42@bastelstu.be> In-Reply-To: <3b1bd29cd42074b499d38e10cb97bf42@bastelstu.be> Date: Thu, 28 Aug 2025 20:26:08 +0200 X-Gm-Features: Ac12FXyZjZc9WRoIt-l1kweC4EpuWESVUMCScolFNoxOGM1SJkqzp-FbhHH_-l4 Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] Add clamp function To: =?UTF-8?Q?Tim_D=C3=BCsterhus?= Cc: Hans Krentel , ocramius@gmail.com, PHP Internals Content-Type: multipart/alternative; boundary="000000000000b4aee5063d710a0d" From: kylekatarnls@gmail.com (Kyle Katarn) --000000000000b4aee5063d710a0d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, 2025-08-28 at 09:37, Tim D=C3=BCsterhus wrote : > Hi > > Am 2025-08-28 08:35, schrieb Hans Krentel: > > This also highlights a minor but important clarification for the > > documentation. To make the behavior perfectly unambiguous, I suggest > > a small edit to explicitly state the bounds are inclusive. > > This is a good point and I agree that it should be spelled out > explicitly. However to me this also raises the question if the interval > boundaries should be configurable to switch between closed intervals and > (half-)open intervals. For many applications of continuous values a > right-open interval (excluding the upper boundary) is more useful, since > this makes it easy to generate non-overlapping ranges (e.g. to clamp to > a given day when passing DateTimeImmutable). This would also provide an > additional value-add over the min-max construction, where the boundaries > would need to be calculated manually, which is super complicated to do > in userland for floats. > > 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 `clamp()` this > could mean: > > $value =3D 1.0; > clamp($value, 0.0, 1.0, IntervalBoundary::ClosedOpen); // > 0.999999999999999888978 > > Best regards > Tim D=C3=BCsterhus > 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. --000000000000b4aee5063d710a0d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

2025-08-28 at 09:37, Tim D= =C3=BCsterhus <tim@bastelstu.be&= gt; wrote :
Hi
Am 2025-08-28 08:35, schrieb Hans Krentel:
> This also highlights a minor but important clarification for the
> documentation. To make the behavior perfectly unambiguous, I suggest > a small edit to explicitly state the bounds are inclusive.

This is a good point and I agree that it should be spelled out
explicitly. However to me this also raises the question if the interval boundaries should be configurable to switch between closed intervals and (half-)open intervals. For many applications of continuous values a
right-open interval (excluding the upper boundary) is more useful, since this makes it easy to generate non-overlapping ranges (e.g. to clamp to a given day when passing DateTimeImmutable). This would also provide an additional value-add over the min-max construction, where the boundaries would need to be calculated manually, which is super complicated to do
in userland for floats.

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 `clamp()` this
could mean:

=C2=A0 =C2=A0 =C2=A0$value =3D 1.0;
=C2=A0 =C2=A0 =C2=A0clamp($value, 0.0, 1.0, IntervalBoundary::ClosedOpen); = //
0.999999999999999888978

Best regards
Tim D=C3=BCsterhus

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 be= haviors depending on an option and so a lot of if-else path complexity, the= n it will tend to defeat the second goal. I would add it if there is a stro= ng interest, otherwise I'd rather stick to a minimalist implementation = (like seen in most languages where clamp() is available.
--000000000000b4aee5063d710a0d--