<< Chapter < Page | Chapter >> Page > |
This method overrides the equals method inherited from the Object class. It compares the double values stored in two matrices and returns true if the values are equal or almost equal and returns false otherwise.
An inherent problem when comparing doubles and floats
There is always a problem when comparing two double or float values for equality. If you perform a series of computations twice, usinga different computational order for each set of computations, you are likely to end up with two values that are not absolutely equal even if they should beequal. Arithmetic inaccuracies along the way may cause the two results to differ everso slightly. However, they may be equal from a practical viewpoint.
See Figure 3 where the a value that should be 0.0 is actually given by -1.7763568394002505E-15
Therefore, when comparing two double or float values for equality, it is customary to subtract one from the other, convert the differenceto an absolute value, and compare that absolute value with an arbitrarily small positive value. If the difference is less than that the test value, thetwo original values are declared to be equal. Otherwise, they are declared to be unequal.
This is the logic that is implemented in Listing 1 , which shouldn't require further explanation.
Overridden equals method of the GM2D03.Point class
Listing 2 presents the overridden equals method of the GM2D03.Point class.
Listing 2 . Overridden equals method of the GM2D03.Point class. |
---|
public boolean equals(Object obj){
if(point.equals(((GM2D03.Point)obj).getColMatrix())){
return true;}else{
return false;}//end else}//end overridden equals method |
This method also overrides the equals method inherited from the Object class. It compares the values stored in the ColMatrix objects that define two Point objects and returns true if they are equal and false otherwise.
One possible point of confusion
The one thing that can be confusing in Listing 2 has to do with the variable named point . This is a reference to an object of type ColMatrix that defines the location of the Point object. Listing 2 calls another new method named getColMatrix to get access to the ColMatrix object that defines the incoming Point object, and uses that object for the comparison. In effect, this method actually compares two objects ofthe ColMatrix class by calling the method that I explained in Listing 1 . This illustrates the advantages of building up the library classes using objectsof the fundamental ColMatrix class.
The getColMatrix method of the GM2D03.Point class
This extremely simple new method, which is called by the code in Listing 2 , is presented in Listing 3 .
Listing 3 . The getColMatrix method of the GM2D03.Point class. |
---|
//Returns a reference to the ColMatrix object that
// defines this Point object.public GM2D03.ColMatrix getColMatrix(){
return point;}//end getColMatrix |
Listing 3 shouldn't require any explanation beyond the embedded comments.
Overridden equals method of the GM2D03.Vector class
The overridden equals method of the GM2D03.Vector class is essentially the same as the code shown for the Point class in Listing 2 so I won't bother to show and explain it. You van view this new method in Listing 11 .
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?