Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:124322
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 1EB681A00B7
	for <internals@lists.php.net>; Tue,  9 Jul 2024 20:27:51 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail;
	t=1720556956; bh=t0K/rvnuK6/9TiX5f6hSowM860xocCjzsZ7TfKy4g+w=;
	h=References:In-Reply-To:From:Date:Subject:To:Cc:From;
	b=HhYGs+sdWHkoxydDQkVl5tz06iWZSC+BF5c84SCOWqwjVpQPgYNijCO32oZ3iJQQU
	 Ll9OsNkemth94sAn7tErkMabgVo2YQKOMjG9/KGQPPriQihrFhH1ROcoHiB6prSdjm
	 fdinbzTF4tWSjJm31uADIhXzxawgg7SKq0xDNyD97TG8+EYGFWS+VsdCqDwqsabV5o
	 NR0cVh/3tFeUR9qrjuW/kBHZ2e31S6+3jXFDwStt8GwkRInmk5w3/FhzwQ4Dmpt6F3
	 NhD2TJ3T4XC1VuVK2kg91rv/+a3VYaphPnDmADFx5uJRQc+zw5vIDB2XEMz6YUBG7n
	 MaRoLauiGZ2Nw==
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id A3C08180079
	for <internals@lists.php.net>; Tue,  9 Jul 2024 20:29:15 +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 autolearn=no autolearn_force=no version=4.0.0
X-Spam-Virus: No
X-Envelope-From: <jordan.ledoux@gmail.com>
Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.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 <internals@lists.php.net>; Tue,  9 Jul 2024 20:29:15 +0000 (UTC)
Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-70af4868d3dso3552624b3a.3
        for <internals@lists.php.net>; Tue, 09 Jul 2024 13:27:49 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20230601; t=1720556869; x=1721161669; 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=t0K/rvnuK6/9TiX5f6hSowM860xocCjzsZ7TfKy4g+w=;
        b=emGPAL1Pq2Grh/0gV28y21AmEbOPXZ7BpbM05m3/CVux/G+sraQw3vaFeuWOpY0Nps
         TvPghvHt1Fl6TquZ2sg5h0Xa1SJsK8tOahP7QrWZkJoXw9LuBiT1n8GlFvWkIsCy/PMy
         11qXPkXlM4bHlwrAjkkn/b3aQ+fo9qafHmH7Bhg9fsoykztHUzECWVfNbzxBHUvEeK8B
         P81mz8fBs93yQJFmk0J7JhxT9oCnkELKozYRqebnVczxT2J9oYuddmBoxURJSCzoeVZ0
         miTthj1RSBBSupz0nKbDuxcmk/cqLYbc8oRhJVhCPhj28gJKqY/NZk8cNaXaFYg+1L8H
         wFzw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20230601; t=1720556869; x=1721161669;
        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=t0K/rvnuK6/9TiX5f6hSowM860xocCjzsZ7TfKy4g+w=;
        b=d6pYSYNb9RKsGuA3in4EnHDRnef3fGVOxFa3h+R0kKoy60fOyx+SNaKeuwOj6GgRXE
         BsiF3uuuQhs9xC54KFvrmUCS1/3ka/RatoLykrnX6ttH6MPTnTk6ecfc2FvopCc4odMq
         cqHgEKKnoJiFs66PhOe6LPHSJgQZwGCkMqCFarXFFN/rY8IP77KdWt7psszsxVPA9QPV
         JtfbKrmpPzi5lVzZMskra5A3s/sJxNP8dJw1iyHjxFVdDIiklvB4TR996mvlQjeCPIjS
         hwQEKUOyMh+WNQq8hnA2ES2aIUxyJpINrI5N/OR3LYx0XpVE5gxXqLTPZR/wQFl56YCG
         bguw==
X-Forwarded-Encrypted: i=1; AJvYcCXz4UZjHKBcuwjf54CN/3i7aBs9l/soOVRgUuJnIA+1Z4jFOQqkWONFVbIV/WG/ISX6GDXhNwRRIcj+n7pa1TjJBCe/tXoJDw==
X-Gm-Message-State: AOJu0YwRtDXE/S02l+TaPWlwjwS+JCYQos0AEPw7X912nqXMHkVdmo5a
	N2M6QVTxmdTF0uruwFe93CTj44owrNKpyNJ+cYPXAfCHLxvSlHc0lbKtPsxJJjyYwyCqfhiOLV9
	S/XrFY5zvc65ysN9PQnQ6YQOXmuTFRQ==
X-Google-Smtp-Source: AGHT+IFW+nijlqu/ZbfQpmgMiTOO+MX9nhwcqirrqQNHzQOXct0kdrcIeL+BVwcs+nFV3XIXcHeV/+xLGjHlFc+JgmE=
X-Received: by 2002:a05:6a20:3d8a:b0:1c2:8b26:1bb6 with SMTP id
 adf61e73a8af0-1c29822d0c1mr3585362637.17.1720556868452; Tue, 09 Jul 2024
 13:27:48 -0700 (PDT)
Precedence: bulk
list-help: <mailto:internals+help@lists.php.net
list-unsubscribe: <mailto:internals+unsubscribe@lists.php.net>
list-post: <mailto:internals@lists.php.net>
List-Id: internals.lists.php.net
MIME-Version: 1.0
References: <668B4C78.2000808@adviesenzo.nl> <8FA0DE3B-2AF4-4110-9836-14E035DC8169@sakiot.com>
 <CAMrTa2GZqDTTLQDvD-gf9V20+cg_ANoHT97ad+KRvvzgYG+v8w@mail.gmail.com> <2ac2914f-1672-4f7a-863b-86b6692bff60@bastelstu.be>
In-Reply-To: <2ac2914f-1672-4f7a-863b-86b6692bff60@bastelstu.be>
Date: Tue, 9 Jul 2024 13:27:35 -0700
Message-ID: <CAMrTa2GT-komjCePzJ0cV8OJVhYCrX-3yCMJH9DXyZNYAxk39A@mail.gmail.com>
Subject: Re: [PHP-DEV] [RFC] [Discussion] Allow int type argument to BCMath function
To: =?UTF-8?Q?Tim_D=C3=BCsterhus?= <tim@bastelstu.be>
Cc: Saki Takamachi <saki@sakiot.com>, 
	Juliette Reinders Folmer <php-internals_nospam@adviesenzo.nl>, internals@lists.php.net
Content-Type: multipart/alternative; boundary="000000000000f5c8d9061cd65c47"
From: jordan.ledoux@gmail.com (Jordan LeDoux)

--000000000000f5c8d9061cd65c47
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Tue, Jul 9, 2024 at 10:47=E2=80=AFAM Tim D=C3=BCsterhus <tim@bastelstu.b=
e> wrote:

> Hi
>
> On 7/8/24 11:36, Jordan LeDoux wrote:
> > I suspected the same thing when I was doing my arbitrary precision
> library,
> > but I actually have yet to find a test case in all my unit tests where
> this
> > happens when converting to a string. You can see this here:
> >
> > https://3v4l.org/Ud8Cn
>
> PHP emits the shortest possible representation that roundtrips, so the
> precision loss indeed does not happen when converting from float, but
> rather when converting to float / when the implicit rounding happens
> during the calculation.
>
> Nevertheless the inherent rounding error of floats and an arbitrary
> precision maths library do not mix: Users should not be encouraged to
> mindlessly pass a float, but rather work with strings all the time or
> explicitly perform the necessary conversion as appropriate for their use
> case - which of course is easier with strict_types enabled because then
> the conversion would need to be made explicit with a cast.
>
> Best regards
> Tim D=C3=BCsterhus
>

Yes, absolutely agree that arbitrary precision math should not be done with
floats, and developers should not be encouraged to use floats. However for
the specific case of type-coercion into the argument of a `bcadd` or
similar call, the detailed string conversion issues do not happen, which is
all I was saying.

Integers ALSO have a maximum precision, detailed by the `PHP_INT_MAX`
constant, which varies by system and install (mainly on whether 64-bit
integers are available).

My point was not that floats or integers are sufficient, but that the entry
point into an arbitrary precision calculation often comes from numbers that
are stored as ints and floats. If we accept ints, and don't ask the
developer to explicitly cast to string, I think we should do the same for
floats. Both or neither. I can see the argument against accepting either, I
can see the argument for accepting either.

I do not understand the argument for accepting one but not the other.

Jordan

--000000000000f5c8d9061cd65c47
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote">=
<div dir=3D"ltr" class=3D"gmail_attr">On Tue, Jul 9, 2024 at 10:47=E2=80=AF=
AM Tim D=C3=BCsterhus &lt;<a href=3D"mailto:tim@bastelstu.be">tim@bastelstu=
.be</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"marg=
in:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1e=
x">Hi<br>
<br>
On 7/8/24 11:36, Jordan LeDoux wrote:<br>
&gt; I suspected the same thing when I was doing my arbitrary precision lib=
rary,<br>
&gt; but I actually have yet to find a test case in all my unit tests where=
 this<br>
&gt; happens when converting to a string. You can see this here:<br>
&gt; <br>
&gt; <a href=3D"https://3v4l.org/Ud8Cn" rel=3D"noreferrer" target=3D"_blank=
">https://3v4l.org/Ud8Cn</a><br>
<br>
PHP emits the shortest possible representation that roundtrips, so the <br>
precision loss indeed does not happen when converting from float, but <br>
rather when converting to float / when the implicit rounding happens <br>
during the calculation.<br>
<br>
Nevertheless the inherent rounding error of floats and an arbitrary <br>
precision maths library do not mix: Users should not be encouraged to <br>
mindlessly pass a float, but rather work with strings all the time or <br>
explicitly perform the necessary conversion as appropriate for their use <b=
r>
case - which of course is easier with strict_types enabled because then <br=
>
the conversion would need to be made explicit with a cast.<br>
<br>
Best regards<br>
Tim D=C3=BCsterhus<br></blockquote><div><br></div><div>Yes, absolutely agre=
e that arbitrary precision math should not be done with floats, and develop=
ers should not be encouraged to use floats. However for the specific case o=
f type-coercion into the argument of a `bcadd` or similar call, the detaile=
d string conversion issues do not happen, which is all I was saying. <br></=
div><div><br></div><div>Integers ALSO have a maximum precision, detailed by=
 the `PHP_INT_MAX` constant, which varies by system and install (mainly on =
whether 64-bit integers are available).</div><div><br></div><div>My point w=
as not that floats or integers are sufficient, but that the entry point int=
o an arbitrary precision calculation often comes from numbers that are stor=
ed as ints and floats. If we accept ints, and don&#39;t ask the developer t=
o explicitly cast to string, I think we should do the same for floats. Both=
 or neither. I can see the argument against accepting either, I can see the=
 argument for accepting either.</div><div><br></div><div>I do not understan=
d the argument for accepting one but not the other.</div><div><br></div><di=
v>Jordan<br></div></div></div>

--000000000000f5c8d9061cd65c47--