Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123037 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 894091A009C for ; Mon, 8 Apr 2024 10:23:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712571849; bh=dJklrB+b2FcJgnGgSOsNX6prCIgPD695rkqHnRYo7mg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=RgXKXDjr6VMI+XD3o0E8Sx5JWz3rv5ibLqiIvBrLXBkS3QtxuZlsWSC26R7VOGegf LHm5P/wRSTBoLZW7skbv/lWZxWbyJK0Lng2ZqpOXv827y4aUFMwfprQroEvt0EPjVl GSLNdy9mPcjWA6NElIIfHEaH6CFhCPBxmY7pFvlbqkkGpjvN+ZuDoFZl8eGKBIiacP u9nbK7l6c3wzOUPeOYxjiSbl9qsO3lncPeZ28bNRyIHCaoUCoapZnl1icPvFo25rHL JPdnoMC2C5FWGkJdPn0SAtikJu2Vt/v3B4cRO7ED8a3oxYO2MU4VJXmdaoSnD9yMDr u/KK2Gqp62EVQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DF19E18055E for ; Mon, 8 Apr 2024 10:24: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.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-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) (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 10:24:07 +0000 (UTC) Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-dde0b30ebe2so3571904276.0 for ; Mon, 08 Apr 2024 03:23:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=interi-co.20230601.gappssmtp.com; s=20230601; t=1712571815; x=1713176615; 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=a10rIvoIQQPD9kkz1ensSNql6k/aYRu7oUEbGSnPzIM=; b=TJ/mLmQVz/TY/2XdZ8FA8y6dNijvUNJYXJtZF7rsMriWA2CY0cqN9StQMQhyE3Bt6n Z60nuBvvJfvhticpIUyeWrorCzaEzuPB9CFw91as1uKlOni51HCbFVwPw/SR7AqT2SfK xrSf7opwmW6KJ/EgPhnaSeXsGZJMEf4DyWG9lWAJpCENEXx8WUt/h7uHq+Eb9YAmOrsV zuLJb3AxJ7Jvv7iKIYlbRYdxiQAQfkZR5MZvk/tlYPNCH32JprvtcCw3EkkKxPwS1iFe iy1qu2XhaL7x7P18MP6IypqXGMPPUXzQoU2sobUUgxl81XHhtldT+vh+xx+ZUFfnPOKA T37Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712571815; x=1713176615; 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=a10rIvoIQQPD9kkz1ensSNql6k/aYRu7oUEbGSnPzIM=; b=dcSUWZ02nT7/7yN0gS4EaQ699PEPoOgM4i6vBAPyPrpeaiTpnwSKTPisBaAy5uKCSX FzdI+pc7T6ruax05vAd46cWc/Iexcy/18UFe66IUXCMPmk0BiejsjeRp9AXMEQN2/vBD Bj3eN73vxXpzkJOM1r4drc5+T+V6/Dm2jPOsM3IqVgHzsvmTBCZ1B1ZcpGGEtCRPuRMZ MLc5B9Ju33s+zNBTEnAjkvO3/IPQXP3TaV9RExXDWhRp/jr03ue+4lPXC85/5hU/or+h skJc7AJ+/pk3hhex5TAbbEBJlItr0xx5qcwunlruOvrkHLkqiGUhcw0AFsUL2ZXBrBgP +exg== X-Gm-Message-State: AOJu0YxhaLCF2l/w053gLh9xbI1rOpLidDtxxTxqGIrRIhLBfvla/eeO bcG9G7zppNtt2mPY+hFVtGkV/TI0G7BVY4nmoVoA97AY0zKAmDH+VSQHYre8xOv6lcQhPcZRAKV aW5j1avoIoJ5TE+2V6ETGtsh7eqKJLVWspNOdmUQJMpR4uUetV3E= X-Google-Smtp-Source: AGHT+IHPYSAUt+bBmX6Z5r9KeILHKTsulysSADMpHiE83m6bfb0W2ETt4l7XrTtY436AzJYQgFJwQjiYjX7EXvHHNuw= X-Received: by 2002:a25:8211:0:b0:dc6:ff12:1a21 with SMTP id q17-20020a258211000000b00dc6ff121a21mr6528710ybk.31.1712571815068; Mon, 08 Apr 2024 03:23:35 -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, 8 Apr 2024 19:23:24 +0900 Message-ID: Subject: Re: [PHP-DEV] Proposal: Arbitrary precision native scalar type To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: alex.pravdin@interi.co (Alexander Pravdin) On Tue, Mar 19, 2024 at 5:35=E2=80=AFAM Rowan Tommins [IMSoP] wrote: > I think a reasonable number of people do share the sentiment that having = two separate modes was a mistake; and neither mode is actually perfect. It'= s not about "making it on by default", it's about coming up with a unified = behaviour that makes the setting redundant. This is off-topic, but just to conclude: as a user, I personally would love to see strict_types to be always enabled eventually. > I don't think choice is really what you want: if you were designing a lan= guage from scratch, I doubt you would say "let's give the user a choice of = what type 1 / 10 returns". What it's actually about is *backwards compatibi= lity*: what will happen to code that expects 1/10 to give a float, if it su= ddenly starts giving a decimal. > In short, the best way of avoiding declare() directives is not to replace= them with something else, but to choose a design where nobody feels the ne= ed for them. I understand your point and it is completely valid. But I'm speaking from a different angle. There are obviously two opposite areas of calculations, two "modes": "I don't care much about the performance, give me precise results" and "I don't care about rounding errors, give me the fastest results". I can't imagine a way of joining them and unifying the work with them until a completely new CPU architecture is invented. It makes me think "how can we make both types of users happy"? I would like to see native and intuitive support for both of the "modes". So "declare(default_decimal)" seems to be a good option. Users already know what is "declare" so it will be not a problem to start using another option. Again, I'm not trying to replace float calculations with decimals due to performance and backward compatibility. My goal is to invent a way to make two "modes" convenient without bandaids. > For most cases, I think the rule can be as simple as "decimal in means de= cimal out". What's maybe not as obvious at first sight is that that can app= ly to operators as functions, and already does: 100 / 10 gives int(10), but= 100.0 / 10 gives float(10.0), as do 100 / 10.0 and 100.0 / 10.0 I agree that if one of the operands is decimal then the result should be decimal. The "declare(default_decimal)" is suggested specifically for the cases when it is unclear what result we should return and to not introduce monstrous syntax with a lot of typecast or special syntax. > By the same logic, decimal(1) / 10 can produce decimal(0.1) instead of fl= oat(0.1), and we don't need any fancy directives. Even better if we can int= roduce a shorter syntax for decimal literals, so that it becomes 1_d / 10 With "declare(default_decimal)" there's no need to introduce new syntax for numbers. I personally would prefer to work with "normal" number literals and let PHP decide (under my guidance) how to represent them internally - as floats or as decimals. > Where things get more complicated is with *fixed-precision* decimals, whi= ch is what is generally wanted for something like money. What is the correc= t 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? As a user, I see it as we store a decimal number internally with the maximum number of fractional digits and let the user choose how to represent it only when converting it from decimal to something else. Only at this moment, some rounding will happen and we may give users the proper tools for this. When it comes to 1/3, we may hardcode some specific rounding mode when it exceeds the internal limit of fractional digits. In my view, this limit will be more than enough for 99.9% of so they will be able to apply their own rounding mode to 0 or 2 or 10 fractional digits with no issues. > If you stick to functions / methods, this is slightly less of an issue, b= ecause 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 pa= ckage. Yes, but the whole point of my suggestion is to go away from the object syntax :) My goal is not to make the future implementation simpler but to lift the PHP language to the next level even if will cost much more effort in total. And just to clarify. I'm not a big expert in decimal calculations, my proposal is still not so specific and I may have a lack of knowledge in some terms or details and may need help from more experienced people. I'm discussing possible ways of the implementation and I would like to help the community to develop some design that will make future development aligned with some principles and eventually achieve the final goal. I understand the complexity of the proposal so I'm not standing on the implementation as a whole by some PHP version. But I would be more than happy to know that the language is evolving in the defined direction. -- Best, Alexander