News for hackers. Stuff that matters.
Re:'9223372036854775807' == '9223372036854775808' by DiabloD3 on Thursday April 12, 2012 @09:04AM (#3832125)
The fact PHP fails at floating point math isn't news (and I swear I've seen this exact bug somewhere, same numbers and everything, somewhere else)...

Its the fact PHP refuses to fix it that is the news.

Re:'9223372036854775807' == '9223372036854775808' by gloob on Thursday April 12, 2012 @10:13AM (#3832354)
This sort of thing is an intrinsic property of floating point math. It has limited precision. When the numbers get sufficiently large, that precision is insufficient to distinguish successive integers. That is to say, this is symptomatic of PHP implementing floating point stuff correctly.

This is what ghci (Haskell) says:

  Prelude> 9223372036844775807 == 9223372036844775808
  False
  Prelude> 9223372036844775807.0 == 9223372036844775808.0
  True
This is what Python says:

  >>> 9223372036844775807 == 9223372036844775808
  False
  >>> 9223372036844775807.0 == 9223372036844775808.0
  True
Here is what SBCL (Common Lisp) says:

  * (= 9223372036844775807 9223372036844775808)
  NIL
  * (= 9223372036844775807.0 9223372036844775808.0)
  T
Lua:

  > print(9223372036844775807 == 9223372036844775808)
  true (!!!!!)
  > print(9223372036844775807.0 == 9223372036844775808.0)
  true
Javascript:

  alert(9223372036844775807 == 9223372036844775808)
  true (!!!!!)
  alert(9223372036844775807.0 == 9223372036844775808.0)
  true
Other languages that will also do this[1]: Javascript, Lua. Languages that won't: anything with actual, honest-to-god integers, and not floats or doubles masquerading as them.[2] Languages that actually handle numbers sensibly: Lisp.[3] I'm not familiar with any others that actually treat rational numbers like rational numbers, but I expect there are some. (It's still, of course, impossible to treat real numbers like real numbers, meaning that this sort of thing will also happen there.)

[1] Well, not the string-to-number bit, but whatever.

[2] Except for the niggle that they'll still do this when you're using floating point numbers, because this is what floating point numbers do.

[3] https://en.wikipedia.org/wiki/Numerical_tower

Re:'9223372036854775807' == '9223372036854775808' by eblume on Thursday April 12, 2012 @10:41AM (#3832486)
I think you may be missing the point, or maybe it's I who am missing the point. Your three very thorough examples are a good way of showing how (most?) languages handle floating-point arithmetic vs. arbitrary arithmetic.

But it seems to me - and let me stress that I am not a PHP developer and won't be bothered to install PHP on my machine at this time - that PHP is failing to exhibit exactly the behavior your code examples are giving.

Put it another way - type coercion 'run amok' being another thing entirely, you are correct that this bug stems from the fact that PHP is converting these integers to floating-point, and the standard floating point implementations will all behave in this exact way (thus, not a PHP bug.)

However, the issue here is that (again, "most?") languages also provide an easy way to get to arbitrary-precision arithmetic - and indeed, in the three examples you posted, you simply encode in the most natural way (by simply writing them) the two integers and they automatically compare correctly.

My understanding is that this is not the case in PHP, and that is a shame.

Re:'9223372036854775807' == '9223372036854775808' by gloob on Thursday April 12, 2012 @10:58AM (#3832572)
I agree that, when we are talking about high-level languages, I prefer ones that will transparently convert integers to bignum when required. I'm just replying to the contention that (paraphrased) "this is a bug in PHP's handling of floating point numbers".
Re:'9223372036854775807' == '9223372036854775808' by eblume on Thursday April 12, 2012 @11:09AM (#3832623)
Ah, point taken, sorry about that. :)
Re:'9223372036854775807' == '9223372036854775808' by tmcb on Thursday April 12, 2012 @01:14PM (#3833286)
Lua internally represents any kind of number as a floating-point one.

http://www.lua.org/pil/2.3.html

Re:'9223372036854775807' == '9223372036854775808' by bonzoesc on Thursday April 12, 2012 @09:06AM (#3832131)
To anybody not familiar with PHP, it appears to fail at string comparison.
Re:'9223372036854775807' == '9223372036854775808' by ctdonath on Thursday April 12, 2012 @10:40AM (#3832479)
Appears? It DOES fail at string comparison. They're strings. You can't make it any plainer that they're strings. Sometimes, like with barcode processing, strings contain only numeric characters - they're still strings regardless. If I can't ask if one string literal is equal to another string literal without it making a concerted effort to not just convert both strings to integers (which, BTW, would be fine so long as it preserved all digits) but to then convert it from a non-lossy to a lossy data type - that's TWO unwarranted type conversions - then the axioms of the language are untenable, rendering it useless in the real world and making it little more than a toy.

If you're going to run with PHP's axioms, then the specifications should (1) demand an unlimited-length integer type, and (2) NEVER convert a non-lossy to lossy data type without very good overt reason.

Fail.

Re:'9223372036854775807' == '9223372036854775808' by zdouglas on Thursday April 12, 2012 @09:29AM (#3832201)
Arguably, such an obtuse implicit cast is failure at string comparison.
Re:'9223372036854775807' == '9223372036854775808' by kba on Thursday April 12, 2012 @09:13AM (#3832152)