Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many double numbers are there between 0.0 and 1.0?

This is something that's been on my mind for years, but I never took the time to ask before.

Many (pseudo) random number generators generate a random number between 0.0 and 1.0. Mathematically there are infinite numbers in this range, but double is a floating point number, and therefore has a finite precision.

So the questions are:

  1. Just how many double numbers are there between 0.0 and 1.0?
  2. Are there just as many numbers between 1 and 2? Between 100 and 101? Between 10^100 and 10^100+1?

Note: if it makes a difference, I'm interested in Java's definition of double in particular.

like image 510
polygenelubricants Avatar asked Jun 05 '10 02:06

polygenelubricants


People also ask

How many doubles are there between 0 and 1?

This basically means there is a total of 2^62-2^52+1 of possible double representations that according to the standard are between 0 and 1.

How many float between 0 and 1?

So how many “normal” non-zero numbers are there between 0 and 1? The negative exponents range from -1 all the way to -126. In each case, we have 223 distinct floating-point numbers because the mantissa is made of 23 bits. So we have 126 x 223 normal floating-point numbers in [0,1).

Is 1.5 a double or float?

And the reason the comparison succeeds with 1.5 is that 1.5 can be represented exactly as a float and as a double ; it has a bunch of zeros in its low bits, so when the promotion adds zeros the result is the same as the double representation.

What is double number example?

To get a double of a number, we add the same number to itself. For example, double of 2 is 2 + 2 = 4. Example: Michelle has 4 marbles and Jane has double the marbles that Michelle has.


2 Answers

Java doubles are in IEEE-754 format, therefore they have a 52-bit fraction; between any two adjacent powers of two (inclusive of one and exclusive of the next one), there will therefore be 2 to the 52th power different doubles (i.e., 4503599627370496 of them). For example, that's the number of distinct doubles between 0.5 included and 1.0 excluded, and exactly that many also lie between 1.0 included and 2.0 excluded, and so forth.

Counting the doubles between 0.0 and 1.0 is harder than doing so between powers of two, because there are many powers of two included in that range, and, also, one gets into the thorny issues of denormalized numbers. 10 of the 11 bits of the exponents cover the range in question, so, including denormalized numbers (and I think a few kinds of NaN) you'd have 1024 times the doubles as lay between powers of two -- no more than 2**62 in total anyway. Excluding denormalized &c, I believe the count would be 1023 times 2**52.

For an arbitrary range like "100 to 100.1" it's even harder because the upper bound cannot be exactly represented as a double (not being an exact multiple of any power of two). As a handy approximation, since the progression between powers of two is linear, you could say that said range is 0.1 / 64th of the span between the surrounding powers of two (64 and 128), so you'd expect about

(0.1 / 64) * 2**52 

distinct doubles -- which comes to 7036874417766.4004... give or take one or two;-).

like image 134
Alex Martelli Avatar answered Sep 22 '22 15:09

Alex Martelli


Every double value whose representation is between 0x0000000000000000 and 0x3ff0000000000000 lies in the interval [0.0, 1.0]. That's (2^62 - 2^52) distinct values (plus or minus a couple depending on whether you count the endpoints).

The interval [1.0, 2.0] corresponds to representations between 0x3ff0000000000000 and 0x400000000000000; that's 2^52 distinct values.

The interval [100.0, 101.0] corresponds to representations between 0x4059000000000000 and 0x4059400000000000; that's 2^46 distinct values.

There are no doubles between 10^100 and 10^100 + 1. Neither one of those numbers is representable in double precision, and there are no doubles that fall between them. The closest two double precision numbers are:

99999999999999982163600188718701095... 

and

10000000000000000159028911097599180... 
like image 21
Stephen Canon Avatar answered Sep 25 '22 15:09

Stephen Canon