LWN.net Logo

PHP: a fractal of bad design (fuzzy notepad)

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 22, 2012 18:24 UTC (Sun) by Cyberax (✭ supporter ✭, #52523)
In reply to: PHP: a fractal of bad design (fuzzy notepad) by ajf
Parent article: PHP: a fractal of bad design (fuzzy notepad)

Well, use of equality for object types in Java is dangerous. Which is told in every textbook on it.

On the other hand, the real equality comparison operation ('equals' method) is transitive and commutative. Well, unless you screw it up intentionally.


(Log in to post comments)

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 26, 2012 0:10 UTC (Thu) by IkeTo (subscriber, #2122) [Link]

It depends on what you call "screw it up intentionally". The following program output "true" for most numbers larger than 16777216 or smaller than -16777217. It is 127/128 of the range [-2^31, 2^31-1] of int:

public class T {
    public static void main(String[] args) {
        int a = Integer.parseInt(args[0]);
        float b = a;
        int c = a + 1;
        System.out.println(a == b && b == c && a != c);
    }
}
Similar behavior can be found if you replace int by long and float by double.

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 26, 2012 1:50 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link]

That's completely expected and correct behavior of equality comparisons involving floating point numbers. It's also noted in every textbook under the DO NOT USE category.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds