Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127769 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 5A3501A00BC for ; Fri, 27 Jun 2025 13:00:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1751029104; bh=CKqMSRnHtGJ+nDJq0Sz2MJgu0zstMv2v7GJcPXGGJxs=; h=From:Subject:Date:References:Cc:In-Reply-To:To:From; b=UBNlEYm2fyJb33C8EfwTgt0EWiit3as93TRKaJykrbxpaMhk12OPs00ykm145gEHF IMuynVrG936uWNFYb7bqAzGtvKKsKRbSl6tQZ7mZ3k+dU8vMZTZvipYGSz4gObd+Fc BoS7hwGJtvOql8F8bsg5WRs6JoxGreCSGP+Z019or1LuAX7T6tJaqgfpRFa84xNtzI rC/0P2Cbx85b4xy89zrj25D5CmZHCoAJVC6DKQ6ygd2l+IZYlL4ly0dbUT7PuPuU0e coPdgQ9DXz7Qh45im/zuk6flLgUf0PAI6qN1r072gSAobNQJ+KpfnCvNQZVnkc4Brp Vr7gwW5gp9w3Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 395C3180041 for ; Fri, 27 Jun 2025 12:58:20 +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=-2.1 required=5.0 tests=BAYES_00,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 ; Fri, 27 Jun 2025 12:58:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=sakiot.com; s=default; t=1751029211; bh=CKqMSRnHtGJ+nDJq0Sz2MJgu0zstMv2v7GJcPXGGJxs=; h=From:Subject:Date:References:Cc:In-Reply-To:To:From; b=IMyejKIOYr5Y+dvbl5c+R3HKRtPYA2cZpUhDsFz75iIFdM23rrNpzAGUHMaklG3Hl XmHQYK8Gm6zGcYBkZJiOy2M7xaH/aNcXkiKD2JBEToIP291tU6I+hCD1jYNCla2h3/ chhA0ZWxXoHAPIva7JT8kp6RHcaviQSOkuLOymPs= Received: from smtpclient.apple (unknown [117.55.37.250]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.sakiot.com (Postfix) with ESMTPSA id D34B340210; Fri, 27 Jun 2025 22:00:11 +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 (1.0) Subject: Re: [PHP-DEV] Allow BcMath\Number to work identically to bcmath functions Date: Fri, 27 Jun 2025 21:59:59 +0900 Message-ID: <189192D8-2505-4742-9FD9-E3CD2789A2B9@sakiot.com> References: <20250626143156.5AA9D1A00BD@lists.php.net> Cc: internals@lists.php.net In-Reply-To: <20250626143156.5AA9D1A00BD@lists.php.net> To: Russell Lee X-Mailer: iPhone Mail (22F76) From: saki@sakiot.com (Saki Takamachi) > =EF=BB=BFWhen `BcMath\Number` was introduced it allowed `string|int` metho= d arguments. According to a separate RFC to allow `int` to the functions thi= s was done as a performance optimization. Unfortunately this also means it e= ffectively disallows `float` types because PHP will truncate them to integer= s (and emit a deprecation report) instead of preserving them as strings. >=20 > Example: https://3v4l.org/Bi5Kl >=20 > ```php > var_dump( > (string)new BcMath\Number(5)->sub(4.2, 1), > bcsub(5, 4.2, 1), > ); >=20 > string(3) "1.0" > string(3) "0.8" > ``` >=20 > Per SakiTakamachi: > > If there is strong demand for creating the object from a `float`, it wou= ld be better to add support for `float` rather than removing `int` from the s= ignature. > > As with the bc functions, converting a `float` to a `string` can be easi= ly handled within php-src. > > > > In any case, discussion on the mailing list is necessary. >=20 >=20 > I was hoping to be able to use Number as a drop-in replacement for various= operations, especially back-of-the-napkin calculations, but this behavior m= akes it too easy to accidentally write `$number * 1.2` and get unexpected re= sults. >=20 > There are many arguments that could be made that float is the wrong type, t= hat it has rounding issues, and possibly other edge cases, but in the vast m= ajority of cases `(string)$float` does exactly what is expected. The lack of= a non-float decimal numeric type doesn't leave much room for native express= ion short of doing `Number((string)$i) * Number((string)$j)` or `Number('1.2= ') * '1.2'`. >=20 > The expectation is that in PHP 9.0 float-to-int will be disallowed and wou= ld automatically drop back to float-to-string in this case, but that is stil= l a long way away and would mean being unable to recommend using Number in 8= .4, 8.5, and beyond. >=20 > Request context and discussion: > https://github.com/php/php-src/issues/18674 >=20 > BcMath\Number RFC: > https://wiki.php.net/rfc/support_object_type_in_bcmath >=20 > bcmath function int type RFC: > https://wiki.php.net/rfc/allow_int_args_to_bcmath_function Hi Russell! As I already mentioned in the issue thread, I want to clarify just in case: e= ven if we allow `float` as an argument, the internal behavior would essentia= lly be the same as passing `(string) $float_num`. In fact, implementing any kind of custom conversion logic should be avoided,= as it could lead to inconsistencies with PHP=E2=80=99s native casting behav= ior. Also, even if implicit casting behavior changes in PHP 9.0, please note that= this would not benefit methods when `strict_types=3D1`. Personally, I chose not to accept `float` in the method signature because I p= rioritize the precision of BCMath. To be honest, I=E2=80=99m curious about how much demand there actually is fo= r this kind of feature. Regards, Saki