// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.math.BigDecimal;
import java.math.MathContext;

public class Precision {
    private static final int REP = 1000000000;
    private static final float DF = 0.01f;
    private static final double DD = 0.01;
    private static final BigDecimal DBD = new BigDecimal("0.01", new MathContext(2));
    public static void main(String[] args) {
        int n;
        n = 0;
        float xf = 0;
        for(int i = 0; i < REP; i++) {
            n++;
            xf += DF;
        }
        System.out.printf("Java float says %d x %.2f = %.2f\n", n, DF, xf);
        n = 0;
        double xd = 0;
        for(int i = 0; i < REP; i++) {
            n++;
            xd += DD;
        }
        System.out.printf("Java double says %d x %.2f = %.2f\n", n, DD, xd);
        n = 0;
        BigDecimal xbd = new BigDecimal("0", new MathContext(2));
        for(int i = 0; i < REP; i++) {
            n++;
            xbd = xbd.add(DBD);
        }
        System.out.printf("Java BigDecimal says %d x %s = %s\n", n, DBD, xbd);
    }
}
