Created
March 28, 2018 16:45
-
-
Save hossman/815032db878e898f63c1d90ff02f1b3f to your computer and use it in GitHub Desktop.
why java float math is fun -- aka -- IEEE 754 and base2 rounding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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