Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123068 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 83C7C1A009C for ; Tue, 9 Apr 2024 19:27:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712690871; bh=ojElNdyPON7mnk/6DTNdHWwqzLjIeJL0H/HOiE/FxXA=; h=Date:Subject:To:References:From:In-Reply-To:From; b=l//jvTY83aEzZ5rfwjkadSqHKPrarxMOZLXAQaQN7IkXkPzFouEKn5HcXDuwVN34h ET1L5RRGC8PGv2/VxprCmi9xNSrEVvJbg+KAqz2id1B49P5EgY0hFSXrmGPRaDLb9t 3b5T3j91znnf0PfykTTdpFEX/VO1VoQzRMAs8LbyAPt3AVTfUA52Srqri39acon/vH HEBPFD8zVw4do5SzYTDiFBrIQOnkwcUaPaU9UTpd0iEJldONUD3LOxlZv5ZHmHFzt0 ogo2h51pm7pSY/U5eSvy46Vy3IVA3cKqo1ADHxi9LXQh6/OmPEOxKxbF6XYP8OvuYX yUn7gLTRrdhqw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7E91F180A79 for ; Tue, 9 Apr 2024 19:27:50 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,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 fout6-smtp.messagingengine.com (fout6-smtp.messagingengine.com [103.168.172.149]) (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 ; Tue, 9 Apr 2024 19:27:49 +0000 (UTC) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.nyi.internal (Postfix) with ESMTP id 7E1FA1380185 for ; Tue, 9 Apr 2024 15:27:16 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Tue, 09 Apr 2024 15:27:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1712690836; x=1712777236; bh=FhTwszVUJZ Xd0zoZyKMdtt5O/4ICKJWg5SX9FRvQwbo=; b=IOpnpiOLUCI6GeXRiVi9nLKUVq m2OIFr07qiWBsqwN55M2fkAKgwAgstDfNeK5e6G+Egnv1Kh3s2rjRN9C9JuUmGwb Bnvd+T/cD6yutmPL0hVwiOI+dw2PL2woCBpFr1KNtUrVhnuyyEfp8jlZqrScmySG Oo0Gw/RyPkWZamva94r+F6N/eUa8UsXVRQWqQv4urfNPpLl1UYZYZRYBsiNcZ2Hl d9D24Wj5HcM4kk8ix/SSfWs763IrOJamUWMILN13QpUbY8WZmQ5maXiCcs138SxG Zpuo9L90SC19VEVSYJobree7d2SFRIHN9CRdgqXY/hCjaDuPguV9bRehdOQg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1712690836; x=1712777236; bh=FhTwszVUJZXd0zoZyKMdtt5O/4IC KJWg5SX9FRvQwbo=; b=VyFncsOOFlvM88HMHfhV20TRdLKWMlB1mTXRM1JwVC+Y G7jYW2Qkkk994QBFAonZ8Y5r6eP5mHpljfLaLIBKopHNM5QA7UrG4pJupKtBIDqj Dvr7Mqzdz8YnIoZSuTsysaH3lPGOXugB2wfsbcZ510vhGDnXMJvGjeS2NcatFd0l SjwLjYW3Zi8KbGMC7mGa4ePM6LXQK9W/xaB9HCG4aJw/Tg+emF3ltGbGsjThOShx EmNDld7eP2QK3rgftVbnVUsD/JcucaLCvr0CMOAMjIKBIdd+2jtraLBHbFhbCFfH hZxPvuslHOeSmKxjSqiJsua0f/b4KsHz9aJbFFfaIQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudehgedgtdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpegtkfffgggfuffvfhfhjgesrgdtre ertddvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhsucglkffoufhorfgnfdcu oehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecuggftrfgrthhtvghrnhepge fghfdvgeefueffteekkeelfeduvdegjeetudevveetgedvfeejleefgedtkeevnecuffho mhgrihhnpehphhhprdhnvghtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomhepihhmshhophdrphhhphesrhifvggtrdgtohdruhhk X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 9 Apr 2024 15:27:14 -0400 (EDT) Content-Type: multipart/alternative; boundary="------------tvwiJpQj0Jir0JeTAfYr811P" Message-ID: <6628b5e3-5f4b-4c9c-a940-56f72a645af1@rwec.co.uk> Date: Tue, 9 Apr 2024 20:27:11 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] [Discussion] Support object type in BCMath To: internals@lists.php.net References: <4F094EDA-5058-407D-AF39-06FD934FDE1F@sakiot.com> Content-Language: en-GB In-Reply-To: <4F094EDA-5058-407D-AF39-06FD934FDE1F@sakiot.com> From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") This is a multi-part message in MIME format. --------------tvwiJpQj0Jir0JeTAfYr811P Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 24/03/2024 13:13, Saki Takamachi wrote: > https://wiki.php.net/rfc/support_object_type_in_bcmath Based on the various discussions we've been having, I'd like to propose a simplified handling of "scale". I think there are two groups of users we are trying to help: a) Users who want an "infinite" scale, and will round manually when absolutely necessary, e.g. for display. The scale can't actually be infinite in the case of calculations like 1/3, so they need some safe cut-off. b) Users who want to perform operations on a fixed scale, with configurable rounding, e.g. for e-commerce pricing. They are not interested in any larger scale, except possibly in some intermediate calculations, when they want the same as group (a). I propose: - The constructor accepts string|int $num only. - All operations accept an optional scale and rounding mode. - If no rounding mode is provided, the default behaviour is to truncate. This means that (new BCMath\Number('20'))->div(3, 5) has the same result as bcdiv('20', '3', 5) which is 6.66666 - If a rounding mode is provided, the object transparently calculates one extra digit of scale, then rounds according to the specified mode. - If no scale is provided, most operations will automatically calculate the required scale, e.g. add will use the larger of the two scales. This is the same as the current RFC. - If no scale is provided to div(), sqrt(), or pow(-$x), the result will be calculated to the scale of the left-hand operand, plus 10. This is the default behaviour in the current RFC. - Operator overloads behave the same as not specifying a scale or rounding mode to the corresponding method. Therefore (new BCMath\Number('20')) / (new BCMath\Number('3')) will result in 6.6666666666 - an automatic scale of 10, and truncation of further digits. Compared to the current RFC, that means: - Remove the ability to customise "max expansion scale". For most users, this is a technical detail which is more confusing than useful. Users in group (b) will never encounter it, because they will specify scale manually; advanced users in group (a) may want to customise the logic in different ways anyway. - Remove the ability for a Number value to carry around its own default rounding mode. Users in group (a) will never use it. Users in group (b) are likely to want the same rounding in the whole application, but providing it on every call to new Number() is no easier than providing it on each fixed-scale calculation. - Remove the $maxExpansionScale and $roundingMode properties and constructor parameters. - Remove withMaxExpansionScale and withRoundMode. - Remove all the logic around propagating rounding mode and expansion scale between objects. I've also noticed that the round method is currently defined as: - public function round(int $precision = 0, int $mode = PHP_ROUND_HALF_UP): Number {} Presumably $precision here is actually the desired scale of the result? If so, it should probably be named $scale, as in the rest of the interface. I realise it's called $precision in the global round() function; that's presumably a mistake which is now hard to fix due to named parameters. Ideally, it would be nice to have both roundToPrecision() and roundToScale(), but as Jordan explained, an implementation which actually calculated precision could be difficult and slow. Regards, -- Rowan Tommins [IMSoP] --------------tvwiJpQj0Jir0JeTAfYr811P Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
On 24/03/2024 13:13, Saki Takamachi wrote:
https://wiki.php.net/rfc/support_object_type_in_bcmath


