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
nielsm
Jun 1, 2009



php:
<?
function is_equal_equal($val1, $val2) {
    if (($val1 == $val2) == ($val1 === $val2)) {
        return true;
    } else {
        return false;
    }
}
?>

Adbot
ADBOT LOVES YOU

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
No, see, if you were going down that route, you'd test that ($val1 == $val2) == ($val2 == $val1), in case equality isn't symmetric.

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane
gently caress PHP. That is all.

Pollyanna
Mar 5, 2005

Milk's on them.


php:
<?
function is_equal_equal_equal($val1, $val2) {
    if ((($val1 == $val2) == ($val1 === $val2)) ==== (($val1 == $val2) == ($val1 === $val2))) {
        return true;
    } else {
        return false;
    }
}
?>

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer
PHP code:
function is_equal_equal_equal($val1, $val2) {
    if ($val1 == $val2)===========================))-)********  {
        return true;
    } else {
        return false;
    }
}

VikingofRock
Aug 24, 2008




TooMuchAbstraction posted:

No, see, if you were going down that route, you'd test that ($val1 == $val2) == ($val2 == $val1), in case equality isn't symmetric.

PHP's == is symmetric though, right? :ohdear:

Linear Zoetrope
Nov 28, 2011

A hero must cook
You know, sometimes I think I get too cute with commit messages. I just looked at one of my old repositories and my description of a bugfix (I was accidentally checking if an atomic bool was false instead of true) is "not is not not not".

OddObserver
Apr 3, 2009

VikingofRock posted:

PHP's == is symmetric though, right? :ohdear:

Symmetric equality or IEEE floating point math, pick one.

Linear Zoetrope
Nov 28, 2011

A hero must cook

OddObserver posted:

Symmetric equality or IEEE floating point math, pick one.

IEEE floating point equality is symmetric. a == b <=> b == a. It's also transitive, because a == b & b == c => a == c. What it's not is reflexive (because of NaN). IEEE floating point is weird and insane for many reasons, but the reason equality is dodgy with it is because expressions that should be equal at infinite precision end up not being equal, not because it's not symmetric.

Linear Zoetrope fucked around with this message at 17:59 on Jul 15, 2016

spiritual bypass
Feb 19, 2008

Grimey Drawer

VikingofRock posted:

PHP's == is symmetric though, right? :ohdear:

Like most things in PHP, it works exactly the way you'd expect. The trouble with PHP, especially since the introduction of namespaces, is the things people do with it rather than the language itself. PHP is easy to use, so it tends to attract people who need something that's easy to use.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
No, PHP's == absolutely does not work in exactly the way I'd expect.

VikingofRock
Aug 24, 2008




Jsor posted:

IEEE floating point equality is symmetric. a == b <=> b == a. It's also transitive, because a == b & b == c => a == c. What it's not is reflexive (because of NaN). IEEE floating point is weird and insane for many reasons, but the reason equality is dodgy with it is because expressions that should be equal at infinite precision end up not being equal, not because it's not symmetric.

Along these lines, I thought PHP's == was symmetric but not transitive (because a and b can both be false-y but not == to each other).

ToxicSlurpee
Nov 5, 2003

-=SEND HELP=-


Pillbug
PHP is bad and stupid. Use literally anything else if you can.

Absurd Alhazred
Mar 27, 2010

by Athanatos
You should never use == with floats, anyway. You need something that allows you to set a precision.

Linear Zoetrope
Nov 28, 2011

A hero must cook

Absurd Alhazred posted:

You should never use == with floats, anyway. You need something that allows you to set a precision.

That's not true, sometimes == with floats is very useful. In functions with no randomness, the same input will still yield the same output. It's only dodgy if you're relying on different inputs to yield a bit-identical output in a floating point calculation. These details also matter with the distinction between >= and >.

xzzy
Mar 5, 2009

Absurd Alhazred posted:

You should never use == with floats, anyway. You need something that allows you to set a precision.

sprintf, best function ever.

OddObserver
Apr 3, 2009

