Floating Point Errors



Because of the way computers store numbers there are going to be mistakes when doing computations with doubles. You should never use == to check if two doubles are equale, because even though they should be, they might not be.

public class tester {
public static void main() {
double dollar = 1.00;
double dime = 0.10;
int number = 7;
System.out.println("A dollar less " + number + " dimes is $" +
(dollar - number * dime) );

//another floating point calculation

double amount1 = 2.15;
double amount2 = 1.10;
System.out.println("difference between 2.15 and 1.0 using double is: " + (amount1 - amount2));
}
}


and even more isolated with Blue J

double amount1 = .15;
double amount2 = .1;
double s = amount1 - amount2;
System.out.println(s);
output
0.04999999999999999