Based on the various discussions we've been having, I'd like to propose a simplified handling of "scale".

I think there are two groups of users we are trying to help:

a) Users who want an "infinite" scale, and will round manually when absolutely necessary, e.g. for display. The scale can't actually be infinite in the case of calculations like 1/3, so they need some safe cut-off.

b) Users who want to perform operations on a fixed scale, with configurable rounding, e.g. for e-commerce pricing. They are not interested in any larger scale, except possibly in some intermediate calculations, when they want the same as group (a).

I propose:

- The constructor accepts string|int $num only.

- All operations accept an optional scale and rounding mode.

- If no rounding mode is provided, the default behaviour is to truncate. This means that (new BCMath\Number('20'))->div(3, 5) has the same result as bcdiv('20', '3', 5) which is 6.66666

- If a rounding mode is provided, the object transparently calculates one extra digit of scale, then rounds according to the specified mode.

- If no scale is provided, most operations will automatically calculate the required scale, e.g. add will use the larger of the two scales. This is the same as the current RFC.

- If no scale is provided to div(), sqrt(), or pow(-$x), the result will be calculated to the scale of the left-hand operand, plus 10. This is the default behaviour in the current RFC.

- Operator overloads behave the same as not specifying a scale or rounding mode to the corresponding method. Therefore (new BCMath\Number('20')) / (new BCMath\Number('3')) will result in 6.6666666666 - an automatic scale of 10, and truncation of further digits.

Compared to the current RFC, that means:

- Remove the ability to customise "max expansion scale". For most users, this is a technical detail which is more confusing than useful. Users in group (b) will never encounter it, because they will specify scale manually; advanced users in group (a) may want to customise the logic in different ways anyway.

- Remove the ability for a Number value to carry around its own default rounding mode. Users in group (a) will never use it. Users in group (b) are likely to want the same rounding in the whole application, but providing it on every call to new Number() is no easier than providing it on each fixed-scale calculation.

- Remove the $maxExpansionScale and $roundingMode properties and constructor parameters.

- Remove withMaxExpansionScale and withRoundMode.

- Remove all the logic around propagating rounding mode and expansion scale between objects.

I've also noticed that the round method is currently defined as:

- public function round(int $precision = 0, int $mode = PHP_ROUND_HALF_UP): Number {}

Presumably $precision here is actually the desired scale of the result? If so, it should probably be named $scale, as in the rest of the interface.

I realise it's called $precision in the global round() function; that's presumably a mistake which is now hard to fix due to named parameters.

Ideally, it would be nice to have both roundToPrecision() and roundToScale(), but as Jordan explained, an implementation which actually calculated precision could be difficult and slow.

Regards,

-- 
Rowan Tommins
[IMSoP]
--------------tvwiJpQj0Jir0JeTAfYr811P--