Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128565 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 lists.php.net (Postfix) with ESMTPS id 341EE1A00BC for ; Tue, 26 Aug 2025 18:33:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1756233111; bh=maNSqxZVldQLUtNKd/tAy8SZP+NEp/X3sOTLNq88w/g=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=mOXogufR/xhoYSdVWH6PdK1cIVCK1VVxLmuX/VAexihGn8L+rKAKqKCSVeTkIsnIc 7kubHe7YR6sZYsaG5rzbonMgdbptSDEt3GP3WUpcoKo1atgL6m43CRH5qQ2OdM09HE Xpxb4zBmqm/CsZeUZWLXvsyOL2AsoG3u/mViicowgi6CKiMWlWF34beVqlUAdW1EM9 1uOU6VdJgaX33YKqEjVIgVmGgVBv2L2RT+7G1QNHd71tYi/0r/yFZ8CENN56z9PR57 BU26sChwaW79NWBs/FSF2ZRp9oxEZxq91dQN5cwU5NTGeyYNjrva5CENVwrIg8DSzD KjbuRrPAWOD6g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4D1A118005D for ; Tue, 26 Aug 2025 18:31:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fout-a5-smtp.messagingengine.com (fout-a5-smtp.messagingengine.com [103.168.172.148]) (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 ; Tue, 26 Aug 2025 18:31:50 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id EC3FEEC0358; Tue, 26 Aug 2025 14:33:21 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-05.internal (MEProxy); Tue, 26 Aug 2025 14:33:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1756233201; x= 1756319601; bh=15ajP/PscoLzCdqpSGR5V90cFkfq9xv7IsKKc14loVg=; b=u ypmEYEq0a5oltQsc1jU4yqQ05psfHUzecawBCglj7jpBqHPYPVLQnedCNTR1Xwmy MdtpVVsYXPWuJCidSlS8Q2DdqyeKtW7x3Us4wBz1tc6DLYmG3N1LS7twVsRIp33T cughOSFZN4gPyFtNbGFHi+V7/rgkCZE6dx230BpDWKctOUE/rHW+5gny36BYA6Sc 65y4Tw6LVy/iAZ09HkBo0UFksz5mIJrcg27mqTRkiI/hFoJCRTXMxp+EnRfvzhZb hMNq0IXZlpXtMLOJIp/JJhKS+lgLy5cRDD9mWzLxAjUSVCkHlk7lRpuj5YhKBzeR jIv82O0gTOxF0v4IDqFig== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1756233201; x=1756319601; bh=15ajP/PscoLzCdqpSGR5V90cFkfq9xv7IsK Kc14loVg=; b=coWvEh2trMIUh7DyGWtWG/NFUebb3CB2BVcBTqJ0aQCUE6UgRBm PA8f3VHSZb67aPk117hU3Ykv0NYfeuMplYAp1EOGhWp8HmWRqHPw67mA6hKUAxhG O4/MtQEthYx+2HgzIoZi91XvxHcMvUcq39SLNeqo5zQhF7wqZCCvXx+zjkxi7u8x 0ooLClAucS+nM8wsu0ip4YvO1dglWDRoZXXz9wEWB4th9y5x65ZkdP/irc4Td3oF DmNiu3BoN9h3dP0CtQR/wR2HnTI74HEHCBmZsoC14aIhOk8PgEM8P6wzBRl8/En+ A2nTnLzbj1Lwx3GtMcHvxiAStCWaW0WgFBw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgddujeehleelucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepofggfffhvfevkfgjfhfutgesrgdtreerredtjeenucfhrhhomhepfdftohgsucfn rghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtffrrghtth gvrhhnpeeiueethedvvdefjefhgfeiheelheehtdfhfeekjefflefgvedvkeduteejjedt tdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehroh gssegsohhtthhlvggurdgtohguvghspdhnsggprhgtphhtthhopeefpdhmohguvgepshhm thhpohhuthdprhgtphhtthhopehtihhmsegsrghsthgvlhhsthhurdgsvgdprhgtphhtth hopegrlhgvgidruggruhgsohhishdophhhphesghhmrghilhdrtghomhdprhgtphhtthho pehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 937F71820074; Tue, 26 Aug 2025 14:33:21 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: AJ2Gy4vr0DuR Date: Tue, 26 Aug 2025 20:33:00 +0200 To: =?UTF-8?Q?Tim_D=C3=BCsterhus?= , "Alexandre Daubois" Cc: "PHP internals list" Message-ID: <78af4148-101f-4ced-90a7-51a32fe33d14@app.fastmail.com> In-Reply-To: <499395ff-4ac9-4dc1-86fd-697b205467ba@bastelstu.be> References: <499395ff-4ac9-4dc1-86fd-697b205467ba@bastelstu.be> Subject: Re: [PHP-DEV] [RFC] Add "is_representable_as_float()" and "is_representable_as_int()" functions Content-Type: multipart/alternative; boundary=30356179f7304b06935c14d2f969f534 From: rob@bottled.codes ("Rob Landers") --30356179f7304b06935c14d2f969f534 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Tue, Aug 26, 2025, at 20:23, Tim D=C3=BCsterhus wrote: > Hi >=20 > On 8/26/25 19:14, Alexandre Daubois wrote: > >> 3.14 is not exactly representable as a IEEE-754 double-precision > >> floating point number. The two nearest representable values are > >> 3.14000000000000012434 (this one is the nearest) and > >> 3.13999999999999968026. Returning `true` for > >> `is_representable_as_float("3.14")` is therefore wrong to me. > >=20 > > You're right about mathematical precision. Perhaps the function name > > is misleading. What the function actually should check is whether a > > string > float > string roundtrip preserves the value as PHP > > developers would expect it, not whether it's mathematically exactly > > representable in IEEE-754. > >=20 > > Maybe a more accurate name would better reflect this behavior. Naming > > is super hard again here. The goal is pragmatic: "will this value > > survive a type conversion cycle without surprising changes?" >=20 > "Surprising changes" is not a meaningful term. Keep in mind that the=20 > behavior of the function will need to be accurately documented and tha= t=20 > folks need something to work with to determine whether a report is val= id=20 > or not when bugs in the function get reported - which will inevitably=20 > happen. >=20 > In fact to use one of the examples for the RFC: 1e10 does not roundtri= p. >=20 > php > var_dump((string)(float)"1e10"); > string(11) "10000000000" >=20 > "1e10" clearly is different than "10000000000". >=20 > Generally speaking, I'm also not sure if =E2=80=9Cprinting raw floats=E2= =80=9D is a=20 > use-case we should encourage. Instead almost any application will need=20 > to format a number for a specific use-case. Formatting numbers for hum= an=20 > consumption [1] has different requirements compared to formatting=20 > numbers for programmatic consumption. Indeed. In some parts of the world, 1.000,01 is interpreted as 1000.01 i= n computers. That format also doesn=E2=80=99t roundtrip but is a valid n= umeric string, depending on locale. =E2=80=94 Rob --30356179f7304b06935c14d2f969f534 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable


On Tue, Aug 26, 2025, at 20:23, Tim D=C3=BCsterhus wro= te:
Hi

On 8/26/25 19:14, Alexandre Daubois wrote:
&g= t;> 3.14 is not exactly representable as a IEEE-754 double-precision<= /div>
>> floating point number. The two nearest representable = values are
>> 3.14000000000000012434 (this one is the ne= arest) and
>> 3.13999999999999968026. Returning `true` f= or
>> `is_representable_as_float("3.14")` is therefore w= rong to me.
> You're right about mathe= matical precision. Perhaps the function name
> is misleadin= g. What the function actually should check is whether a
> s= tring > float > string roundtrip preserves the value as PHP
<= div>> developers would expect it, not whether it's mathematically exa= ctly
> representable in IEEE-754.
> Maybe a more accurate name would better reflect this behavior= . Naming
> is super hard again here. The goal is pragmatic:= "will this value
> survive a type conversion cycle without= surprising changes?"

"Surprising changes" is n= ot a meaningful term. Keep in mind that the 
behavior of = the function will need to be accurately documented and that 
<= div>folks need something to work with to determine whether a report is v= alid 
or not when bugs in the function get reported - whi= ch will inevitably 
happen.

In f= act to use one of the examples for the RFC: 1e10 does not roundtrip.

     php > var_dump((string= )(float)"1e10");
     string(11) "10000000= 000"

"1e10" clearly is different than "10000000= 000".

Generally speaking, I'm also not sure if = =E2=80=9Cprinting raw floats=E2=80=9D is a 
use-case we s= hould encourage. Instead almost any application will need 
to format a number for a specific use-case. Formatting numbers for hum= an 
consumption [1] has different requirements compared t= o formatting 
numbers for programmatic consumption.
=

Indeed. In some parts of the world, 1.0= 00,01 is interpreted as 1000.01 in computers. That format also doesn=E2=80= =99t roundtrip but is a valid numeric string, depending on locale.

=E2=80=94 Rob
--30356179f7304b06935c14d2f969f534--