/* * T01n04.java -- Shows why you sometimes need to worry about how * floating-point numbers are stored. */ public class T01n04 { public static void main (String [] args) { float sumF, // holds the sum of ten tenths oneTenthF = 0.1F; // one-tenth as a float double sumD, // also holds the sum of ten tenths oneTenthD = 0.1; // also one-tenth as a double // The sum of ten one-tenths ought to equal one, either way sumF = oneTenthF + oneTenthF + oneTenthF + oneTenthF + oneTenthF + oneTenthF + oneTenthF + oneTenthF + oneTenthF + oneTenthF; sumD = oneTenthD + oneTenthD + oneTenthD + oneTenthD + oneTenthD + oneTenthD + oneTenthD + oneTenthD + oneTenthD + oneTenthD; // Let's find out if the sums are correct System.out.println("The sum of 10 one-tenths should be exactly 1..."); System.out.println("Using Float types: " + sumF); System.out.println("Using Double types: " + sumD); } }