Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122767 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 qa.php.net (Postfix) with ESMTPS id 79A411A009C for ; Wed, 27 Mar 2024 00:37:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711499853; bh=ikO0OHX83Np2KGwDEBAOv4x2acDPg0syJFEx1FNvWP0=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=XOAF9xgM0gXVymMWA/n245Fh/B+gfD8PPaoEayC4ki6Lfs5tnNGh0tWgikoT8S9LW iAffMUofSPxFnvpDw8GphtpQYlh6NcMIk1eKmz8h2RdlUoy5NehxZHT6t9UVMld9Wy F/4dI+y8/afsHWTvzez0KKgOsJZxEckjhyqoAO4Oe5yyzXLMTwg/eCOs3oCeG1Atda DP0ZxG+y911jnZH0LAr5e0kVrzyA2eyQa8Qars0GUg7belpvYtcX7H7Vtpf1zol6ii B5B71tr2LZR08/+xxe28unfQWfO7gLy8RLipaNWk6V1CwSwOwaxM78NVMMi9doBzwn rN9dFXfU3sr7A== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CAECC180635 for ; Wed, 27 Mar 2024 00:37:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No 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 ; Wed, 27 Mar 2024 00:37:32 +0000 (UTC) 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 B955A401DD; Wed, 27 Mar 2024 09:37:05 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=sakiot.com; s=default; t=1711499825; bh=ikO0OHX83Np2KGwDEBAOv4x2acDPg0syJFEx1FNvWP0=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=vcS1SB8GK9zbfX4CDjON28AoSZuGyypwAsdJ7Gi0eRufpi8QIEMNAani+qv9tyg8B 2kO/gFgqd5uOno0jKU/9+CkpOnHXIVWLe4CLDIVG2ohkgd7gaN5+jtccG1rhBW/D9k 3cYRJy2xA6jbAbCos8J5m01N2fw9uKsO1DIuRUIk= Content-Type: text/plain; charset=us-ascii Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.700.6\)) Subject: Re: [PHP-DEV] [RFC] [Discussion] Support object type in BCMath In-Reply-To: <23c22396-4389-405a-ab24-b66070f58c01@alec.pl> Date: Wed, 27 Mar 2024 09:36:52 +0900 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <9094EA74-584A-45EF-A887-5776F132C4BF@sakiot.com> References: <4F094EDA-5058-407D-AF39-06FD934FDE1F@sakiot.com> <23c22396-4389-405a-ab24-b66070f58c01@alec.pl> To: Aleksander Machniak X-Mailer: Apple Mail (2.3731.700.6) From: saki@sakiot.com (Saki Takamachi) Hi Aleksander, > Here's another question. >=20 > 1. Since we have withScale(), do we need to inherit the $scale = argument from the functional API? Can't we derive it from the object the = method is being invoked on? >=20 > So, instead, e.g. >=20 > public function add(BcNum|string|int $num, ?int $scale =3D null): = BcNum {} > public function sqrt(?int $scale =3D null): BcNum {} >=20 > I'd suggest: >=20 > public function add(BcNum|string|int $num): BcNum {} > public function sqrt(): BcNum {} >=20 > but I have no clue about BCMath. Yeah, you're right. By using `withScale` before calculating, we can = obtain results of arbitrary precision without using Scale during = calculation. The code in that case would look like this: ``` $num =3D new Number('1.23'); $num2 =3D new Number('4.56'); $numRescaled =3D $num->withScale(4); $result =3D $numRescaled->add($num2); $result->value; // '5.7900' $result->scale; // 4 ``` On the other hand, if allow the calculation method to specify a scale, = we can write it like this: ``` $num =3D new Number('1.23'); $num2 =3D new Number('4.56'); $result =3D $num->add($num2, 4); $result->value; // '5.7900' $result->scale; // 4 ``` It's just one less line, but that's the only reason to support the = `$scale` argument. However, for calculations other than mul and div, the calculation = results will always fit within the scale. ``` $num =3D new Number('1.23'); // scale 2 $num2 =3D new Number('1'); // scale 0 $num->add($num2); // '2.23', scale 2 is enough $num->sub($num2); // '0.23', scale 2 is enough $num->mod($num2); // '0.23', scale 2 is enough ``` On the other hand, for mul, div, and pow, the original `$num` scale may = not be enough. ``` $num =3D new Number('1.23'); // scale 2 $num2 =3D new Number('1.1'); // scale 1 $num->mul($num2); // '1.353' be '1.35', scale 2 is not enough ``` ``` $num =3D new Number('1.23'); // scale 2 $num2 =3D new Number('4'); // scale 0 $num->div($num2); // '0.3075' be '0.30', scale 2 is not enough $num->pow($num2); // '2.28886641', be '2.28' scale 2 is not enough ``` For mul and pow, can calculate the required scale. However, for div, the = calculation may never end, such as `0.33333....`, so it is not possible = to calculate the scale to fit the result completely. In cases like this, we thought that some users would want to easily = specify the scale, so we created an easy-to-use signature. However, it may not be necessary to specify scale for all calculation = functions. Is it reasonable to specify only mul, div, pow, or only div? Regards. Saki