Jsor posted:

IEEE floating point equality is symmetric. a == b <=> b == a. It's also transitive, because a == b & b == c => a == c. What it's not is reflexive (because of NaN). IEEE floating point is weird and insane for many reasons, but the reason equality is dodgy with it is because expressions that should be equal at infinite precision end up not being equal, not because it's not symmetric.

Doh. Got my symmetry and reflexivity reversed.... Well, it's not the most embarrassing thing in /this/ tread, at least.

fishmech
Jul 16, 2006

by VideoGames
Salad Prong

Plorkyeran posted:

No, PHP's == absolutely does not work in exactly the way I'd expect.

Sure it does, it works bizarrely just like you'd expect something in PHP to work. :v:

VikingofRock
Aug 24, 2008




Absurd Alhazred posted:

You should never use == with floats, anyway. You need something that allows you to set a precision.

What about for special casing to avoid dividing by zero?

Absurd Alhazred
Mar 27, 2010

by Athanatos

VikingofRock posted:

What about for special casing to avoid dividing by zero?

It seems like that might be and allowable exception, but in situations where you will be dividing by a number that could be zero, you probably also don't want it to be too small, either.

canis minor
May 4, 2011

Floating point comparison is unpredictable in PHP (or at least version 5.3), irregardless if it's using ==,or bccomp, or comparing round()ed numbers. It was delightful when we had to compare floats casted to strings because 13.61 (or something like that) wasn't equal to 13.61, no matter what. If I remember correctly, one side of comparison had to be first assigned to additional variable. Happened once - laughs were had, hair were lost, as was the sanity. Fun times.

Still my favourite is adding an empty comment to the source code, to make certain Zend logic work. And it happened suddenly - everything works fine, then forms start throwing errors on initialization, then I add some debugging, error disappears, but reappears when I remove the debugging, then I add a comment to change filesize of the file, that does the trick. No caching of source code, no opcache, just :ghost:

VikingofRock
Aug 24, 2008




Absurd Alhazred posted:

It seems like that might be and allowable exception, but in situations where you will be dividing by a number that could be zero, you probably also don't want it to be too small, either.

Fair enough. I could see preferring huge numbers to Infinity though, since big numbers are at least more well behaved (as far as I know).

Hammerite
Mar 9, 2007

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

VikingofRock posted:

Fair enough. I could see preferring huge numbers to Infinity though, since big numbers are at least more well behaved (as far as I know).

Isn't the result of dividing a sufficiently large number by a sufficiently small (but nonzero) one still infinity?

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

VikingofRock posted:

What about for special casing to avoid dividing by zero?

Don't you end up missing dividing by -0 then?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

leper khan posted:

Don't you end up missing dividing by -0 then?

No, -0 == 0.

Absurd Alhazred
Mar 27, 2010

by Athanatos

rjmccall posted:

No, -0 == 0.

But 1/-epsilon is really really negative, while 1/+epsilon is really really positive.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

Absurd Alhazred posted:

But 1/-epsilon is really really negative, while 1/+epsilon is really really positive.

Most of the time when I worry about dividing by zero, all I'm looking to prevent is the exception; I don't care if I get wildly weird numbers as long as the CPU doesn't just throw up its hands and go "nope, gently caress it, I'm out."

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Absurd Alhazred posted:

But 1/-epsilon is really really negative, while 1/+epsilon is really really positive.

For that matter, 1/-0 is -Inf while 1/+0 is +Inf. FP equality doesn't imply perfect substitutability.

TooMuchAbstraction posted:

Most of the time when I worry about dividing by zero, all I'm looking to prevent is the exception; I don't care if I get wildly weird numbers as long as the CPU doesn't just throw up its hands and go "nope, gently caress it, I'm out."

FP divide-by-zero doesn't trap unless you've in some weird configuration or have non-IEEE hardware.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

rjmccall posted:

FP divide-by-zero doesn't trap unless you've in some weird configuration or have non-IEEE hardware.

Oh, you're right, I was thinking of integer division. My bad.

