Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115056 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 91062 invoked from network); 23 Jun 2021 08:44:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Jun 2021 08:44:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 29E7A1804CC for ; Wed, 23 Jun 2021 02:03:08 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from forward106j.mail.yandex.net (forward106j.mail.yandex.net [5.45.198.249]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 23 Jun 2021 02:03:07 -0700 (PDT) Received: from iva3-5cde98c480ea.qloud-c.yandex.net (iva3-5cde98c480ea.qloud-c.yandex.net [IPv6:2a02:6b8:c0c:4997:0:640:5cde:98c4]) by forward106j.mail.yandex.net (Yandex) with ESMTP id 5CCDF11A25B1 for ; Wed, 23 Jun 2021 12:03:04 +0300 (MSK) Received: from iva7-f62245f79210.qloud-c.yandex.net (iva7-f62245f79210.qloud-c.yandex.net [2a02:6b8:c0c:2e83:0:640:f622:45f7]) by iva3-5cde98c480ea.qloud-c.yandex.net (mxback/Yandex) with ESMTP id Yr2IeZniwa-34Ia4ZNt; Wed, 23 Jun 2021 12:03:04 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=php.watch; s=mail; t=1624438984; bh=kFVmKSIzang82L7cNlT3KbIkAl9i5J0PdKe+OumIDQ4=; h=To:Subject:From:In-Reply-To:Cc:Message-ID:Date:References; b=mJlwVZr/L9p5RSB4vMLvDGsdCAbQ9s7WgRSR2N+zkqpxDbFeLdDHa3IY7MC6ofm+/ MZgCC48KztLJK7kloNS2RVntNTk5im1OgqUNlmfCNXKh8MX3nkxM/BaVlaWdxCmJEd Eezp74E2XuF/cLtDtRIHA2UXGHEOqUbG/0ME0kaI= Authentication-Results: iva3-5cde98c480ea.qloud-c.yandex.net; dkim=pass header.i=@php.watch Received: by iva7-f62245f79210.qloud-c.yandex.net (smtp/Yandex) with ESMTPSA id S04FNCrKAO-332SteBL; Wed, 23 Jun 2021 12:03:03 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) Received: by mail-oi1-f174.google.com with SMTP id t40so2545425oiw.8 for ; Wed, 23 Jun 2021 02:03:03 -0700 (PDT) X-Gm-Message-State: AOAM533aU5WLcqpHdk3RUPPpIfMU3WWANiLY2qlhqzzix7GOGhYtEWZ5 XASIFY1C/WH6VykVXLY7zJkx7lRohQYYLV5tOXU= X-Google-Smtp-Source: ABdhPJw9YKHHI3C3hzoZKv+fxhOCZL+60Sl0BguuwX/1OVDMfeQkFpsskGkAaJ20HhFiJYlYmbDmcHbn0IVsaFrk8IU= X-Received: by 2002:aca:b609:: with SMTP id g9mr2371734oif.145.1624438981992; Wed, 23 Jun 2021 02:03:01 -0700 (PDT) MIME-Version: 1.0 References: <1014AE76-B3D5-48F7-A0F0-B8E3327123A2@gmail.com> In-Reply-To: <1014AE76-B3D5-48F7-A0F0-B8E3327123A2@gmail.com> Date: Wed, 23 Jun 2021 14:32:35 +0530 X-Gmail-Original-Message-ID: Message-ID: To: Kim Hallberg Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Proposal: clamp From: ayesh@php.watch (Ayesh Karunaratne) > Greetings internals, > > I present to you a proposal for a new basic math function: clamp. > > The main goal of this function is to contain a number inside a given boun= d. > And return the number if it is inside of that bound, and if not, and the > number is outside of the given bound, the nearest bound is returned. > > Now, does this need an RFC? The behaviour can be implemented in userland > in a number of ways, one of which is to compare the values in a if statem= ent. > Another is to use the min and max math functions. > > max($min, min($max, $num)); > > I don=E2=80=99t have any schpeel as to why this should be in core, only t= hat I thought it already was. > Considering it is part of other languages standard math library, and it i= s a useful function. > > Can see this being useful in image manipulation, server-side rendering an= d math just to name few. > > The proposed signature follows the convention of other math related funct= ions. > > function clamp(int|float $num, int|float $min, int|float $max): int|float= {} > > This function is very easy to implement, has no side effects or backward > compatibility issues, unless a userland implementation already has the fu= nction declared, > in which case PHP will throw a fatal error since the function cannot be r= edeclared. > > I've implemented this feature a few months ago already. > - Link: https://github.com/thinkverse/php-src/pull/1 > > What are your opinions on this function? > > Regards, > Kim Hallberg. +1 from me as well. We recently added `str_contains`/`starts_with`/`ends_with`, which were welcome additions. I think `clamp` would be a nice addition too. I found three user-land `clamp` function declarations on a search on GitHub public repos. - [doubleleft/hook](https://github.com/doubleleft/hook/blob/master/src/boo= tstrap/helpers.php#L129) - 764 stars, Last activity in 2016. - [GetSimpleCMS/GetSimpleCMS](https://github.com/GetSimpleCMS/GetSimpleCMS= /blob/master/admin/inc/basic.php#L2489) - 336 stars, Has recent activity. - [shish/shimmie2](https://github.com/shish/shimmie2/blob/master/core/poly= fills.php#L477) - 267 stars, Has recent activity. I think it's safe to say that these libraries will be updated to accommodate the `clamp` function in PHP core; I'd even happily send pull requests to them. `doubleleft/hook` probably wouldn't be updated because it doesn't show recent activity, but that's an acceptable BC impact for a nice addition IMO. Thank you, --- Ayesh.