adamleesaintfrancis / spacewar

Automatically exported from code.google.com/p/spacewar
0 stars 0 forks source link

Vector2D equals error #8

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Make a HashSet of Vector2D
2. Add a Vector2D to the set
3. Perform HashSet.contains(Vector2D)

or even repeat with an ArrayList

What is the expected output? What do you see instead?
I would expect it to state that the set/list contains the vector, but it 
does not.

What version of the product are you using? On what operating system?
The latest update of the code on eclipse 3.3.1.1

Please provide any additional information below.
Can be fixed by updating Vector2D to include:

public int hashCode() {
        final int prime = 31;
        int result = 1;
    result = prime * result + Float.floatToIntBits(magnitude);
    result = prime * result + Float.floatToIntBits(x);
    result = prime * result + Float.floatToIntBits(y);
    return result;
}

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    final Vector2D other = (Vector2D) obj;
    if (Float.floatToIntBits(magnitude) != Float
            .floatToIntBits(other.magnitude))
        return false;
    if (Float.floatToIntBits(x) != Float.floatToIntBits(other.x))
        return false;
    if (Float.floatToIntBits(y) != Float.floatToIntBits(other.y))
        return false;
    return true;
}

That is code generated by eclipse.

Also, please cc: jfager on any new issues that are opened!  Thank you!

Original issue reported on code.google.com by a1tetris...@gmail.com on 20 Feb 2008 at 9:08

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
This is because even parameterized collections, like, say ArrayList<Vector2D>, 
use
equals(Object), not equals(Vector2D) when determining if an element is in the 
list.
This is counter-intuitive because, in contrast, sorting collections like
PriorityQueue do do use the parameterized compareTo method, as specified by
Comparable<Vector2D>.

The set/list actually will return true for containing the same exact Vector2D 
because
it will default to identity (==) in the base Object class equals, but it will 
not
return true for contains when given a Vector2D that should be considered 
equivalent
(x and y equal).

The included code should address the issue. It's possible that magnitude need 
not be
considered for equality.

I will add this to the repository before project 2.

Original comment by b1m...@gmail.com on 20 Feb 2008 at 10:32

GoogleCodeExporter commented 9 years ago
Just as a side comment, you shouldn't depend too much on comparing floating 
point
numbers for equality, unless you're being really careful.  It's not hard to 
stumble
into two different calculations that should mathematically result in the same 
vector,
that would end up giving different Vector2Ds due to floating-point arithmetic
limitations.

Original comment by jfa...@gmail.com on 13 Mar 2008 at 4:01

GoogleCodeExporter commented 9 years ago

Original comment by jfa...@gmail.com on 25 Mar 2008 at 10:47