|
Golbez posted:Can we all agree that, when comparing two items of the same type, that there should never be any coercion performed when comparing them? Apparently not. code:
|
# ? Sep 5, 2012 03:32 |
|
|
# ? May 17, 2024 10:06 |
|
So my dad emailed me a question about why he couldn't install Joomla and apparently there is a line in the file like this: code:
He was pretty confused how something could be changed like that between versions... I just told him that PHP was built pretty badly. P.S. T_PAAMAYIM_NEKUDOTAYIM is the worst error message possible.
|
# ? Sep 5, 2012 05:45 |
|
Strong Sauce posted:P.S. T_PAAMAYIM_NEKUDOTAYIM is the worst error message possible. 90% of my job is PHP.
|
# ? Sep 5, 2012 06:09 |
|
zergstain posted:
This won't even work, because sizeof is a compile-time operator, not a function.
|
# ? Sep 5, 2012 08:15 |
|
code:
|
# ? Sep 5, 2012 08:35 |
|
I could almost blame the second one on somebody who's too used to Java, if it was by itself.
|
# ? Sep 5, 2012 09:22 |
|
Somebody who is too used to Java and also doesn't know what size a char is in C
|
# ? Sep 5, 2012 10:24 |
|
JewKiller 3000 posted:This won't even work, because sizeof is a compile-time operator, not a function. How does that make it not work?
|
# ? Sep 5, 2012 11:07 |
|
code:
|
# ? Sep 5, 2012 11:22 |
|
Aleksei Vasiliev posted:Somebody who is too used to Java and also doesn't know what size a char is in C It's 1! How can anyone not know that?
|
# ? Sep 5, 2012 11:55 |
|
MarsMattel posted:
So it might throw a compile-time error, but it's far from guaranteed.
|
# ? Sep 5, 2012 12:29 |
|
Golbez posted:Can we all agree that, when comparing two items of the same type, that there should never be any coercion performed when comparing them? Nope! I mean, you just need to know the language, and then it's a simple matter of not writing poo poo code. If this trips you up you just didn't know the language well enough. Suspicious Dish posted:
Do you mean it fails as is (and if so why?), or that wanting that not to fail was the rationale for comparing strings as numbers originally?
|
# ? Sep 5, 2012 13:43 |
|
Golbez posted:Can we all agree that, when comparing two items of the same type, that there should never be any coercion performed when comparing them? Clearly they have different types, one is "exists x in string. hextoint(x)" and the other is "exists x in string. decimaltoint(x)".
|
# ? Sep 5, 2012 14:11 |
|
E: Never mind. Still a lovely codebase, tho. Space Kablooey fucked around with this message at 15:11 on Sep 5, 2012 |
# ? Sep 5, 2012 14:55 |
|
nm, was wrong, AND sorry for double post
Golbez fucked around with this message at 15:05 on Sep 5, 2012 |
# ? Sep 5, 2012 15:02 |
|
Suspicious Dish posted:Apparently not. It will? var_dump("1" == "1.0") returns true. So what do you mean by fail?
|
# ? Sep 5, 2012 15:05 |
|
Golbez posted:It will? var_dump("1" == "1.0") returns true. So what do you mean by fail? Yeah I just tested that with this: PHP code:
|
# ? Sep 5, 2012 15:19 |
|
What I meant was that they didn't want to fix that "bug" of comparing strings as numbers, or else that comparison would fail, and that's the rationale. Given PHP's roots as a hypertext thingymajig, I can believe that. It's horrible, but I think it at least has some premise other than idiocy surrounding it. I'm just bad at expressing myself.
|
# ? Sep 5, 2012 15:20 |
|
Suspicious Dish posted:What I meant was that they didn't want to fix that "bug" of comparing strings as numbers, or else that comparison would fail, and that's the rationale. Given PHP's roots as a hypertext thingymajig, I can believe that. It's horrible, but I think it at least has some premise other than idiocy surrounding it. I know, and since PHP was designed primarily as a web language, and all form input is a string... It just seems, well, appalling that (string)$x === (string)$y could return differently than (string)$x == (string)$y. The moral of the story is, always cast form input to the appropriate type before comparing.
|
# ? Sep 5, 2012 15:29 |
|
Golbez posted:Maybe "0.00" should evaluate to false, as should ".00" and any number of zeros before and after the decimal. It definitely should, and I'm really surprised it doesn't. I'll have to watch out for that. This is the actually the first example I've seen of PHP type coercion doing something unintuitive.
|
# ? Sep 5, 2012 15:31 |
|
PT6A posted:So it might throw a compile-time error, but it's far from guaranteed. Especially since it looks like dynamic stack arrays are valid C99. I remembered it being a GCC extension, didn't know about that. Edit: Or at least I remember it working in GCC. It was probably compiling under C99 rules at the time. Haven't been writing C for *that* long. zergstain fucked around with this message at 15:58 on Sep 5, 2012 |
# ? Sep 5, 2012 15:52 |
|
Suspicious Dish posted:Given PHP's roots as a hypertext thingymajig, I can believe that. It's horrible, but I think it at least has some premise other than idiocy surrounding it. That would make sense. Still makes me wonder why anyone thought it was a good idea to begin with (though that can be said about a lot of things in PHP; I'm looking at you left-associative ternary operator).
|
# ? Sep 5, 2012 15:52 |
|
Tiny Bug Child posted:It definitely should, and I'm really surprised it doesn't. I'll have to watch out for that. This is the actually the first example I've seen of PHP type coercion doing something unintuitive. The string "Nancy Biggins" is coerced into an IEEE754 NaN iirc
|
# ? Sep 5, 2012 16:05 |
|
PHP has a page specifically for type comparisons. Even has a lovely chart http://php.net/manual/en/types.comparisons.php And speaking of NaN. According to that page, you apparently can't do comparisons against NAN in PHP. So something like sqrt(-1), which returns NAN, cannot be compared to NAN. Meaning sqrt(-1) == NAN returns false, even though clearly evaluating sqrt(-1) returns NAN. I'm not sure of the reasoning for that. So the "safest" way to get a proper check on NAN seems to be, "is_nan((double)sqrt(-1))" because NAN is of type double. You could coerce the result of sqrt(-1) into a string you can evaluate the string "NAN", but that could lead to problems later when you try putting it into is_nan, since it throws a warning if you try to input in a string, which is a really strange warning to throw in a is_* function. No other is_* functions throw warnings when you put in a string. And of course, since NAN is type double, and anything besides zero is true when its a number, echo (NAN ? "t":"f"); returns true. I don't know what NAN should return boolean wise, tbh. I usually defend PHP because of the ease of getting poo poo up and running but stuff like this is just mind-boggling that I'm glad I usually run into these kind of problems while I'm working in PHP (which at this moment is 0 projects). Strong Sauce fucked around with this message at 17:02 on Sep 5, 2012 |
# ? Sep 5, 2012 16:59 |
|
That's floating point semantics for NaN. NaNs have the property that NaN != NaN. Blame the IEEE if you must, but that is where PHP is actually following a standard.
tef fucked around with this message at 17:22 on Sep 5, 2012 |
# ? Sep 5, 2012 17:16 |
|
Strong Sauce posted:So something like sqrt(-1), which returns NAN, cannot be compared to NAN. code:
|
# ? Sep 5, 2012 17:19 |
|
Strong Sauce posted:And speaking of NaN. According to that page, you apparently can't do comparisons against NAN in PHP. So something like sqrt(-1), which returns NAN, cannot be compared to NAN. Meaning sqrt(-1) == NAN returns false, even though clearly evaluating sqrt(-1) returns NAN. I'm not sure of the reasoning for that. This is actually a property of IEEE floats, not of PHP; any language that uses IEEE floats does this. Try it in Python or Lua and you'll get the same answers. The rationale, presumably, is that NaN is, well, not a number; it signifies the result of a failed operation (either one with no defined answer, or one that it can't express the answer of) and thus comparisons against it are meaningless. (Otherwise we end up with situations like sqrt(-1) == 0/0) Runtimes that care about this can use "signaling NaNs" to raise exceptions or coerce them to complex numbers or the like, but most languages just use "quiet NaNs" that behave as described above and are generally uninteresting.
|
# ? Sep 5, 2012 17:20 |
|
I have egg on my face... because of PHP
|
# ? Sep 5, 2012 17:30 |
|
tef posted:
In most languages, this would be the only case where $a != $a. But this is PHP. I'm sort of curious now, does PHP have any other edge cases like this?
|
# ? Sep 5, 2012 17:51 |
|
I never knew PHP had a special NAN value!Suspicious Dish posted:In most languages, this would be the only case where $a != $a. But this is PHP. I'm sort of curious now, does PHP have any other edge cases like this? SQL has the special case that NULL != NULL.
|
# ? Sep 5, 2012 17:55 |
|
Suspicious Dish posted:In most languages, this would be the only case where $a != $a. But this is PHP. I'm sort of curious now, does PHP have any other edge cases like this? php:<?php $xml = simplexml_load_string('<foo/>'); var_dump($xml == false); // true var_dump($xml == 0); // true var_dump($xml); // SimpleXMLElement $xml = simplexml_load_string('<foo>bar</foo>'); var_dump($xml == false); // false var_dump($xml == 0); // true var_dump($xml); // SimpleXMLElement
|
# ? Sep 5, 2012 18:05 |
|
DaTroof posted:
That violates transitivity, but that's old news. The question was about cases that violate reflexivity.
|
# ? Sep 5, 2012 18:17 |
|
Hammerite posted:That violates transitivity, but that's old news. The question was about cases that violate reflexivity. Fair enough. I can't think of anything that doesn't involve coercion abuse.
|
# ? Sep 5, 2012 18:37 |
|
I'm just curious, is there anything in PHP that actually violates symmetry?
|
# ? Sep 5, 2012 18:42 |
|
I saw this out of the corner of my eye on a classmate's text editor:code:
|
# ? Sep 5, 2012 21:59 |
|
Golbez posted:I never knew PHP had a special NAN value! Every language that deals with floating point has it. I was kinda astounded last time I saw a senior developer say that the following function didn't do anything and was pointless: bool isNaN(double x){return x!=x;}
|
# ? Sep 6, 2012 01:09 |
|
If the language already had an isNaN function it certainly would be pointless to write your own.
|
# ? Sep 6, 2012 01:14 |
|
hobbesmaster posted:Every language that deals with floating point has it. ^^ true. This is the kind of thing where high level developers don't know about some the very basic things afforded to them through a language and its associated library. See people using -999999 instead of INT_MIN because it was "small enough."
|
# ? Sep 6, 2012 01:17 |
|
pokeyman posted:If the language already had an isNaN function it certainly would be pointless to write your own. The function itself is shorter than the preprocessor directives to figure out if you have std::isnan or are using visual studio or whatever. In short, C++ is the coding horror.
|
# ? Sep 6, 2012 01:25 |
|
|
# ? May 17, 2024 10:06 |
|
Which is pretty much why Boost exists.
|
# ? Sep 6, 2012 01:41 |