Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122919 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 681CC1A009C for ; Thu, 4 Apr 2024 01:30:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712194240; bh=9Ep0+G+xhdc0Jwpy+tfLzjwOV/YnJUAuLjlNvMu9Ss0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=fKLzQUU8aHigg4fhuyF/gNy9SyRkcrNsCBH/Dg4oQO9pMz4bTNuJgDM+9k5d07mvi Fzc5HjDpTSBkO+kel6ZQQN0O8v65sd+6hqcacKgGD0DtOWhtG8wzd+eWPwtZbt3U/X 6knvgnNeon9I7xa14aUi/wvkUhiRvVhvht0Ux78pTsj3FtIpz+7spMwRiy4T6rGNSx CfcZOQfHF2Hq0lxeDUxx1o+dRT5dZhhFF1LxF29/Bm+tabOgCb8g/DATtlAnjDC7W2 lrbL8oWj5Rj2tZFBMD+p5Kd1KhsFE4ho4ejR9GoPaXLAbIln2xStTxMMYEj8fIiYCC jVdsaj2sU/i8w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D8F25180676 for ; Thu, 4 Apr 2024 01:30:38 +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-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.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 ; Thu, 4 Apr 2024 01:30:38 +0000 (UTC) Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6ecd957f949so438873b3a.0 for ; Wed, 03 Apr 2024 18:30:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712194208; x=1712799008; 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=MZTwsDb9M5fVHaJifVwKkmLWcOFitlkPiTRojvJ2woE=; b=U2i+yR+qMTqa0V54026oPQoBZ/LH005emHw+iRpF3EEdUECQuWB3KbrEgsyUc9JaLL dKXIfI+YHtJ3XKF8ZWCyitONUBsobzRrvzaL1R2YBLnMQrOVjvjQxNxZ6fZB/OqxlGB3 8LCN/8I40mHHsAlvUfpINHNMjvO7UATtXkTOGcM2HulLNW7XAE9MlR4ZVjCuKfLR2sbD H+6Krj84BIsWia0x/APYwhou2zJanJuf05k3N6O+mSe0bJxI/PgMA1xkUxpOiUHpHC8K dGZJp4HACUBp6MZ5VVEfGfXMaYwAOGwtcFKdC0lkWDn/+WphiNfbB+4HJKppcvDBJ/qU gmYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712194208; x=1712799008; 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=MZTwsDb9M5fVHaJifVwKkmLWcOFitlkPiTRojvJ2woE=; b=bRfLEwE9sfg6p0jo6hkaVVfBp1MJ3ZyAvnGYdxtnMO1ZcFXbhP1Ad+2wpp1JJL3HkV 58Vm/IIunSV9BqSaJOy5kpUgHCXM+ym5CCWRrvmZ6g9JMnMQZe4pxz2vD6fA5FEu05p4 srU2mOCx9+oH6svrzE/KSUvw4Bq7JY5EW63r+uzUu+CgdoEJ1a1LSPLVyKA9tzM9wky8 pSCuZoChUbnWCn0gMsvqkC0XBNoBu1ZnuzFbNSRrS2N1vXxLEawJ++E3tkGWMsF2dvjU fWaIy5eF7fVwpNZBAL77NnTDjpTzfy5TmgyKYhKOA2ijQXGqBa6tX1NW6+xiEVP2eEsB p2NQ== X-Gm-Message-State: AOJu0YwDupLREt2yJZBl5dzlT4iZwKKN1NfUAYsiOsyRVxbJDuW/ySiH zl3cEoOpUf89scGAHUhvY3npegYhP5dS2lx+9s0IS6dR+/u3ugBWMNTPPk6o/sUu1LZ2BEzrd9S d4F1cL3er6vYNi0JDJt+RWtPYBud21GrK X-Google-Smtp-Source: AGHT+IEaRVVr0p7ZsAf7AgKpn2KGOsD37wUEEw0899C+zpaeee333zl02byFfFvktOLgEPO7C1fD3f156ELU0EWeDR4= X-Received: by 2002:a05:6a20:8f26:b0:1a5:6a94:b487 with SMTP id b38-20020a056a208f2600b001a56a94b487mr1594376pzk.42.1712194208278; Wed, 03 Apr 2024 18:30:08 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <18c42fdbb30.2831.17a3710df6d58f02ca570cc47e197a63@interi.co> In-Reply-To: Date: Wed, 3 Apr 2024 18:29:55 -0700 Message-ID: Subject: Re: [PHP-DEV] Proposal: Arbitrary precision native scalar type To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000009232f806153b4774" From: jordan.ledoux@gmail.com (Jordan LeDoux) --0000000000009232f806153b4774 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 18, 2024 at 1:35=E2=80=AFPM Rowan Tommins [IMSoP] wrote: > > Where things get more complicated is with *fixed-precision* decimals, > which is what is generally wanted for something like money. What is the > correct result of decimal(1.03, precision: 2) / 2 - decimal(0.515, 3)? > decimal(0.51, 2)? decimal (0.52, 2)? an error? And what about decimal(10)= / > 3? > > If you stick to functions / methods, this is slightly less of an issue, > because you can have decimal(1.03, 2)->dividedBy(2, RoundingMode::DOWN) = =3D=3D > decimal(0.51, 2); or decimal(1.03, 2)->split(2) =3D=3D [ decimal(0.52, 2)= , > decimal(0.51, 2) ] Example names taken directly from the brick/money > package. > > At that point, backwards compatibility is less of an issue as well: make > the new functions convenient to use, but distinct from the existing ones > I came back to this discussion after my last reply on the BCMath as an object thread, because we went through a very similar discussion there with regard to operators. I think that, roughly, the fixed precision should be defined on the scalar itself: 1.234_d3 1.234 is the value, _d makes it a decimal type, and 3 shows that this value has a precision of 3. (Actually, it has a SCALE of 3, since precision is significant digits, and also includes the integer portion). But when it comes to fixed-precision values, it should follow rules very similar to those we discussed in the BCMath thread: - Addition and subtraction should return a value that is the largest scale/precision of any operands in the calculation. - Division and multiplication should return a value that is the sum of the scale/precision of any operands + 2 or a default (perhaps configurable) value if the sum is small, to ensure that rounding occurs correctly. Near zero, floats have about 12-ish decimal digits of accuracy, and will return their full accuracy for example. - Pow is extremely difficult to work with if you allow a decimal in the exponent. The libraries we have discussed previously handle this difficulty, but the precision needed often depends on what the calculation is for, so I'm not entirely sure what the best way to handle this with operators would be. Minimally, probably the same as division and multiplication. But if we have scalar decimal types, we need something like that _d12 to denote a decimal scalar with a scale/precision of 12. Jordan --0000000000009232f806153b4774 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Mon, Mar 18, 2024 at 1:35=E2=80=AF= PM Rowan Tommins [IMSoP] <imsop.= php@rwec.co.uk> wrote:
=20 =20 =20

