Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96565 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81298 invoked from network); 23 Oct 2016 19:18:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Oct 2016 19:18:33 -0000 Authentication-Results: pb1.pair.com header.from=dev@mabe.berlin; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=dev@mabe.berlin; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mabe.berlin from 80.237.132.167 cause and error) X-PHP-List-Original-Sender: dev@mabe.berlin X-Host-Fingerprint: 80.237.132.167 wp160.webpack.hosteurope.de Received: from [80.237.132.167] ([80.237.132.167:46168] helo=wp160.webpack.hosteurope.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EE/62-28528-80D0D085 for ; Sun, 23 Oct 2016 15:18:32 -0400 Received: from dslb-094-223-150-185.094.223.pools.vodafone-ip.de ([94.223.150.185] helo=[192.168.178.53]); authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1byOHs-00039L-Ro; Sun, 23 Oct 2016 21:18:28 +0200 To: internals@lists.php.net References: <2cab4219-5128-7493-c604-5772534743ea@mabe.berlin> Message-ID: <89761933-b0db-09f8-7861-dc6a1f836745@mabe.berlin> Date: Sun, 23 Oct 2016 21:18:28 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: <2cab4219-5128-7493-c604-5772534743ea@mabe.berlin> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-bounce-key: webpack.hosteurope.de;dev@mabe.berlin;1477250312;5f711bb2; Subject: Re: [PHP-DEV] strtod and NaN vs. zero From: dev@mabe.berlin (Marc Bennewitz) Am 23.10.2016 um 21:13 schrieb Marc Bennewitz: > > > Am 23.10.2016 um 13:49 schrieb Niklas Keller: >> 2016-10-23 12:55 GMT+02:00 Marc Bennewitz > >: >> >> Hi internals, >> >> On casting a non numeric value to a float in PHP the result will be >> float(0). >> >> In PHP-7.0 an exception was introduced that on casting a string >> "/\s*NaN\s*/i" will result in float(NaN). >> >> https://3v4l.org/2Xakm >> >> Wouldn't it be more logical and expected to return NaN in all cases >> of casting a non numeric string to a floating point number? >> >> Thanks >> Marc >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> >> +1 > > I just noticed that the behavior change in PHP-7 has been "fixed" > already by https://bugs.php.net/bug.php?id=73329. > > So the old behavior will be presented soon. > > But I'm still curious why casting any non numeric string results in the > valid number float(0) where there is a special value in floating point > numbers declared to represent not a number values. > > I also did a small research on other languages and found the following: > > C strtod returns float(0) > -> but supports converting the strings "INFINITY" or "NAN" > (case-insensitive) > > JavaScript returns NaN I forgot to mention that in JavaScript (parseFloat) it is nearly the behavior I would expect. The only difference is that it allows the string +/- "Infinity" to be casted to Infinity. > > Python throws ValueError: could not convert string to float > -> but supports the string +/- "NaN", "Inf", "Infinity" > (case-insensitive) > > Perl returns float(0) > -> but supports the string +/- "NaN", "Inf", "Infinity" > (case-insensitive) > > So there is a nice mix of how other languages will handle this problem. > > > In my opinion it still makes much more sense to return NaN in any case a > not numeric string will be casted to float incl. "Inf" and "Infinity". > (If spaces should be ignored or suffixes allowed is another question) > > = The beginning of a string must contain at least one number by ignoring > spaces to be casted to a valid floating point number. In any other > situations NaN must be returned because this represents what it is = Not > a Number. > > So for me the following example table makes sense: > > In -> Out > "" -> NaN > " " -> NaN > "+" -> NaN > "-" -> NaN > "." -> NaN > ".5" -> 0.5 > "0.5" -> 0.5 > " 0.5" -> 0.5 > "0" -> 0 > " 0" -> 0 > "Nan" -> NaN > "Inf" -> NaN > "Info" -> NaN > "foo" -> NaN > ".foo" -> NaN > ".2foo" -> 0.2 > " .2foo" -> 0.2 > "foo1" -> NaN > "foo." -> NaN > "foo0.1" -> NaN > > > Thoughts > Marc > >> >> Regards, Niklas >