Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122903 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 8AC081A009C for ; Wed, 3 Apr 2024 00:05:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712102776; bh=+oQE/SJzMIEsWP+M5OnEX397S7zwYM2SnFvQJe1otl0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=itFujMWtPyYU80YhB++FuNFgkkzmtQH/rWMivEQLHaCcjyYES1U8QUqdWYn+SuJFN MGaREHbQGwnlkqgQ4O4jbD5qU+135AJ9WXJgaXQrgoVw5c1UjpIBcpNjTfdtn7AJ6u wVIfdg6A5IxjuzM82k2DF3qfIpB9IdGvvKJB/vYOcoMsz0zcMsKVMCle11B94+Wmtm j7GxNNaFqJYjWyqaAK0QhyNLSs21f0lKKiFg9+/TutTRkkrxDsleIxXinsGlH7NsSE xtYXoT5JezTjmCdEqY4/zn0QQaXpa8FTJO4osqS0fKZJwULl6IyfpyJyMNo0eHDedw 5KZS2gA69mIoQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CFBBE180737 for ; Wed, 3 Apr 2024 00:06:15 +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,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, 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-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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, 3 Apr 2024 00:06:15 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2a04ac98cf7so323776a91.0 for ; Tue, 02 Apr 2024 17:05:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712102745; x=1712707545; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=+oQE/SJzMIEsWP+M5OnEX397S7zwYM2SnFvQJe1otl0=; b=CKUSEZiXQf/yqArSabxrsvzQOtAcef424BJawYfdcyDcVb/5zVQICozSjLQkE9WGhE W9lf9jrj+yl+P40gp1LpCXJTu8RBECUQqaqOCQr5uLBprgKjtCgI2ZA0b1tqAusFvd/b wGcIOl1xaxK28UrKCdUbg0SgLvjVE0AGtJs2fYCexhbjm11zoX3VwG435niVMx4h5JBc A8adlb2ZfoZGlqg2xWDqOFSv2vZK1kBahipbvU6SRjuagralDYVSsTWEiUdqYP13Lf+y 1ZHXha9En/Htop/CLzORSTsz2Hqw9tkZKl1He712GoeTYeNzz3IEBqjbWAHaNbR8H7dV bxNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712102745; x=1712707545; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+oQE/SJzMIEsWP+M5OnEX397S7zwYM2SnFvQJe1otl0=; b=J0m2OJ2vyUh3lLQkm3As/Bi8z1cvdmFUc9NgfxaSWOPPJ/Dpxmes8+Ga9KY5YpkMA5 XK27QCT/t72P9apvggGkJNfbNRYJQjV1iIMCgHPkut5so+tQBUC9sr0I6UkA43mNvrA7 Zsh15AmcnlxxO1KcORqraLsPzYhD90e/zadUykfsammMn4NHIgWKJVcyTrs1UxNs2Sva M59R98/XKFvbdF1WdOHMTacNF39rfuUhWxdC/2y1RbIZ5Cuc0B/Yds46+M278IUTfvMQ MspNsZt85YoEZB+OhF9mWyEXXMTNp48vipq9vY1qk3xPZeHuQ9BPPIH2hTI2cGVUtGI4 GlDg== X-Forwarded-Encrypted: i=1; AJvYcCVcNzS32VBKumeyzjtm2q+JO8JzX0UspldfPSXSe+hwjJkApo2KK/GgqfI4uRi3w3+cREnAOQy3j7ras0ed2PHHdIikpQjWnw== X-Gm-Message-State: AOJu0YxGo4v38dSEC4NGqa4Ry++zF0gw2qBXNB0MupI+gCnHEYsTOkeo aR4GZGofv4KBHv6hA3bXgnfNgH++wGci3lKq995D7LSV2veRlwaf0AOsiCdAx0n/5zTmzVwQBlb 4bJUQHJYru+9qahm0JiEzThY0nZ4= X-Google-Smtp-Source: AGHT+IEBAlyjAutF/xCTwSidzUBobIpfDryBeZGUynG9foWz0g59bFMvEa1tnJq8UaY3zFLp3hv49CaeVNeyXYi2RcY= X-Received: by 2002:a17:90a:1f86:b0:2a2:5f73:a578 with SMTP id x6-20020a17090a1f8600b002a25f73a578mr1352181pja.13.1712102745593; Tue, 02 Apr 2024 17:05:45 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <4F094EDA-5058-407D-AF39-06FD934FDE1F@sakiot.com> <68CF373E-6ABF-4471-8992-813B4BA1B508@sakiot.com> <904197f4-afb5-401e-9e17-7a655c5449d0@alec.pl> <655FEA80-9AB4-4AAD-A310-70ED968C97A2@sakiot.com> In-Reply-To: <655FEA80-9AB4-4AAD-A310-70ED968C97A2@sakiot.com> Date: Tue, 2 Apr 2024 17:05:30 -0700 Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] Support object type in BCMath To: Saki Takamachi Cc: Lynn , Aleksander Machniak , php internals Content-Type: multipart/alternative; boundary="000000000000f8628e061525fb4f" From: jordan.ledoux@gmail.com (Jordan LeDoux) --000000000000f8628e061525fb4f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Apr 2, 2024 at 4:50=E2=80=AFPM Saki Takamachi wro= te: > > The two use cases at issue here are when the div and pow's exponent are > negative values. So how about allowing only these two methods to optional= ly > set `$scale` and `$roundMode` ? > > - The constructor takes only `$num` and always uses implicit scaling. > There is no option for the user to specify an arbitrary scale. > - `$scale`: If specified, use that value, otherwise use `10`. The scale > specified here is added to the scale of the left operand and used as the > scale of the result. In other words, `(new Number('0.01')->div('3', 2))` > results in `'0.0030' // scale =3D 2 + 2 =3D 4`. > - `$roundMode`: Specifies the rounding method when the result does not fi= t > within the scale. The initial value is `PHP_ROUND_TOWARD_ZERO`, which > matches the behavior of the BCMath function. That is, just truncate. > - If lucky enough to get the result within the scale, apply the implicit > scale to the result. In other words, if calculate `1 / 2`, the resulting > scale will be `1`, even if scale is `null` or specify a value such as `20= ` > for scale. > - The result of a calculation with operator overloading is the same as if > the option was not used when executing the method. > > However, I'm not sure if naming it `$scale` is appropriate. > > Also, since `BCMath\Number` is not made into a final class, there is a > possibility of implementing an inherited class in userland. Regarding thi= s, > is it better to make the calculation method a final method, or to use a > function overridden by the user when executing from the opcode? > > The issue is that, presumably, this method will be used within the operator overload portion of the class entry in C. If it is allowed to be overridden, then this RFC is sort of providing a stealth operator overload to PHP developers. As much as I am for operator overloads having written an RFC for it, and as much as I find the arguments generally against it lacking, I am not in favor of doing it that way with a kind of... unspoken capability to overload the basic math operators in userland. I very much like the feature, but I also think it should be intentionally and specifically designed, which is why I spent a long time on it. I do not get a vote for RFCs, but I would vote against this if I could just for that reason IF the calculation methods were not private, the class was not final, AND the function entry was used in the operator overload. And operator overloads are also the place where what you outlined above gets murky. I think what you outlined is very close to a good final design for just the method usage side, but the operator usage side CANNOT provide a scale or a rounding mode. That should be taken into consideration, because allowing this object to be used with operators is probably the single largest benefit this RFC will provide to PHP developers. What I ended up doing was that the VALUE of the object was immutable, but the other information was not immutable. That has its own downsides, but does allow for very explicit control from the developer at the section of code using the class, but also avoids creating copies of the object or instantiating a new object for every single "setting" change during calculations. Jordan --000000000000f8628e061525fb4f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Tue, Apr 2, 2024 at 4:50=E2=80=AFP= M Saki Takamachi <saki@sakiot.com= > wrote:

