I'm having a problem in both Java and MATLAB where two numbers which should be the same apparently aren't.
Java:
float f1 = 0.3f-0.2f;
float f2 = 0.4f-0.3f;
System.out.println(f1==f2); // Prints false
System.out.println(f1); // Prints 0.10000001
System.out.println(f2); // Prints 0.099999994
Obviously f1
should be equal to f2
. What is the most efficient (computationally) method to replace equals
below that would return true?
public boolean equals(float f1, float f2){
return f1 == f2;
}
I will need to operate on hundreds of thousands of floats so optimization is important here.