Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122815 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 0F04D1A009C for ; Sat, 30 Mar 2024 02:11:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711764717; bh=MnDAJfrwlZsJKRDKYy1kPdMfCxMGoxUoUFI9QhfjwZQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=fGrJDYLfgEu3DIzNEr/2W0mb60ol4tOObt7OH5bThe6oJ3Qx6yrJk9697WXWQ0WWj 3r5HOi7YupZz4DHXThm73cWwWx0wh8Us8ZxVSgw5pNGL71kdPUYso885ik96pTJmLT 0rFV0OTS7HgpBQw1azH3/6k5+Aa1GJjUNcK/yT7w61FFgmBMPoGT9LCOrz4dhcl4fh JneUOnHi6EhVAuEB3X6uOz6XtIt81kEcs1J3WcKTzE28OFSSsss/UlicGN76d5EGJm cVKz6eAfTapMByz5lUOvwMChH5tdcbZZMH0aRgojMvS/NcFKDxAlm7sDtOn4kNk5ov VJ9E+kMdczpmw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BF997180554 for ; Sat, 30 Mar 2024 02:11:56 +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,LOTS_OF_MONEY,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-oo1-f41.google.com (mail-oo1-f41.google.com [209.85.161.41]) (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 ; Sat, 30 Mar 2024 02:11:56 +0000 (UTC) Received: by mail-oo1-f41.google.com with SMTP id 006d021491bc7-5a4789684abso1479941eaf.0 for ; Fri, 29 Mar 2024 19:11:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711764689; x=1712369489; 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=MnDAJfrwlZsJKRDKYy1kPdMfCxMGoxUoUFI9QhfjwZQ=; b=Lm3Zgn0/CZwqpj0HDSmy01QKlVwwsYqMPEI6JnXfEb1CVlh5KwIfb/lhY1mI9VGHR+ obtP0RjXaN83Ge1vOk/LgCFgCj0HQjFeP5Ui4fI/V2oaWyUicpuK4z5V/fvW6AaW8IEC WODKvn3Zh2Ya/7Rz+Yqbw0OssYDOQnUOzL5aTeSQ/VA2VuWnF5LZCLsGeq3GIzdVW7y+ 5ZmputwuNBkosabipBrsOTILO8Ag12YD2FtLzzQhFawdQ0b/+zSnIXNXabM66fiDvHCa Ny7yTX/x0fbqMnx8sq0LCmV1AwmnU7RIn+xrO9IF4f5vV/2VALTEQfnwcKbHv6AialbA oTTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711764689; x=1712369489; 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=MnDAJfrwlZsJKRDKYy1kPdMfCxMGoxUoUFI9QhfjwZQ=; b=p1gpjbAXloaXFY36I+gYleOFuHZLv2JwGvVKUu6vFqyDq02g/BGBu88oQl4iaudsCG rJHZMGTZluLW5g2hMvSi+Khaxa8WNKBosOvelbTkUr3IbsMG/RfaTfKlpvGfTwcvMIPg EEO/Ow/wS3t116MMkY4Q4lmmNk+xINSfAFQNT12dWpodBALQnMN1OrqfK3GMy9OAdQsn JIqm2CzN6lg9+lBaH0+4TRNhSUI5OYKFGKu5tsLWmva6uDwDgA+ZqjOZPABJ9pLS4zx4 gN3z0iXmsz1sW7fEMeF16P5o3lTABI0x8w1G2903pPYHVlv5UZZCoAzU4ty8izjobx/m CZ4w== X-Gm-Message-State: AOJu0Yyyuges/xhh6LJSNYos3/tvcHpJoyPWdUqI8+hCdDt7DdFDoOS8 iptSUxEkfSKfqNsXx9fQuoC/BXPjQ4S3lDzT8+yrWjB82oKbH+5UY7bf6our2hHg2+g6qt09yNJ QfoglvCoAOr8SAmQj5lERuozBImTtumI7vhs= X-Google-Smtp-Source: AGHT+IHiRp3CjGOPRGlmUsvyyK9gDfTix9lW00dS6/iNxanX3RyK9d5/MmuF/xjwQ1GolfeqikT/Pje0xueF2KLLW4Y= X-Received: by 2002:a05:6359:5f94:b0:17e:53f9:6985 with SMTP id lh20-20020a0563595f9400b0017e53f96985mr4812511rwc.14.1711764688969; Fri, 29 Mar 2024 19:11:28 -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> In-Reply-To: <904197f4-afb5-401e-9e17-7a655c5449d0@alec.pl> Date: Fri, 29 Mar 2024 19:11:15 -0700 Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] Support object type in BCMath To: Aleksander Machniak Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000039ae4a0614d74652" From: jordan.ledoux@gmail.com (Jordan LeDoux) --00000000000039ae4a0614d74652 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Mar 27, 2024 at 12:08=E2=80=AFAM Aleksander Machniak = wrote: > On 27.03.2024 01:03, Saki Takamachi wrote: > >> $num =3D new BcNum('1.23', 2); > >> $result =3D $num + '1.23456'; > >> $result->value; // '2.46456' > >> $result->scale; // ?? > > > > In this case, `$result->scale` will be `'5'`. I added this to the RFC. > > I'm not sure I like this. Maybe we should be more strict here and treat > the $scale in constructor (and later withScale()) as the actual scale > for all operations. > > For addition, it absolutely should expand scale like this, unless the constructor also defines a default rounding type that is used in that situation. All numbers, while arbitrary, will be finite, so addition will always be exact and known based on inputs prior to calculation. Treating scale like this isn't more strict, it's confusing. For instance: ``` $numA =3D new Number('1.23', 2); $numB =3D new Number('1.23456', 5); $expandedScale1 =3D $numA + $numB; // 2.46456 $expandedScale2 =3D $numB + $numA; // 2.46456 $strictScale1 =3D $numA + $numB; // 2.46 assuming truncation $strictScale2 =3D $numB + $numA; // 2.46456 ``` I ran into this same issue with operand ordering when I was writing my operator overload RFC. There are ways you could do the overload implementation that would get around this for object + object operations, but it's also mathematically unsound and probably unexpected for anyone who is going to the trouble of using an arbitrary precision library. Addition and subtraction should automatically use the largest scale from all operands. Division and multiplication should require a specified scale. Because of this, I'm not entirely sure that specifying a scale in the constructor is actually a good thing. It is incredibly easy to create situations, unless the implementation in C is VERY careful, where the operand positions matter beyond the simple calculation. Multiplication is commutative, but division is not. This would almost certainly lead to some very difficult to track down bugs. Putting scale in the constructor is similar to some of the examples of "possible misuse cases of operator overloading" that I had to go over when I was making my RFC. We definitely want to avoid that if possible for the first number/math object that has operator overloads. Jordan --00000000000039ae4a0614d74652 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable






Ad= dition and subtraction should automatically use the largest scale from all = operands. Division and multiplication should require a specified scale.

Because of this, I'm not entirely sure that speci= fying a scale in the constructor is actually a good thing. It is incredibly= easy to create situations, unless the implementation in C is VERY careful,= where the operand positions matter beyond the simple calculation. Multipli= cation is commutative, but division is not. This would almost certainly lea= d to some very difficult to track down bugs.

Putti= ng scale in the constructor is similar to some of the examples of "pos= sible misuse cases of operator overloading" that I had to go over when= I was making my RFC. We definitely want to avoid that if possible for the = first number/math object that has operator overloads.

Jordan
--00000000000039ae4a0614d74652--