Skip to content

Instantly share code, notes, and snippets.

@zimmski
Last active November 13, 2022 19:04
Show Gist options
  • Save zimmski/f369cf178ba4d68fd85ca9e3adf7531f to your computer and use it in GitHub Desktop.
Save zimmski/f369cf178ba4d68fd85ca9e3adf7531f to your computer and use it in GitHub Desktop.
Not so floaty in Java
public class MyClass {
public static void main(String args[]) {
float x = 0.3f;
float y = 0.2f;
float z = x - y;
System.out.println(z);
float expectedZ = 0.1f;
float delta = 0.00000001f;
System.out.println(z == expectedZ);
System.out.println(Float.floatToIntBits(z) == Float.floatToIntBits(expectedZ));
System.out.println(Math.abs(z - expectedZ) <= delta);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment