Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113088 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31180 invoked from network); 5 Feb 2021 11:14:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Feb 2021 11:14:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C073E1804D1 for ; Fri, 5 Feb 2021 02:58:49 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail.apserver.co.uk (server2.alteredperspective.co.uk [85.119.82.103]) by php-smtp4.php.net (Postfix) with ESMTP for ; Fri, 5 Feb 2021 02:58:49 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.apserver.co.uk (Postfix) with ESMTP id 886734F4064; Fri, 5 Feb 2021 10:57:54 +0000 (GMT) Received: from mail.apserver.co.uk ([127.0.0.1]) by localhost (server2.alteredperspective.co.uk [127.0.0.1]) (amavisd-new, port 10024) with LMTP id ypzW-vgjTUYp; Fri, 5 Feb 2021 10:57:52 +0000 (GMT) Received: from [192.168.0.7] (cpc113422-maid7-2-0-cust493.20-1.cable.virginm.net [86.18.185.238]) by mail.apserver.co.uk (Postfix) with ESMTPA id 7E2BF4F4061; Fri, 5 Feb 2021 10:57:52 +0000 (GMT) To: Benjamin Morel , "G. P. B." Cc: PHP internals References: Message-ID: <86b1f50b-2ac0-bc92-96b3-d79480262539@allenjb.me.uk> Date: Fri, 5 Feb 2021 10:58:46 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC] Warning for implicit float to int conversions From: php.lists@allenjb.me.uk (AllenJB) On 04/02/2021 17:54, Benjamin Morel wrote: > On Thu, 4 Feb 2021 at 17:05, G. P. B. wrote: > >> Greetings internal, >> >> I'm proposing a new RFC which would warn when an implicit conversion >> from float to int occurs. >> >> The draft is currently located on GitHub: >> https://github.com/Girgias/float-int-warning/ >> for ease of commenting/providing changes to it. >> >> The official discussion phase wouldn't start before I convert it to >> docwiki >> and >> post it on the wiki, something I'm planning to do next week. >> >> Any feedback is appreciated. >> >> Best regards, >> >> George P. Banyard >> > Hi George, > > Thank you for this proposal, I'm all for it, though I'd go one step > further, and actually issue a warning during explicit casting as well: > > (int) 3.5; // E_WARNING > (int) round(3.5); // OK > > In my experience, it's better to be explicit about your intent, so forcing > the user to round() before casting when the float has a fractional part is > OK to me. > This would help prevent weird silent conversions such as when you cast to > (int) from user input: > > $age = $_GET['age']; // '25.75' > $x = (int) $foo; // I'd expect a warning, not a silent conversion to 25 > > — Benjamin There are legitimate cases for explicitly casting floats to int. For example floor() outputs a float, but in the context of the domain I'm working I might know that the result is never going to exceed a certain value and want the result explicitly as an int. (And after checking the manual, I'd also note here that round() also returns a float, so how exactly does your example here work? Is it only OK to explictly cast a float that's the return value of a function? Or only explictly cast a float if the fractional part is .0? Is that viable given the "inaccuracy" of floats? Or would it be better for PHP to have some non-range/accuracy-sensitive representation for integers (and decimals) here?) (and now we're getting into "why are we still using floating point math by default in 2021" territory, so I'll stop right here) AllenJB PS. Apologies for the dupe to those who did receive my original send, but I forgot to amend my from address to my subscribed address and as I recall the list doesn't like list-only replies)