/*** Created by Frank * Compare floating point numbers*/ Public classfloatcmp {//Tolerances Private Final StaticDouble EPSILON = 0.0000001; Public Static voidMain (string[] args) {DoubleDa = 3 *. 3333333333; Doubledb = 0.99999992857; //the base type can be compared by using = = First if(Da = =db) {System.out.println ("Java considers" + da + "= =" +db); } //using the overloaded Equals method Else if(Equals (DA, DB, 0.0000001) {System.out.println ("Equal within Epsilon" +EPSILON); } Else{System.out.println (da+ "!=" +db); } } /*** Compare 2 double type values within a given range of accuracy * *@parama parameter A *@paramB Parameter B *@paramthe tolerance value given by EPS *@returnCompare Results*/ Public Static BooleanequalsDoubleADoubleBDoubleEPS) { returnA = = B | | Math.Abs (A-B) <EPS; } /*** Compare 2 double type values within the default error range * *@parama parameter A *@paramB Parameter B *@returnCompare Results*/ Public Static BooleanequalsDoubleADoubleb) {returnequals (A, b, EPSILON); }}
Java Classic example: comparing floating-point numbers