Home > OS >  What is the range in which IEEE754 can correctly compare the size of floating point numbers
What is the range in which IEEE754 can correctly compare the size of floating point numbers

Time:07-01

I know that the range of integers that can be correctly compared is -(2^53 - 1) to 2^53 - 1.

But what is the range of floating point numbers?

// Should the double type conform to this specification? I guess
double c = 9007199254740990.9;
double d = 9007199254740990.8;
System.out.println(c > d); // return false

CodePudding user response:

There's no particular range in which floating point numbers are guaranteed to be compared correctly - that would require the existence of infinitely many double representations.

All double numbers are accurate to 53 significant figures of their representations in binary. If two numbers differ only in the 54th significant binary figure, they'll generally have the same double representation, unless rounding makes them different.

CodePudding user response:

This question seems to be based on a fundamental misunderstanding of (mathematical) Real numbers, and their relationship to IEEE-754.

"Floating point" refers to a representation scheme for Real numbers. Historically that comprises decimal digits and a "decimal point". There are an infinite number of Real numbers, and (correspondingly) an infinite number of floating point numbers. Provided that you have enough paper to write them down.

One property of Real numbers is that there is an infinite number of Real numbers between any pair of distinct Real numbers. That includes any pair that are really close to one another.

IEEE-754 floating point numbers are different. Each one represents a single precise Real number. There is a set of (less than) 2^64 of these Real numbers for IEEE-754 double precision. Any other Real number that is not in that set cannot be precisely represented as a double.

In Java, when we write this:

double c = 9007199254740990.9;
double d = 9007199254740990.8;
System.out.println(c > d); // return false

the value of c will be the IEEE-754 representation that is closest to the real number that the floating point notation denotes. Likewise d. It is not going to be exactly equal to the Real number. (It can only be exact for a finite subset of the infinite set of Real numbers ...)

The print statement illustrates this. The closest IEEE-754 representations for those 2 Real numbers are the same.

What actually happens is that the Java compiler works out what 9007199254740990.9 and 9007199254740990.8 mean as Real numbers. Then it silently rounds those numbers to the nearest IEEE-754 double.


So to your question:

What is the range in which IEEE-754 can correctly compare the size of floating point numbers

A: There is no such range.

If you give me any Real number "x" (expressed in floating point form) that maps to a double value d, I can write you a different Real number "y" (in floating point form) that also maps to d. All I need to do is to add some additional digits to the least significant end of "x".

Note: this logic is independent of the actual representation of IEEE-754. It only depends on the fact that IEEE-754 representations have a fixed number of bits. That means that the set of possible values is bounded ... and the rest is a logical consequence of that.

CodePudding user response:

Let's think about the actual bit representation of these double numbers:

We can use System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(c))); to print the bit representation.

Both 9007199254740990.9 and 9007199254740990.8 has the same bit representation following IEEE 754:

0 10000110011 1111111111111111111111111111111111111111111111111111

s(sign) = 0
exp = 10000110011 (binary value, which equals 1075 in decimal)
frac = 111...111 (52 bits)

The precise value of this representation is:

value calculation

With IEEE 754, starting from 2^53, the double type is no longer able to precisely represent all integers larger than 2 ^ 53, let alone floating point numbers.

Regarding the OP question, as mentioned in other answers, there's not an accurate range. For example, the following statement will evaluate as true. We won't say that the range of floating numbers that double can compare is less than 1.1. Generally speaking, the larger the number is, the less precise the double type could represent.

double x1 = 1.1;
double x2 = 1.1000000000000001;
System.out.println(x1 == x2); // true
  • Related