Well I have following program. It is given by my teacher for fun. I got surprise about the result.
Code:
public class Testing {
public static void main(String[] args) {
float piF = 3.141592653589793f; // value assigned has float precision
double piD = 3.141592653589793; // value assigned has double precision
final double THRESHOLD = .0001;
if( piF == piD)
System.out.println( "piF and piD are equal" );
else
System.out.println( "piF and piD are not equal" );
if( Math.abs( piF - (float) piD ) < THRESHOLD )
System.out.println( "piF and piD are considered equal" );
else
System.out.println( "piF and piD are not equal" );
}
}
Result:
piF and piD are not equal
piF and piD are considered equal
Well why piF and piD are not equal ? And what actually Math.abs()
do that makes both same ?