Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122850 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 973A71ADD53 for ; Tue, 2 Apr 2024 09:09:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712049002; bh=qRMhuaIfmRUFb8AEk+cJzJP7QnDplyRWYruAqu+Di4w=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Qr+0V7XdLpZiBhExKIfGXfXJngMGxp3lba+0EChhfixz6qHV+H6NBqjDGf6yOar7K iTHYUax+dkGehJ1Qu+ttu9o1D6+bXgS6tf08msmTECZ5qwJZjMBT/ZVeVBHDneC1QP Cj6wlBEvmuS8ZOIVvIBJPq1k7CduklHLi9b5fuqd/S2BvS1jGTBMvheUuzLP1pf9Bp vxgaANCFOHsy6jAw1XFEZwZxdl/ybhJZuyvGHN1UOTM0SP/B6U0s4L5e+2EsZFFfqT k6S2itxYs9dMdkKFX0usnkabtqdA3RmdOLZG2axkKJzntsEKJPJj1P4xKsYTfSmebH Zs8fJFjYw8Igg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8188B180387 for ; Tue, 2 Apr 2024 09:09:58 +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-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (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, 2 Apr 2024 09:09:55 +0000 (UTC) Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-5d42e7ab8a9so2673071a12.3 for ; Tue, 02 Apr 2024 02:09:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712048966; x=1712653766; 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=qRMhuaIfmRUFb8AEk+cJzJP7QnDplyRWYruAqu+Di4w=; b=QVkPqMAtDbgANwVODO68ojC87+80YM990LcB1x8fZHzD9nEQfjaVdvLituZdwzuU4P 1FEw/30v9skwAeqinkZK5/jWTIbPI5DYNoL77HmBpGlUPJRoHV1yhYLz1abyUEUSgwYY 5xs0gAzBWksvZeEgi1xSCR9ObDutwkaWcWD4ezGlZ2QCeQ0FWkPZjava1tl4xGd+lR79 QTTbdMRlDNuIZb7CnU8Mja58NJz6bxlg8WE4ZZqmoo1qgFZxFqZdKactnjWq/1Mhut3B 3ZrTQAT5wJdQY+m22OcRliCVRvsWALp3MHjXSqrBSqLl8wTSWZpEJ0Z74caluUAld+9b obEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712048966; x=1712653766; 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=qRMhuaIfmRUFb8AEk+cJzJP7QnDplyRWYruAqu+Di4w=; b=qZpGnbYgnmfvf4x7Uzxlj2PKjWQgJRJr8pSrPDXgYitY9/q8+nJykfarxrhPRUTK6W kdbOaXPa01fZjGyDtcaVtqLvHtUaqBYp3gL/yjxj2ESQW0GprLQNvwQXGGWUtisEjH7O SJ6gsSfjw5p8Ui3IAth4AbgQSQ4yYg2eLcN8s1rGrdenvs0SQ42N5tAQZuYTLW5W+d1J dvvgCI+XhTmVKD1hsWAcybTQQREkCwmod1BIBqEBedfF86Yp9v2jb8AHY7yMMrthCqHx Xhmu0ahF/soDzqB+9DDI7KMwigUPLQivls1K3tNQn/9gniXtBtT8X1GaDm8e9W+OJfUj ysfw== X-Forwarded-Encrypted: i=1; AJvYcCX/mHEyK126GjC9Skz+6lhvy0bi9f3bBAn1a8q0MxeoqFNimvjl4xdPbzyIBER10A1/LlBQmt+QwLf5j6DKD9DBAzUSs4B6MQ== X-Gm-Message-State: AOJu0Yw7rniXi/SxAlxi6N3KalDb9Yknl7nxvkv2YgwcBGXXwmtTafLf UgCPQIHww2loNFgkEOMgRWV23WySzE6gjm3trAOFfAwX373ZRqukKuynNfbdf787/dAZUtcNZFD +LCE0XI7JZKY8S8oRo4q1xvNvzqIH2qGQWQQ= X-Google-Smtp-Source: AGHT+IFYDph3hAcKQUqgYnG+47qeP9xaTRerGa7sDEzpo9WH9PQUvAbBjW+68J8256LjtKtiA4XSuxfalcSG8ypVnnY= X-Received: by 2002:a05:6a20:7f9b:b0:1a3:c8c4:9c83 with SMTP id d27-20020a056a207f9b00b001a3c8c49c83mr11516580pzj.37.1712048966420; Tue, 02 Apr 2024 02:09:26 -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: Date: Tue, 2 Apr 2024 02:09:13 -0700 Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] Support object type in BCMath To: Saki Takamachi Cc: Aleksander Machniak , php internals Content-Type: multipart/alternative; boundary="0000000000007b588d061519763b" From: jordan.ledoux@gmail.com (Jordan LeDoux) --0000000000007b588d061519763b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Mar 30, 2024 at 5:09=E2=80=AFPM Saki Takamachi wr= ote: > Hi Jordan, > > Your opinion may be reasonable given the original BCMath calculation > order. That is, do you intend code like this? > > Signature: > ``` > // public function __construct(string|int $number) > // public function getNumber(?int $scale =3D null): string > ``` > > Add: > ``` > // public function add(Number|string|int $number): string > > $num =3D new Number('1.23456'); > $num2 =3D new Number('1.23'); > > $add =3D $num + $num2; > $add->getNumber(); // '2.46456' > $add->getNumber(1); // =E2=80=982.4' > > $add =3D $num->add($num2); > $add->getNumber(); // '2.46456' > $add->getNumber(1); // '2.4' > ``` > > Div: > ``` > // public function div(Number|string|int $number, int $scaleExpansionLimi= t > =3D 10): string > > > // case 1 > $num =3D new Number('0.0001'); > $num2 =3D new Number('3'); > > $div =3D $num / $num2; // scale expansion limit is always 10 > $div->getNumber(); // '0.0000333333333' > > $div =3D $num->div($num2, 20); > $div->getNumber(); // '0.00003333333333333333333' > $div->getNumber(7); // =E2=80=980.0000333' > > > // case 2 > $num =3D new Number('1.111111'); > $num2 =3D new Number('3'); > > $div =3D $num->div($num2, 3); > $div->getNumber(); // '0.370' > $div->getNumber(7); // =E2=80=980.3700000' > ``` > > Since the scale can be inferred for everything other than div, a special > argument is given only for div. > > Regards. > > Saki Something like the signature for `getNumber()` in this example would be a decent solution. Operations which have ambiguous scale (of which truly only div is in the BCMath library) should *require* scale in the method that calls the calculation, however for consistency I can certainly see the argument for requiring it for all calculation methods. The issue is how you want to handle that for operator overloads, since you cannot provide arguments in that situation. Probably the most sensible way (and I think the way I handled it as well in my library) is to look at both the left and right operand, grab the calculated scale of the input for both (or the set scale if the scale has been manually set), and then calculate with a higher scale. If internally it produces a rounded result, the calculation should be done at `$desireScale + 2` to avoid compound rounding errors from the BCMath library and then the implementation. If the result is truncated, the calculation should be done at `$desiredScale + 1` to avoid calculating unnecessary digits. So we have multiple usage scenarios and the behavior needs to remain consistent no matter which usage occurs, and what order the items are called in, so long as the resulting calculation is the same. **Method Call** $bcNum =3D new Number('1.0394567'); // Input scale is implicitly 7 $bcNum->div('1.2534', 3); // Resulting scale is 3 $bcNum->div('1.2534'); // Implicit scale of denominator is 4, Implicit scale of numerator is 7, calculate with scale of 8 then truncate **Operators** $bcNum =3D new Number('1.0394567'); // Input scale is implicitly 7 $bcNum / '1.2534'; // Implicit scale of denominator is 4, Implicit scale of numerator is 7, calculate with scale of 8 then truncate This allows you to perhaps keep an input scale in the constructor and also maintain consistency across various calculations. But whatever the behavior is, it should be mathematically sound, consistent across different syntax for the same calculation, and never reducing scale UNLESS it is told to do so in the calculation step OR during the value retrieval. Jordan --0000000000007b588d061519763b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Sat, Mar 30, 2024 at 5:09=E2=80=AF= PM Saki Takamachi <saki@sakiot.com> wrote:
Hi = Jordan,