Where things get more complicated is with *fixed-precision* decimals, which is what is generally wanted for something like money. What is the correct result of decimal(1.03, precision: 2) / 2 - decimal(0.515, 3)? decimal(0.51, 2)? decimal (0.52, 2)? an error? And what about decimal(10) / 3?

If you stick to functions / methods, this is slightly less of an issue, because you can have decimal(1.03, 2)->dividedBy(2, RoundingMode::DOWN) =3D=3D decimal(0.51, 2); or decimal(1.03, 2)->split(2) =3D=3D [ decimal(0.52, 2), decimal(0.51, 2) ] Example names taken directly from the brick/money package.=C2=A0

At that point, backwards compatibility is less of an issue as well: make the new functions convenient to use, but distinct from the existing ones

I came back to this disc= ussion after my last reply on the BCMath as an object thread, because we we= nt through a very similar discussion there with regard to operators.

I think that, roughly, the fixed precision should b= e defined on the scalar itself:

1.234_d3

1.234 is the value, _d makes it a decimal type, and 3 shows= that this value has a precision of 3. (Actually, it has a SCALE of 3, sinc= e precision is significant digits, and also includes the integer portion). = But when it comes to fixed-precision values, it should follow rules very si= milar to those we discussed in the BCMath thread:

= - Addition and subtraction should return a value that is the largest scale/= precision of any operands in the calculation.
- Division and mult= iplication should return a value that is the sum of the scale/precision of = any operands + 2 or a default (perhaps configurable) value if the sum is sm= all, to ensure that rounding occurs correctly. Near zero, floats have about= 12-ish decimal digits of accuracy, and will return their full accuracy for= example.
- Pow is extremely difficult to work with if you al= low a decimal in the exponent. The libraries we have discussed previously h= andle this difficulty, but the precision needed often depends on what the c= alculation is for, so I'm not entirely sure what the best way to handle= this with operators would be. Minimally, probably the same as division and= multiplication.

But if we have scalar decimal typ= es, we need something like that _d12 to denote a decimal scalar with a scal= e/precision of 12.

Jordan
--0000000000009232f806153b4774--