Re: strtod and NaN vs. zero
| From: | Andrea Faulds | Date: | Tue, 25 Oct 2016 15:21:37 +0000 |
| Subject: | Re: strtod and NaN vs. zero | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-96609@lists.php.net to get a copy of this message | ||
Hi,
Marc Bennewitz wrote:
It's totally true that on casting non numeric string to integer will result in 0 but that's a different type even if it's also a numeric type. From my understanding PHP tries to cast from one type to another by loosing as less information as possible. In this case I see a non numeric string that gets casted to a float will loose exactly this information that it's "Not a Number" where it would be possible to keep this information. On casting this to int there is of course no possibility to keep it but I don't see where it's inconsistent as it's a different type used for different use-cases.(int) and (float) are lossy and have always been so. If you want PHP to complain when you give it something non-numeric, rather than coerce to 0, there are alternatives.
Having mathematical operations with non numeric strings should result in the same behavior as NaN will result in NaN which makes sense and I don't see where it will throw lots on WARNINGS or TypeErrors as it's still a float.Because previously it would be an integer. "foo" + "bar" currently gives an integer 0. Now it would give you a float NaN. That has potential knock-on effects. Thanks. -- Andrea Faulds https://ajf.me/