Skip to content

Instantly share code, notes, and snippets.

@hossman
Created March 28, 2018 16:45
Show Gist options
  • Select an option

  • Save hossman/815032db878e898f63c1d90ff02f1b3f to your computer and use it in GitHub Desktop.

Select an option

Save hossman/815032db878e898f63c1d90ff02f1b3f to your computer and use it in GitHub Desktop.
why java float math is fun -- aka -- IEEE 754 and base2 rounding
public final class Temp {
public static void main(String[] args) throws Exception {
// why java float math is fun
// aka: IEEE 754 and base2 rounding
System.out.println("0.0 + 0.3 = " + (0.0F + 0.3F)); // 0.3
System.out.println("0.3 + 0.3 = " + (0.3F + 0.3F)); // 0.6
System.out.println("0.6 + 0.3 = " + (0.6F + 0.3F)); // 0.90000004
System.out.println("0.0 + 0.3 = " + (0.0D + 0.3D)); // 0.3
System.out.println("0.3 + 0.3 = " + (0.3D + 0.3D)); // 0.6
System.out.println("0.6 + 0.3 = " + (0.6D + 0.3D)); // 0.8999999999999999
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment