Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123038 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 E99371A009C for ; Mon, 8 Apr 2024 11:18:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712575148; bh=EzsgjR30nANRFh6N6nAL905pEa+N5dJ+wi9pKYpKiHs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=AVzh3DWCfifqOjJBcCt6YYeMsYk3QGbOfvcEgLArVcTBgqmmjcAliio2CVLZ4evwb AQd7PctuX8KMXJVbP10SX8TKLsC8kaFqkL7B+SywDjqhxfe923lRCdmwd99R9SizHq 0wOquWEa/D9jYnXbanSmGZMoUz6+LBZ9iFiJhnFQqwqt56L3/l+c7QBNJglQQlPM07 s+fbKYDk9WUjQuPhCFRWR/xQACvLDtaSXQunf4jJxmvfmj13p9Rho/gKSFFvAHWvv2 0o1OJTGvJ+89apZL8ZloAO3pItAWL3Wan9kO5bA+Whsxak3Mhm4k+oroiJoOZazmh+ bqkE9ihmyaKmg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 54B0E1801DC for ; Mon, 8 Apr 2024 11:19:07 +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_H2,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-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 ; Mon, 8 Apr 2024 11:19:04 +0000 (UTC) Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-343c2f5b50fso2879282f8f.2 for ; Mon, 08 Apr 2024 04:18:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712575111; x=1713179911; 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=EzsgjR30nANRFh6N6nAL905pEa+N5dJ+wi9pKYpKiHs=; b=NES3PjGXORJjYvnozwbALmnlgIfMRC2ECuVnochWspaUTAevZUCBTj67xGGdDM1qah DDCT+Tvk95coCIUdKeZusFL8SH18y1nqJjWoQKDq+AvzBGSEbz6yYwg7n+FjNYFF6kUP K29wPFNqbbfRkU4S5IMCbWEe1J4ncPGJgdyOBt/UzMTrt4SVOSf5xOW3tSdsiDqs9cC1 fJZNdJgoyTbGUiRMT7/oN9Iio7eYM5kkI81uwaEa4yazcbmHO++oHTyGUs4t2etZ9WMD efmojNi9PUz9nXmoBROTcL0Ma9C+mF8QJrrW6v56tKTOOOZG9E60bu/4uAvF0yGojYnF r8CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712575111; x=1713179911; 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=EzsgjR30nANRFh6N6nAL905pEa+N5dJ+wi9pKYpKiHs=; b=uTgD7NT4BHOrMXeT/cmH7tc/uryK8nqTsUlaSpQ2YPqKng9CmMcFyKcZ/tfJLJuERx /N1st26Etp0ggne7zxRhJUZ9LWVVRzM7McCoKSPYV3nVMWnkKeOh4TnDVu5ANlEw6Z8c 6KxPZGQNG241BBb/HhhqZEg/ifm/lTIFiVh2ODrKmjZ5A2f/2xKP5MjTsvh0q24ZHebB WGIAmMtlbVkk/1zUdm85sNiHkKMdHKJ8Im33DZ9T1R82epJJVehr2lNnEoXPlulKpDSp zy7N9Acnw9q8GDR73WGu/CdROSGtRq/QWVrrC2YezM2cDNA8xx0e50KTwivzJQEZd66u PWow== X-Forwarded-Encrypted: i=1; AJvYcCWqbbiPfA9UIN9p16Zf2hcape3m7G/c1adxDEruyCLO1lJeJufy2WLmhyyGrOnLYyAadh5FICYqY9p7AG54IKA2A3NMVTemQA== X-Gm-Message-State: AOJu0Yy9PahEQmkNwOkdIn2pgAYzhCDWeyPeh4wLiBP1Pn6bumMXfY4B cj7AxP1PHFYD1tBWuwULwveie/m55L3ooUMJ96sv8YEcdqoRyuFjLMqCGQU3x40XN8fTmIomW3q M/DU51bcrf1YSzyuNBLxGbxif5fnOfld82SA= X-Google-Smtp-Source: AGHT+IGXj/oKFY9sU0CwMPp9krUx5i0RZ3SUn6nP0JxPtTMo+RoWfVuH/mL8pzKeCxJotNV+2xFlJEPrPNd737qujKk= X-Received: by 2002:a5d:6105:0:b0:343:b942:326b with SMTP id v5-20020a5d6105000000b00343b942326bmr5630795wrt.18.1712575111296; Mon, 08 Apr 2024 04:18:31 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <40553F28-2EC2-475A-BD8E-1D6517AA2A51@rwec.co.uk> <2B518F62-B774-45C9-82A2-EF6653AAE34E@sakiot.com> In-Reply-To: Date: Mon, 8 Apr 2024 14:17:55 +0300 Message-ID: Subject: Re: [PHP-DEV] Native decimal scalar support and object types in BcMath - do we want both? To: Saki Takamachi Cc: "Rowan Tommins [IMSoP]" , php internals Content-Type: multipart/alternative; boundary="00000000000029003a061593f789" From: arvids.godjuks@gmail.com (Arvids Godjuks) --00000000000029003a061593f789 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello everyone, I've been following the discussion threads and forming my own opinion to share since I have done a bunch of financial stuff throughout my career: I did the integers only at the application level and DECIMAL(20,8) in the database due to handling Bitcoin, Litecoin, etc. My feeling on the discussion is that it got seriously sidetracked from the core tenet of what is actually needed from the PHP engine/core to discussing developing/upgrading a library that can handle money, scientific calculations, yada yada yada. Basically, in my view, the discussion has been catastrophically scope-creeped to a point where nobody could agree on anything and discuss things that were irrelevant to the initial scope. To me, the BCMath library stuff is just that - a BCMath library. It's a tool that can handle any size number. It's a specialized tool. And, frankly, for the vast majority of use cases, it's complete overkill. If we are talking about implementing a Decimal type into the language as a first-class citizen alongside int/float/etc, do we really need it to handle numbers outside 64-bit space? Ints have a size limit (64 bits), floats also have a defined range. Why not have decimal be represented as 2 64-bit ints at the engine level, and similarly to floating-point numbers, you can have a php.ini setting where you can define the precision you want? Floats have a default of 14 positions. Why not have a setting that defines precision for the decimal type, set it to the same 14 positions, and you can have a decimal type that has 114 bits for the integer part and 14 bits for the floating-point part? In the vast majority of cases, for all practical intents and purposes, that would be enough. For the rest - you have ext/decimal, BCMath, GMP extensions (and by all means, improve those as much as needed, make them as powerful as Python's math libs). This approach has some major benefits: if done right, it's just another type that is compatible with float, but does integer precision math, and having the precision of 14 in the vast majority of needs is basically overkill already. Ideally, you should be able to just replace your float and ints with decimal type hints and just do the roundings/formatting via the usual means of round/ceil/floor/number_format. Normal math just works. If any part of the expression has a decimal type, the result is a decimal number. The only sticking point I see is how to define a decimal type variable since we do not have var/let for/const; we can only define types on class properties and their constants. Do we add a function decimal(int|float $value): decimal? Or do we need to do prep work to be able to define variables with type? Another idea I have is to just do $decimal =3D (decimal)10.054 when instantiating a variable. Actually, that's not that uncommon to do it like that already when you want to ensure the result is of a certain type, PSL library does a lot of that and I do quite like it. Long story short, give people a tool that's simple and works, things like scale and all that stuff we can just handle in userland code ourselves because everyone has different needs, different scales, and so on. It's the same as right now with integers - if you require an integer bigger than 64 bits, you use GMP/BCMath/etc. You are also not going to have fun with databases and PDO because there are going to be some shenanigans there too. Basically, at that point, you are running against various other PHP engine limitations and when software has to be written with those considerations in mind anyway in literally any language to begin with. Some are easier than others. Sorry for it being a bit long, I'm happy to clarify/expand on any parts you have questions about. --=20 Arv=C4=ABds Godjuks +371 26 851 664 arvids.godjuks@gmail.com Telegram: @psihius https://t.me/psihius --00000000000029003a061593f789 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello everyone, I've been following the discussio= n threads and forming my own opinion to share since I have done a bunch of = financial stuff throughout my career: I did the integers only at the applic= ation level and DECIMAL(20,8) in the database due to handling Bitcoin, Lite= coin, etc.

My feeling on the discussion is that it got seriously sid= etracked from the core tenet of what is actually needed from the PHP engine= /core to discussing developing/upgrading a library that can handle money, s= cientific calculations, yada yada yada. Basically, in my view, the discussi= on has been catastrophically scope-creeped to a point where nobody could ag= ree on anything and discuss things that were irrelevant to the initial scop= e.

To me, the BCMath library stuff is just that - a BCMath library. = It's a tool that can handle any size number. It's a specialized too= l. And, frankly, for the vast majority of use cases, it's complete over= kill.

If we are talking about implementing a Decimal type into the l= anguage as a first-class citizen alongside int/float/etc, do we really need= it to handle numbers outside 64-bit space? Ints have a size limit (64 bits= ), floats also have a defined range. Why not have decimal be represented as= 2 64-bit ints at the engine level, and similarly to floating-point numbers= , you can have a php.ini setting where you can define the precision you wan= t? Floats have a default of 14 positions. Why not have a setting that defin= es precision for the decimal type, set it to the same 14 positions, and you= can have a decimal type that has 114 bits for the integer part and 14 bits= for the floating-point part? In the vast majority of cases, for all practi= cal intents and purposes, that would be enough. For the rest - you have ext= /decimal, BCMath, GMP extensions (and by all means, improve those as much a= s needed, make them as powerful as Python's math libs). This approach h= as some major benefits: if done right, it's just another type that is c= ompatible with float, but does integer precision math, and having the preci= sion of 14 in the vast majority of needs is basically overkill already. Ide= ally, you should be able to just replace your float and ints with decimal t= ype hints and just do the roundings/formatting via the usual means of round= /ceil/floor/number_format. Normal math just works. If any part of the expre= ssion has a decimal type, the result is a decimal number. The only sticking= point I see is how to define a decimal type variable since we do not have = var/let for/const; we can only define types on class properties and their c= onstants. Do we add a function decimal(int|float $value): decimal? Or do we= need to do prep work to be able to define variables with type? Another ide= a I have is to just do $decimal =3D (decimal)10.054 when instantiating a va= riable. Actually, that's not that uncommon to do it like that already w= hen you want to ensure the result is of a certain type, PSL library does a = lot of that and I do quite like it.

Long story short, give people a = tool that's simple and works, things like scale and all that stuff we c= an just handle in userland code ourselves because everyone has different ne= eds, different scales, and so on. It's the same as right now with integ= ers - if you require an integer bigger than 64 bits, you use GMP/BCMath/etc= . You are also not going to have fun with databases and PDO because there a= re going to be some shenanigans there too. Basically, at that point, you ar= e running against various other PHP engine limitations and when software ha= s to be written with those considerations in mind anyway in literally any l= anguage to begin with. Some are easier than others.

Sorry for it being a bit long, I'm happy to clarify/expand on any= parts you have questions about.
--

Arv=C4=ABds Godjuks
+371 26 851 664
Telegram: @psihius=C2=A0https://t.me/psihius
--00000000000029003a061593f789--