Skip to content

Instantly share code, notes, and snippets.

@szegedi
Created January 21, 2014 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szegedi/8545498 to your computer and use it in GitHub Desktop.
Save szegedi/8545498 to your computer and use it in GitHub Desktop.
Fun with floats
import java.util.Objects;
public class FunWithFloats {
public static void main(String[] args) {
float f1 = 0.7f;
System.out.println(f1); // prints 0.7
double d = f1;
System.out.println(d); // prints 0.699999988079071
float f2 = (float)d;
System.out.println(f2); // prints 0.7
System.out.println(f1 == f2); // true
System.out.println(Objects.equals(f1, f2)); // true
System.out.println(d == f1); // true
System.out.println(Objects.equals(d, f1)); // false
System.out.println(d == f2); // true
System.out.println(Objects.equals(d, f2)); // false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment