Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

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:
if ($_GET['a'] == $_GET['b'])
    echo "butts";
will fail for &a=1&b=1.0, which is the rationale for the behavior in the first place.

Adbot
ADBOT LOVES YOU

Strong Sauce
Jul 2, 2003

You know I am not really your father.





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:
JError::$legacy = true;
After some Google searching and finding out he was using PHP 4.47 (the horror), apparently the scope resolution operator in PHP4 does not allow you to access static variables in a class so that line of code will only work in PHP5+.

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.

Tad Naff
Jul 8, 2004

I told you you'd be sorry buying an emoticon, but no, you were hung over. Well look at you now. It's not catching on at all!
:backtowork:

Strong Sauce posted:

P.S. T_PAAMAYIM_NEKUDOTAYIM is the worst error message possible.
It's a funny little joke that everyone should appreciate and not get upset about, and certainly should never be fixed, even after a multitude of non-Hebrew-speaking people have posted bewildered questions about it on every PHP board from here to infinity.

90% of my job is PHP. :smith:

JewKiller 3000
Nov 28, 2006

by Lowtax

zergstain posted:

code:
#define sizeof(x) rand()

This won't even work, because sizeof is a compile-time operator, not a function. :colbert:

bucketmouse
Aug 16, 2004

we con-trol the ho-ri-zon-tal
we con-trol the verrr-ti-cal
code:

#define uint8 char
#define uint16 char

:stare:

UraniumAnchor
May 21, 2006

Not a walrus.
I could almost blame the second one on somebody who's too used to Java, if it was by itself.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
Somebody who is too used to Java and also doesn't know what size a char is in C

shrughes
Oct 11, 2008

(call/cc call/cc)

JewKiller 3000 posted:

This won't even work, because sizeof is a compile-time operator, not a function. :colbert:

How does that make it not work?

MarsMattel
May 25, 2001

God, I've heard about those cults Ted. People dressing up in black and saying Our Lord's going to come back and save us all.
code:
char buttes[sizeof(MyStruct)];

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

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?

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane

MarsMattel posted:

code:
char buttes[sizeof(MyStruct)];

So it might throw a compile-time error, but it's far from guaranteed.

ToxicFrog
Apr 26, 2008


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. :pseudo:


Suspicious Dish posted:

code:
if ($_GET['a'] == $_GET['b'])
    echo "butts";
will fail for &a=1&b=1.0, which is the rationale for the behavior in the first place.

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?

Zombywuf
Mar 29, 2008

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)".

Space Kablooey
May 6, 2009


Can we all agree that the code that mysteriously needs recompiling over a dozen or so files, and then the bug that you were hunting for vanishes, is the worst code?

E: Never mind. :v: Still a lovely codebase, tho.

Space Kablooey fucked around with this message at 15:11 on Sep 5, 2012

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
nm, was wrong, AND sorry for double post

Golbez fucked around with this message at 15:05 on Sep 5, 2012

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Suspicious Dish posted:

Apparently not.

code:
if ($_GET['a'] == $_GET['b'])
    echo "butts";
will fail for &a=1&b=1.0, which is the rationale for the behavior in the first place.

It will? var_dump("1" == "1.0") returns true. So what do you mean by fail?

Optimus Prime Ribs
Jul 25, 2007

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:
<?php
var_dump($_GET);
var_dump($_GET["a"] == $_GET["b"]);
?>
and apparently it works fine.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
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.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

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'm just bad at expressing myself.

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.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

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.

zergstain
Dec 15, 2005

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

Optimus Prime Ribs
Jul 25, 2007

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).

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

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

Strong Sauce
Jul 2, 2003

You know I am not really your father.





PHP has a page specifically for type comparisons. Even has a lovely chart :v:

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

tef
May 30, 2004

-> some l-system crap ->
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

tef
May 30, 2004

-> some l-system crap ->

Strong Sauce posted:

So something like sqrt(-1), which returns NAN, cannot be compared to NAN.

code:
$ php -a
Interactive shell

php > $a=sqrt(-1);
php > if ($a != $a) { echo "NaN";}
NaN
php > 

ToxicFrog
Apr 26, 2008


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.

Strong Sauce
Jul 2, 2003

You know I am not really your father.





I have egg on my face... because of PHP :cripes:

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

tef posted:

code:
$ php -a
Interactive shell

php > $a=sqrt(-1);
php > if ($a != $a) { echo "NaN";}
NaN
php > 

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?

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
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.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

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

:negative:

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

DaTroof posted:

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

:negative:

That violates transitivity, but that's old news. The question was about cases that violate reflexivity.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

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.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

I'm just curious, is there anything in PHP that actually violates symmetry?

super_chair
Mar 13, 2012
I saw this out of the corner of my eye on a classmate's text editor:

code:
#include <stdio.h>

<?php

...
This was a database management class.

hobbesmaster
Jan 28, 2008

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;}

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
If the language already had an isNaN function it certainly would be pointless to write your own.

FamDav
Mar 29, 2008

hobbesmaster posted:

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;}

^^ 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."

hobbesmaster
Jan 28, 2008

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.

Adbot
ADBOT LOVES YOU

MrMoo
Sep 14, 2000

Which is pretty much why Boost exists.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply