Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127661 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 59FEF1A00BC for ; Sun, 15 Jun 2025 09:07:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1749978349; bh=ruebP461YkLOKj3+QYwHjsYXUHeGpMHsJBrwra1DO+s=; h=From:Subject:Date:To:From; b=Act0oFAnPlnX7fDa0sXwhhFJD4DxKDaVwnIO0t0RTT6Nql1msYiNdX9tq8Ul5BKOJ iAiI1bA8WW3ul79CMXmu+C/pXmIyyvUW/Ytme7kbJIDEZuk8I1wO4jNsNi/tLSQVqf 02w0Xew/arFdlMRDsauf59hWgc5K5GDpVt7LTDDerY5CZRtM7Qa3zsFQj2RkxliMBO jR1NPgkVLb6DQrNA8CAgP6Z3Dtm5Qu5+isoOVG/XFVjJD2fshYUAcVRkl55J58aOLX zmJCtbTzJDQpLkxpbFoYfYVNXpJDh6oKE46g9hZjvSVMC5PdzlUYSUakZfWI4a0Yk2 fHE4O1xoDXEUA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 08DEF180053 for ; Sun, 15 Jun 2025 09:05:48 +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_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail.sakiot.com (mail.sakiot.com [160.16.227.216]) (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 ; Sun, 15 Jun 2025 09:05:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=sakiot.com; s=default; t=1749978463; bh=ruebP461YkLOKj3+QYwHjsYXUHeGpMHsJBrwra1DO+s=; h=From:Subject:Date:To:From; b=cccPMOUKltWi8lmR0fyFga87iqTFEo1/3k4IJZ8hCu7E9vOJqSwEYTwutkAkfxMCF /iSrz13wTBqF3Q6U+Wbnu4y4zPOTnc6hLnMYEp+tFNI6UdlAJVIpOwbepqYSu8wjVT 0Aev/abT0j52DKUK2seIYYFcCk2MTn1tdc6ywtEw= Received: from smtpclient.apple (unknown [117.55.37.250]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.sakiot.com (Postfix) with ESMTPSA id 7131B401FB for ; Sun, 15 Jun 2025 18:07:43 +0900 (JST) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.600.51.1.1\)) Subject: [PHP-DEV] Proposal for `BcMath\Number::sum()`, `max()`, and `min()` Message-ID: Date: Sun, 15 Jun 2025 18:07:31 +0900 To: PHP internals X-Mailer: Apple Mail (2.3826.600.51.1.1) From: saki@sakiot.com (Saki Takamachi) Hi internals! I would like to propose adding three static methods =E2=80=94 `sum()`, = `max()`, and `min()` =E2=80=94 to BcMath\Number. Before moving forward, I=E2=80=99d like to gauge whether there is = general interest or demand for such functionality. Of course, I'm aware that these operations can be implemented easily in = userland code. However, having them as built-in methods provides a clear = performance advantage. In a quick benchmark using a prototype implementation, I observed that = `sum()` was approximately 5x faster than a userland implementation, = while `max()` and `min()` were roughly 2x faster. I also experimented with an `avg()` method, but found that the = performance difference between built-in and userland implementations was = negligible. For that reason, I'm not including it in this proposal. The proposed method signatures are as follows: ``` public static function sum(array $nums, ?int $scale =3D null): Number {} public static function max(array $nums, ?int $scale =3D null): Number {} public static function min(array $nums, ?int $scale =3D null): Number {} ``` Each element in `$nums` should be of type `Number`, `string`, or `int`. For reference, here are the userland implementations I used for = comparison: sum: ``` $nums =3D [/* omit */]; $num_0 =3D new BcMath\Number(0); foreach ($nums as $num) { $num_0 +=3D $num; } ``` max (and min with slight modification): ``` $nums =3D [/* omit */]; $num_0 =3D array_shift($nums); foreach ($nums as $num) { $num_0 =3D $num_0 >=3D $num ? $num_0 : $num; } ``` Regards, Saki=