Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this true?

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));
    }
}
like image 605
fabrizioM Avatar asked Nov 04 '10 05:11

fabrizioM


2 Answers

Plainly said - 50 is a rounding error when a float has a value of two-billion.

like image 22
Stevko Avatar answered Sep 21 '22 21:09

Stevko


Because a float can 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 float consists of three parts:

  • the sign bit (1 bit)
  • the exponent (8 bits)
  • the significand (24 bits, but only 23 bits are stored since the MSB of the significand is always 1)

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 a float can hold log(2 ^ 24) = 7.225 significant 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 double since it has a 52-bit significand, which is more than enough to represent every possible 32-bit number.

like image 119
In silico Avatar answered Sep 20 '22 21:09

In silico