[prev in list] [next in list] [prev in thread] [next in thread]
List: php-internals
Subject: Re: [PHP-DEV] strtod and NaN vs. zero
From: 2.124.144.170
Date: 2016-10-25 15:21:37
Message-ID: 0C.13.24108.B987F085 () pb1 ! pair ! com
[Download RAW message or body]
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/
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic