I was writing a sample program with floats but suddenly something weird happened. I would really appreciate if someone can shed some light on why I am facing such behavior from my program.
package Programs;
public class FloatTest {
/**
* @param args
*/
public static void main(String[] args) {
float f1 = (float) 3.2;
float f2 = (float) 6.5;
if (f1 == 3.2) {
System.out.println(f1 + " same");
} else {
System.out.println(f1 + " different");
}
if (f2 == 6.5) {
System.out.println(f2 + " same");
} else {
System.out.println(f2 + " different");
}
}
}
Output:
3.2 different
6.5 same
After doing some tests with changing values of f2 I noticed that I get unexpected result for f2 > 3.5 Why is that? Any input is really appreciated.
Thanks