Your opinion may be reasonable given the original BCMath calculation order.= That is, do you intend code like this?

Signature:
```
// public function __construct(string|int $number)
// public function getNumber(?int $scale =3D null): string
```

Add:
```
// public function add(Number|string|int $number): string

$num =3D new Number('1.23456');
$num2 =3D new Number('1.23');

$add =3D $num + $num2;
$add->getNumber(); // '2.46456'
$add->getNumber(1); // =E2=80=982.4'

$add =3D $num->add($num2);
$add->getNumber(); // '2.46456'
$add->getNumber(1); // '2.4'
```

Div:
```
// public function div(Number|string|int $number, int $scaleExpansionLimit = =3D 10): string


// case 1
$num =3D new Number('0.0001');
$num2 =3D new Number('3');

$div =3D $num / $num2; // scale expansion limit is always 10
$div->getNumber(); // '0.0000333333333'

$div =3D $num->div($num2, 20);
$div->getNumber(); // '0.00003333333333333333333'
$div->getNumber(7); // =E2=80=980.0000333'


// case 2
$num =3D new Number('1.111111');
$num2 =3D new Number('3');

$div =3D $num->div($num2, 3);
$div->getNumber(); // '0.370'
$div->getNumber(7); // =E2=80=980.3700000'
```

Since the scale can be inferred for everything other than div, a special ar= gument is given only for div.

Regards.

Saki

<= div>
--0000000000007b588d061519763b--