/****************************************************************************** * This program demonstrates floating point error. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ import org.pureprogrammer.Utils; public class FloatingPointError { public static void main(String[] args) { double oneThird = 1. / 3.; System.out.println(Utils.format("1/3: {0:.16f}", oneThird)); System.out.println(Utils.format("one: {0:.16f}", oneThird * 3.0)); System.out.println(Utils.format("three: {0:.16f}", oneThird * 9.0)); System.out.println(Utils.format("hundred: {0:.16f}", oneThird * 300.0)); double twoTenths = 0.2; double sum = 0.0; for (int x = 0; x < 1000; ++x) { sum += twoTenths; } System.out.println(Utils.format("twoTenths: {0:.16f}", twoTenths)); System.out.println(Utils.format("sum: {0:.16f}", sum)); System.out.println(Utils.format("200: {0:.16f}", twoTenths * 1000.)); } }