This is IEEE 754 standard question. I don’t completely understand the mechanics behind it.
public class Gray {
public static void main(String[] args){
System.out.println( (float) (2000000000) == (float) (2000000000 + 50));
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because a
floatcan only hold about 7 to 8 significant digits. That is, it doesn’t have enough bits to represent the number 2000000050 exactly, so it gets rounded to 2000000000.Specifically speaking, a
floatconsists of three parts:You can think of floating point as the computer’s way doing scientific notation, but in binary.
The precision is equal to
log(2 ^ number of significand bits). That means afloatcan holdlog(2 ^ 24) = 7.225significant digits.The number 2,000,000,050 has 9 significant digits. The calculation above tells us that a 24-bit significand can’t hold that many significant digits. The reason why 2,000,000,000 works because there’s only 1 significant digit, so it fits in the significand.
To solve the problem, you would use a
doublesince it has a 52-bit significand, which is more than enough to represent every possible 32-bit number.