Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122918 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 251A31A009C for ; Thu, 4 Apr 2024 00:54:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712192104; bh=YxA/iKamcYYOMzi92SkzsdmRtEoyKCw0LjSagYPMYHA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=iktTab8X7Xsak68aEmNTFzDwLg6caajS/7yXpqyv570f1c53T07lpqpkX/ic2D/oR oL0D4G7wRcoIur206LMUIpfncNNwPwdK9tuWp6smpKreCtbXapEH0eMsAvRsvNyNyX cR/W27XVVB4bizCgkmzkE1xSGp04hOsrKyZJwufizyq32pt5/P4L0CCGCZkaHP01gF lzCWbpEKcmlT6WRWwrgc91/YuydaLSkIUTjdisf8AioOyNuiU3WjA9N6YX0KDsaJwV 2Fu508BOvPPnpZZU3cuOceDNTIHmkJBg+9wAH41f2rPxbCNAnYUulFpaj3z9amD4M+ 5KUSG+IiHn0MQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9DB7F180086 for ; Thu, 4 Apr 2024 00:55:03 +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-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 ; Thu, 4 Apr 2024 00:55:03 +0000 (UTC) Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2a274c4a21aso324317a91.0 for ; Wed, 03 Apr 2024 17:54:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712192073; x=1712796873; 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=HUUOpE8KdCyyDatWH/nWPhNnzOKaQxCfkEUFE73a59Y=; b=NfnwPk3TRx7jeK7BgM9/JNkYSKiHYHERMbxp7o5Oy982v+pX8EkXDMtg4hea2bSFRX ZiEba7hopym393GwrcDYpnTY7cDB/6EtI+Dv16WcjBULYzZxVpD1XOwX2hr2NgNeWTzu tZifIZP6ltaDvEyiznOtLlrAzxEiSyYUuRiY8dTH2KfBokN6ZNWTd9FpqTJJdhdrxlmT bJcaDLmLYCEPUj2C1lR4mnYzorP+GcyyBpTyomRocju7sNf2nfiJ1O4Idx7W1EI6bg39 pMk7sexbWfmLtVeQLp+6CkjP9iGBirZ5ttxNFAjn1xjkSmWUFuphvNf2b7wMhJ+VLNcw ugNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712192073; x=1712796873; 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=HUUOpE8KdCyyDatWH/nWPhNnzOKaQxCfkEUFE73a59Y=; b=WwPeOQw/pZt80KnPD4/vESnf+oebWzETofXq4rk6SgOFIn3CB7CRqLvAhzOi0+EneU Am9C8OakpwO2xVWtL450mr+4Nj1Lt/gFcHsYky3uN5BE/SgWgf1N+90oWRimlnZQAuJH mc2LuoRkEvuf3G1iBnt5TlNL18U+RZhV8ZNd9YS9S2MvtzWMc/+zOObWSORg/ZDSJ3C4 A9UAtt8fbQ86MpBxS+FPqqVbWpVa9VpyXxmVH261GyMpqLyiWnUOqDWqMZdYcxRJa534 nuY1EC4zl4VFhVRnz0S5pL1VwLV12SpfewFbKRYmXnzeWY4FputH8AA9b9tGDdG55RMA 8z5g== X-Forwarded-Encrypted: i=1; AJvYcCUbXFylJdWdO0AVUSf+hNLuocMqvKf1N2y97L01KsjzvzmR2a3RIzBPOIRTIH3BlckJSC1ZTJonimmlNQfM6tdR1OCzkxzBzg== X-Gm-Message-State: AOJu0Yxqh0wt1aVvrKf4NujB6STFW9h2khh/kIiVaM0/b4mj+1q2h27s qMkPkAfYCndSVM5xufYED7oYi1zlDesrxEl0Ktr3USMBY+TZinpWVIM7r17+057ksnTzz9oryxQ HhhW0FzZio7hKG8DHAQl5Fx5ST+A= X-Google-Smtp-Source: AGHT+IFMuqMgyqlb8kMfpU567sFGXukbD1ouXvQuaOGlnvdBSGGu6nygC7pvlaLQh6j+0Hc2qnFEa3yz5Sa5CL8NrUo= X-Received: by 2002:a17:90a:4688:b0:2a2:b2bd:e1b1 with SMTP id z8-20020a17090a468800b002a2b2bde1b1mr997451pjf.22.1712192072898; Wed, 03 Apr 2024 17:54:32 -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> <8FB87901-02D7-4934-9119-55B21CEDDA9D@sakiot.com> In-Reply-To: <8FB87901-02D7-4934-9119-55B21CEDDA9D@sakiot.com> Date: Wed, 3 Apr 2024 17:54:19 -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="0000000000004ae01906153ac82b" From: jordan.ledoux@gmail.com (Jordan LeDoux) --0000000000004ae01906153ac82b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Apr 2, 2024 at 5:43=E2=80=AFPM Saki Takamachi wro= te: > > If make a class final, users will not be able to add arbitrary methods, s= o > I think making each method final. Although it is possible to completely > separate behavior between method and opcode calculations, this is > inconsistent and confusing to users and should be avoided. > Right, if a class is not final but has only final methods (including > constructor) and no protected properties then it allows people to extend = it > but not break any encapsulation or (I think) impose any more BC > requirements on the maintainer (unless we care about conflicting with > method names of child classes when updating). It lets people just do > something a bit like adding 'extension methods' in C#. I like it. People > could write e.g. `$bcChildNumber->isPrime()` instead of > `BcNumberUtils::isPrime($bcNumber)` Yeah, I suspect the most common child class will be something like "Money" that additionally has a parameter denoting what currency the value is in, since that is by far the most common use case for arbitrary precision math in PHP. Making the calculation methods final should do fine in my opinion. > How about setting the `$scale` and `$roundMode` that I mentioned earlier > in the constructor instead of `div` and `pow`? By doing so, we can use an= y > scale when calculating with opcodes. If we want to specify a different > scale for each calculation, you can do it using methods. > Or would it be more convenient to have a method to reset `$scale` and > `$roundMode`? It is immutable, so when reset it returns a new instance. > I think that having it be an optional part of the constructor and having a method to modify it is probably sufficient to help with operator overloads. Importantly, it still needs to take that setting and use it intelligently depending on the operation as we've discussed, but that should cover the use cases I was imagining. The exact behavior should be very clearly documented and explained though, as this is a different way of interacting with BCMath than the functions. It's better I think, because it takes advantage of the fact that this will be state-aware, which the functions aren't, and is one of the largest benefits of using objects. Also, to address an earlier question you had Saki, the term used should be "scale". Scale is the total number of digits to the right of the decimal point. Precision is the total number of significant digits. The BCMath C library works on scale, I believe, so that should carry over here. Most other arbitrary precision libraries in C use precision. Jordan --0000000000004ae01906153ac82b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


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

If make a class final, users will not be able to add arbitrary methods, so = I think making each method final. Although it is possible to completely sep= arate behavior between method and opcode calculations, this is inconsistent= and confusing to users and should be avoided.
=C2=A0<= /div>

Right, if a class is not final but has only final methods (including constructor) and no protected properties then it allows people to extend it but not break any encapsulation or (I think) impose any more BC requirements on the maintainer (unless we care about conflicting with method names of child classes when updating). It lets people just do something a bit like adding 'extension methods' in C#. I like it. People could write e.g. `$bcChildNumber->isPrime()` instead of `BcNumberUtils::isPrime($bc= Number)`

Yeah, I suspect the most common ch= ild class will be something like "Money" that additionally has a = parameter denoting what currency the value is in, since that is by far the = most common use case for arbitrary precision math in PHP. Making the calcul= ation methods final should do fine in my opinion.
=C2=A0
<= /div>
How about setting the `$scale` and `$roundMode` that I mentioned earlier in= the constructor instead of `div` and `pow`? By doing so, we can use any sc= ale when calculating with opcodes. If we want to specify a different scale = for each calculation, you can do it using methods.
Or would it be more convenient to have a method to reset `$scale` and `$rou= ndMode`? It is immutable, so when reset it returns a new instance.

I think that having it be an optional part of t= he constructor and having a method to modify it is probably sufficient to h= elp with operator overloads. Importantly, it still needs to take that setti= ng and use it intelligently depending on the operation as we've discuss= ed, but that should cover the use cases I was imagining.

The exact behavior should be very clearly documented and explained t= hough, as this is a different way of interacting with BCMath than the funct= ions. It's better I think, because it takes advantage of the fact that = this will be state-aware, which the functions aren't, and is one of the= largest benefits of using objects.

Also, to addre= ss an earlier question you had Saki, the term used should be "scale&qu= ot;. Scale is the total number of digits to the right of the decimal point.= Precision is the total number of significant digits. The BCMath C library = works on scale, I believe, so that should carry over here. Most other arbit= rary precision libraries in C use precision.

J= ordan
--0000000000004ae01906153ac82b--