Java double
s 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 double
s (i.e., 4503599627370496 of them). For example, that's the number of distinct double
s 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 double
s 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 / 64
th of the span between the surrounding powers of two (64 and 128), so you'd expect about
(0.1 / 64) * 2**52
distinct double
s -- which comes to 7036874417766.4004
... give or take one or two;-).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…