Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121945 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 82183 invoked from network); 7 Dec 2023 14:36:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Dec 2023 14:36:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 435AB180003 for ; Thu, 7 Dec 2023 06:36:57 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,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 autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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, 7 Dec 2023 06:36:56 -0800 (PST) Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1d05e4a94c3so8466735ad.1 for ; Thu, 07 Dec 2023 06:36:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701959803; x=1702564603; 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=eE/vC4rmA62zdrpvmTtadQLF/XuYtWIcupSXCEzK/d8=; b=JKKM9dmXUjRHsdB+20XMT01jsiX4XqSplr+KI8BF7FFQKGeKEVcdaTvBY6GnAyeP22 NL4rHjNplN8Nou96QF3ceb/39A+UPqhhR34dVtwEpQ22oFd22R8DRbhFCUALUdjKDyQs XUn62cq0QGo8Im+7CONG+FUk20r47UkFGc/0QMSMUfR4NNJpVJYQMQeIEUkYNd7K4RFA jP2a698OcTyltF5XDbrhoY84CAmi4yC0RdSV5CeWXnvCZ9iP0f93CTZmhwCgzNHLcAUp TTE0wz184oueIUlrGy1zhTMPYXab108ON4V7iM0vZJkHUKMRfjn7zTdejOO0vStBotZ7 OZcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701959803; x=1702564603; 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=eE/vC4rmA62zdrpvmTtadQLF/XuYtWIcupSXCEzK/d8=; b=ukqCs0NvLNRGFFC2yVmuqqDxjDLmUWath3V4oUZ8bZ2OB2v13J2xlihQXmIIWib3rT COLE8f6C6OTzHT6/a0+C4p+Ewi5FdX/UI5tQdf90AtAWqITAlS3I2GdkHLXiUoqS3SDA T/0r35QmYnWfWR50uQMIhM6MsOivube8tG93GcsrzDRGfNov1LRnKWzsI3EDIPsHy/36 OAZIHwX1IaFW45SimsKU4E+eF7IGLX/L7aQOPCQfbJ+PbvTHjjUkfaP+jOSx68GYKxsW ubQKcDY1Qf1bG7P4lVnOOqiUUARyuxOz2glz5bl39b8Nnh5noBdRltlCO7VhFT2Yi7HE En+g== X-Gm-Message-State: AOJu0Yy/1+a9vYI6cCeWOMp7hXOP4MXSqfa1tm0MGHCIldXWCuMCHSFP ZVcWvTglRflnWNhXQ7Fq+Q1JqavWupOOHuOWHrYIRRgotjw= X-Google-Smtp-Source: AGHT+IEgZQbKu8l1vrSx2v8bkzqD2JTGa7wCnNFffhs3OcujQnqIGqQyBCWLGn10ixvL8ztnGfv2O+jK+2femBmFvUU= X-Received: by 2002:a17:90b:1dc6:b0:286:6cc1:780a with SMTP id pd6-20020a17090b1dc600b002866cc1780amr2576626pjb.77.1701959802598; Thu, 07 Dec 2023 06:36:42 -0800 (PST) MIME-Version: 1.0 References: <18c42fdbb30.2831.17a3710df6d58f02ca570cc47e197a63@interi.co> In-Reply-To: <18c42fdbb30.2831.17a3710df6d58f02ca570cc47e197a63@interi.co> Date: Thu, 7 Dec 2023 14:36:31 +0000 Message-ID: To: Alex Pravdin Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000074bb50060bec651e" Subject: Re: [PHP-DEV] Proposal: Arbitrary precision native scalar type From: george.banyard@gmail.com ("G. P. B.") --00000000000074bb50060bec651e Content-Type: text/plain; charset="UTF-8" On Thu, 7 Dec 2023 at 06:36, Alex Pravdin wrote: > Hello internals, > [...] > GMP: > - Workaround: implements the GMP class that allows basic math operations. > - Requires using separate functions for the rest of operations. > > - Objects are always casted to true, GMP(0) will equal to true. > This is incorrect, GMP object do _not_ support casts to bool See https://3v4l.org/LHpD1 > It works the same as "float" in terms of its usage and type casting except > for one thing. Float value can be passed to a decimal argument or > typecasted with a warning like "Float to decimal conversion may incur > unexpected results". > > Decimal to float conversion is allowed and smooth: > > function f (float $value) {} > > f(0.2); > > f(0.2d); // allowed with no warnings > I disagree with this part, floats can not represent decimal values properly e.g. $f1 = 0.1; $f2 = 0.2; $f3 = 0.3; var_dump($f1 + $f2 === $f3); will return false. As such, floats are not at all compatible with decimals. Moreover, the whole point of adding a warning when implicit conversions from int to float happen was to be able to warn before elevating the warning to a TypeError. Therefore, introducing behaviour that warns instead of throwing a TypeError is already a no-go from my PoV. > [...] > > Literal numbers in the code are converted to floats by default. If > prepended by the "(decimal)" typecast, the decimal result is produced > without an intermediary float. > This behaviour is rather suboptimal, I'd rather have literals be decimals, as decimals to floats should always be a reasonable implicit coercion considering we already do int to float. > New declare directive "default_decimal" is added. When used, literals and > math operations return decimal by default instead of float. This is to > simplify creating source files working with decimals only. > Please no, no new declare directives that affect engine behaviour. Strict types was already a mistake because of this IMHO. > New language construct "as_decimal()" is added to produce decimal math > results for literals and math operations instead of float without > intermediary float: > > $var = 5 / 2; // returns float 2.5 > $var = as_decimal(5 / 2); // returns decimal 2.5 > > This is a kind of "default_decimal" for a specific operation. > Again, this should return a decimal instead IMHO. > If mixed float and decimal operands are used in a math operation, decimal > is converted to float by default. If "default_decimal" directive or > "as_decimal()" construct is used, float is converted to decimal (with a > warning): > > $f = (float) 0.2; > $d = (decimal) 0.2; > > $r = $f + $d; // returns float result by default > $r = as_decimal($f + $d); // returns decimal result with a warning about > implicit float to decimal conversion > > All builtin functions that currently accept float also accept decimal. So > users don't need to care about separate function sets, and PHP developers > don't need to maintain separate sets of functions. If such functions get > the decimal parameter, they return decimal. If they have more than one > float parameter and mixed float and decimal passed, decimals converted to > float by default. If "default_decimal" or "as_decimal" used, float is > converted to decimal with the warning. > Messing with the return value type has already proved controversial. And as I said previously, I am dead against having implicit float to decimal conversions. Making the functions type generic is something that I am fine, however. > The new type uses libmpdec internally to perform decimal calculations > (same > as Python). > I really don't think we need arbitrary precision decimals. I'm also not convinced using a floating point spec is the most sensible, due to the rounding errors this is going to introduce. The non-arbitrary IEEE 754-2008 specification cannot describe the decimal 123457.1467 exactly, which is frankly pointless. Decimals, or more broadly rational numbers that, outside numerical computations, that are going to be used are not going to need huge denominators. I've been thinking about this for a while, and I personally think that rational numbers are just better than trying to support only decimals. And considering we have 64 bits to play with for a new zval type splitting the bits into a 32-bit unsigned integer for the numerator and an 32-bit signed integer for the denominator should provide us with enough reasonable rational numbers. As any number that do not fit in this structure seems to be something relegated to the world of numerical computation where floats are what are mostly used anyway. > All of the points above are subject to discussions, it is not an RFC > candidate right now. So please share your opinions. > > I know that the implementation of this will require a lot of work. But I > don't think this is a stopper from formulating the requirements. > Sometimes, > any project requires big changes to move forward. I'm pretty sure this > functionality will move PHP to the next level and expand its area of > applications. My thoughts here are mostly from the user's perspective, I'm > not so familiar with PHP internal implementation. But I think this feature > can be a good goal for PHP 9. > Yes, this is a major project, and as said above I have also thought about adding a rational number type. But the main hurdle for this is changing the zval structure and applying the change everywhere to the engine and extensions. Best regards, Gina P. Banyard --00000000000074bb50060bec651e--