The two use cases at issue here are when the div and pow's exponent are= negative values. So how about allowing only these two methods to optionall= y set `$scale` and `$roundMode` ?

- The constructor takes only `$num` and always uses implicit scaling. There= is no option for the user to specify an arbitrary scale.
- `$scale`: If specified, use that value, otherwise use `10`. The scale spe= cified here is added to the scale of the left operand and used as the scale= of the result. In other words, `(new Number('0.01')->div('3= ', 2))` results in `'0.0030' // scale =3D 2 + 2 =3D 4`.
- `$roundMode`: Specifies the rounding method when the result does not fit = within the scale. The initial value is `PHP_ROUND_TOWARD_ZERO`, which match= es the behavior of the BCMath function. That is, just truncate.
- If lucky enough to get the result within the scale, apply the implicit sc= ale to the result. In other words, if calculate `1 / 2`, the resulting scal= e will be `1`, even if scale is `null` or specify a value such as `20` for = scale.
- The result of a calculation with operator overloading is the same as if t= he option was not used when executing the method.

However, I'm not sure if naming it `$scale` is appropriate.

Also, since `BCMath\Number` is not made into a final class, there is a poss= ibility of implementing an inherited class in userland. Regarding this, is = it better to make the calculation method a final method, or to use a functi= on overridden by the user when executing from the opcode?


The issue is that, presumably, this me= thod will be used within the operator overload portion of the class entry i= n C. If it is allowed to be overridden, then this RFC is sort of providing = a stealth operator overload to PHP developers. As much as I am for operator= overloads having written an RFC for it, and as much as I find the argument= s generally against it lacking, I am not in favor of doing it that way with= a kind of... unspoken capability to overload the basic math operators in u= serland. I very much like the feature, but I also think it should be intent= ionally and specifically designed, which is why I spent a long time on it. = I do not get a vote for RFCs, but I would vote against this if I could just= for that reason IF the calculation methods were not private, the class was= not final, AND the function entry was used in the operator overload.
=

And operator overloads are also the place where what yo= u outlined above gets murky. I think what you outlined is very close to a g= ood final design for just the method usage side, but the operator usage sid= e CANNOT provide a scale or a rounding mode. That should be taken into cons= ideration, because allowing this object to be used with operators is probab= ly the single largest benefit this RFC will provide to PHP developers.

What I ended up doing was that the VALUE of the object= was immutable, but the other information was not immutable. That has its o= wn downsides, but does allow for very explicit control from the developer a= t the section of code using the class, but also avoids creating copies of t= he object or instantiating a new object for every single "setting"= ; change during calculations.

Jordan
--000000000000f8628e061525fb4f--