Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122676 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 5364F1AD8F6 for ; Mon, 18 Mar 2024 04:39:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1710736807; bh=zMVUSr8lYN9udPC7SarNNKKwA6Nbq/oOCWj64zqBbls=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Ed1j7j0eqdpZGBMikyDtbwqQIGyxL7tyOUNmns1QCN5Y16BnHVYhQhWiRIH/f65RK wJWEyLZeQfzGXgkmmpp/byqdGY9KGCu2W7/9EoE40TZ1qCj8K1TDk5bgk7LCbUcT2g QtjOPSgBDkJs4GgnSf5q2G8d0aHFa259xapGuEPD0miaT3EZX4igKcNRNrbk2vaY9Z TJ2EV2LJbKWMw/n/jefmPq7HWytF/TYV2gTdkM4I3/KQyc1AP1txLuEEKnsMh+D4kH lZ4uSHIYC+gTz4/3vuxijJSu7emG3c2EvVe8WBmjJQ1jckKYXdGG9huIaKkXB873Br uiCt9Np4YJtIg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7A731180072 for ; Mon, 18 Mar 2024 04:40:06 +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.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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, 18 Mar 2024 04:40:05 +0000 (UTC) Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-60a0599f6e5so35741767b3.3 for ; Sun, 17 Mar 2024 21:39:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=interi-co.20230601.gappssmtp.com; s=20230601; t=1710736785; x=1711341585; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=2fJv+rrO+rXKZQ9/Q/LDVZFUTjfCmd08bEEw2JLtfyA=; b=fl3/ZY/k+I/Yeq6MuwLoQZW4SFOo02qq5O0lXC0+eV89PAWnq9Vo1xMy+bjCVCLc92 +reu/NOJjkj4oi64UAe11OlkBmfEFn5uWDecP3PO5M5TS+zPoWUDwe53Di0VPx2eU9cz bvSibdkKRBWlTchLBOA2ko4vDVAuStzwgIkz1GeZLNxqlkDyanp8aCIUjoGZ+fDOJWM3 uaP3kJ8BwoiVawlGmfMyUO9H1jlVhYDLi+gDcQeHtvKZgueBoaeEYlVt1gvrpsbH5dCb LOZQxs1FzuzpVFLYAUThMs9WpwfimWNIdTSKyWhKUfn2phZL4bf7AYrpAhvQgMlX62k5 56bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710736785; x=1711341585; h=content-transfer-encoding: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=2fJv+rrO+rXKZQ9/Q/LDVZFUTjfCmd08bEEw2JLtfyA=; b=orPs54hbXF2AWCkyEG8C4u990d7dL3PJIL59JTs3cKXPr6CqvKQq2qhyqC+R+xS3Se VV1Bg7fpwsoFtTZs7lGgGKjZ42/xLMWwOepiu1jH0ItudsRx1HvKOjER/iBJvEdHYuI7 3XNRL2kjUTnBH/iXq75a0lAWB+9U9DQVdVkdRgs3yfQf2xAKSAkpZNHv5o+lrQob/BEH nlWK0Q2JoSy77GUa/tdZqzninbuFc26gFJaA3TPA1RUs2kTlVOCkbpBonMNqw7OCzT9B KRmnLbzGNu4u4YgON3WoDbUrcUEsppNWLG2q9A6bQVuU7GnGqCh40/AKBk3yNuwQQH3y 0dZA== X-Gm-Message-State: AOJu0YwBYOafwFSEDAXZQNGuhhrf2HrZiIY8ZZll5ByvL4wNq7V9MJvm 6mt1lAVm9KQXewIi0nxFCta4UhaMTCYJHxcaPZS4To7g6Z+BJTA7ByQDqm1BkjdKeAJzrDF2O8/ 0OXJJX4sUnNZKG99/etexDa8aPsWlfKRDQV5M9A== X-Google-Smtp-Source: AGHT+IEcAKzCVgEVDoQdF4IPI7eTZQkmpdwqWUG3jpvVZwNXO3dQ2ByQAYkV5X3FQFaQSjYLDKn/1WsenXHhP9Mi5No= X-Received: by 2002:a81:d514:0:b0:60c:bdb0:cd28 with SMTP id i20-20020a81d514000000b0060cbdb0cd28mr11805476ywj.6.1710736785391; Sun, 17 Mar 2024 21:39:45 -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: Mon, 18 Mar 2024 13:39:34 +0900 Message-ID: Subject: Re: [PHP-DEV] Proposal: Arbitrary precision native scalar type To: "G. P. B." Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: alex.pravdin@interi.co (Alexander Pravdin) Sorry for the so late reply, but I would like to continue the discussion on this topic. On Tue, Dec 12, 2023 at 10:04=E2=80=AFPM G. P. B. wrote: >> I didn't know that the strict types directive was a mistake. My intentio= n is to be able to write clean all-decimal units of code and not break the = backward compatibility. The old code should work as it was before. At the s= ame time, there are use cases when the whole class/project should be writte= n with decimals only. As a user, I want to do that without complex language= structures and excessive typehints or explicit conversions. The all-decima= l code should be as clean as it is now with floats. This is why I proposed= this directive. Can you suggest something better to achieve the same? > > > The issue is that I don't think having arbitrary precision decimals as a = core language feature is a necessity compared to rational types. > A cast from rational to float wouldn't produce a large round trip, wherea= s trying to figure out arbitrary precision is more difficult. > But in any case, having a declare/INI or whatever that changes the behavi= our of the engine/language is not a good design choice. How can we introduce the ability to write user code in default decimals and at the same time keep the old way of working as it was before, to not introduce any troubles into the existing code and not introduce performance issues? As a user, I would like to have a choice. When I need speed and I don't care about rounding errors, I will use default floats. When I need precise calculations and can sacrifice some performance, I will use default decimals. And I would like to be able to switch between these modes selectively, not app-wide. Just give me a choice and inform me in the documentation about the differences and pros/cons of both types. I'm not in the context of the core team plans regarding "strict types". Could you share some details here? What is the current plan regarding it? To make strict types on by default eventually? Or something else? >> If you can suggest a better way of working with fractional numbers - ple= ase do :) But IMO limiting the language by 64 bits is not a good way. It is= more easy to go over the limit than you may think :) Especially in some in= termediary values while performing complex calculations. I could be wrong, = but fractional numbers limited to 64 bits sound like a bandaid. The languag= e will tell users "hey, you can do something general, but if you want somet= hing else please don't use me at all or involve bandaids with extensions". = My key point is not only to allow working with decimals, but also to make a= ll the alternatives useless, cover their functionality by builtin tools, to= free users from thinking about workarounds and bandaids. From my POV, intr= oducing a new decimal type that does not cover the GMP/bcmath/ext-decimal f= unctionality is pointless and a waste of time. > > > Again, the use cases for arbitrary precision numbers seems rather limited= , and having this as an extension does not seem problematic at all. > My current issue is that there is no way to represent "small" numbers suc= h as 0.1 or 5.8 exactly. > Arbitrary precision is a totally different ballpark, be that for integers= and/or fractional values and makes everything slow, just look at Python wh= o is *finally* investing time and money to make the most common numbers (th= ose that fit in a machine word) not be abysmally slow. Honestly speaking, my current pain point is that I need to work with precise money calculations in business automation and accounting fields. I would like to see a good tool in the PHP language to do that. As an engineer, I understand that it should be fast enough. I suggested mpdecimal as a good enough option. If you could suggest something else that would fit the majority of user needs, I'm totally for it. >> How quickly you would be able to read something like this: gmp_add(gmp_m= ul(gmp_div(gmp_sub($value, $add), $value2, $factor, gmp_sin($value3), gmp_i= ntval($value))) ? >> >> This absence of intuitive readability makes the development and support = difficult and it's better to choose another language. This is what I want t= o change and make PHP powerful enough for this kind of applications. > > GMP supports operator overloading, so you do not need to write this, and = as far as I see, ext/decimal *also* supports operator overloading so you ca= n just write it using the normal arithmetic operations. > Moreover, it supports type casts so you can just do (int) $GMP instead of= gmp_intval($GMP); > And it also supports using the comparisons operatos on it instead of usin= g gmp_cmp() > > The only thing that is, currently, not possible to do is a cast _to_ GMP = using (GMP), but maybe that's something that could be added to the engine f= or internal objects, but I'm not sure about this. If GMP can be converted to a scalar builtin language type, that also will be totally fine from my point of view. > It seems, your main motivation to have this as a language feature is to h= ave access to operator overloading and casting behaviour, that internal obj= ects already support. My points are the following: - Work with scalar numeric types when I work with numbers, not with objects or strings mimicking numbers. I want (bool)Decimal(0) to be equal to false, not to true because non-null objects are always true. - Have it as a built-in language feature allowing anyone to use it without requiring extension installation. - Keep backward compatibility for the code that already works with floats while introducing an option to use only the new type when performing math operations on numbers. And being able to have two versions of the code working in the same app. - IDEs and static analyzers sometimes go crazy when I do math operations or comparisons on objects. > Which diminish the cost/benefit of making the engine changes, especially = if the default behaviour doesn't change and one needs to maintain effective= ly two different versions of PHP depending on if it is in "decimal" or "flo= ating" mode, which raises a plethora of questions just in its own. As a developer, I don't see an issue with having two fractional numeric types in the language. My old code with floats is working as usual and doesn't break. If I want to introduce precise calculations for money/accounting/whatever using decimals, I change my code and I'm fully responsible for its maintenance. PHP has warnings to inform developers if they do something wrong and this is what can be used to prevent errors in the code. The only problem here that I foresee is the division on two ints (and some other operations I think) should return some specific type (float or decimal) and the language needs a mechanism to determine the intention and return the proper result type. This is why I suggested using "declare". In other cases, if at least one of the operands is decimal we can always return decimals.