OddObserver
Apr 3, 2009

TooMuchAbstraction posted:

Oh, you're right, I was thinking of integer division. My bad.

That producing a SIGFPE always kinda bothered me.

sarehu
Apr 20, 2007

(call/cc call/cc)
Often with floats it is provable that x <= y and to test x == y to check a special case at an endpoint is valid. Or say you have sin(x)/x and want 1, not NaN, at zero.

wide stance
Jan 28, 2011

If there's more than one way to do a job, and one of those ways will result in disaster, then he will do it that way.

Cuntpunch posted:

code:
//Do not change to match database, in dev and test environments this is correct
//I tried adding 2 hours and it worked locally but then is 2 hours too high on server

This is the greatest horror. Writing a novel in your comment that's made illegitimate on the next commit yet everyone is forced to read it and try to get in that person's mind if they want to work in that file.

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.
in php, '==' is 'equivalence' and '===' is 'equality'. Similarly '!=' and '!=='. The main difference is that the extra '=' implies a type check, as well.

The problem comes down to PHP's VeryWeakTypes(tm) - until you force a variable to be a string or an integer (or a float, etc.) then it's not actually certain what it is.



In short:

php:
<?
// strings vs integers
'' == 0; // true 
'0' == 0; // true
'' == '0'; // false

// strings, booleans, integers, nulls, arrays.
'' == false;
null == false;
0 == false;
'0' == false;
[] == false;

'anything other than 0' == true;
1 == true;
-1 == true;
[0] == true;
['0'] == true;
?>
This isn't unique to PHP - Javascript and some other languages have it, too. It's just the nature of weak typing.

bobthecheese fucked around with this message at 08:49 on Jul 16, 2016

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Yeah, but those languages don't have the feature where two number-like strings are parsed to numbers and then tested.

code:
$ php -r 'var_dump("1e3" == "1000");'
bool(true)

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

Suspicious Dish posted:

Yeah, but those languages don't have the feature where two number-like strings are parsed to numbers and then tested.

code:
$ php -r 'var_dump("1e3" == "1000");'
bool(true)

Wait doesn't php also have the thing where "12ab" == "12cd"?

So that would mean that "1d3" == "1d4", but "1e3" != "1e4", right? :psyboom:

comedyblissoption
Mar 15, 2006

weak typing is a non-word

it's just the general language misfeature of allowing implicit type conversions in situations that can easily cause bugs

even "strongly" typed languages often do really stupid poo poo in terms of implicit conversions to boolean that will cause bugs forever and ever, although i guess sane linters will usually warn about it

comedyblissoption
Mar 15, 2006

C++ is considered strongly typed. C++ implicitly casts to bool (this could be blamed on the C legacy). However, c++ even has built-in implicit cast operators that create bugs including implicit casts to bool, so uh make your own determination on c++'s position there. Some would argue that that's just users hanging themselves and abusing the features, but I disagree.

Python is often considered by proponents to be strongly typed. The language encourages pervasive bug-creating casting to bool.

b0lt
Apr 29, 2005
spent a day debugging a mysterious stack overflow caused by va_args pulling garbage arguments because of insufficient stack alignment

:negative:

b0lt fucked around with this message at 10:26 on Jul 16, 2016

comedyblissoption
Mar 15, 2006

did you know in python that an implicit cast to bool for one of their time types will be false if the instance is either null or exactly midnight?

do you agree this is really stupid and can cause bugs?

let's please stop using the weak/strong language terminology and just agree implicit type coercions are very bad except in very limited cases

Adbot
ADBOT LOVES YOU

comedyblissoption
Mar 15, 2006

quote:

There was quite some pushback at the time since many of us feared that the new type and constants would be used by Python newbies to restrict the language's abilities, but Guido was adamant that we were just being pessimistic: nobody would ever understand Python so badly, for example, as to avoid the perfectly natural use of False and True as list indices, or in a summation, or other such perfectly clear and useful idioms.
http://stackoverflow.com/a/3175293

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