Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many distinct floating-point numbers in a specific range?

How many rep­re­sentable floats are there be­tween 0.0 and 0.5? And how many representable floats are there between 0.5 and 1.0? I'm more interested in the math behind it, and I need the answer for floats and doubles.

like image 580
Arlen Avatar asked Jan 16 '12 01:01

Arlen


People also ask

What is the range of the floating-point numbers?

A single-precision, floating-point number is a 32-bit approximation of a real number. The number can be zero or can range from -3.40282347E+38 to -1.17549435E-38, or from 1.17549435E-38 to 3.40282347E+38.

How many different floating-point numbers are there?

For any given value of the exponent, there are [latex] 2^{24} = 16777216[/latex] possible numbers that can be represented. However, the exponent decides how big that number will be.

How do you determine the number of floating points?

To do so, floating-point uses a biased exponent, which is the original exponent plus a constant bias. 32-bit floating-point uses a bias of 127. For example, for the exponent 7, the biased exponent is 7 + 127 = 134 = 100001102. For the exponent −4, the biased exponent is: −4 + 127 = 123 = 011110112.

How many floating-point numbers are there 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).


1 Answers

For IEEE754 floats, this is fairly straight forward. Fire up the Online Float Calculator and read on.

All pure powers of 2 are represented by a mantissa 0, which is actually 1.0 due to the implied leading 1. The exponent is corrected by a bias, so 1 and 0.5 are respectively 1.0 × 20 and 1.0 × 2−1, or in binary:

      S    Ex + 127    Mantissa - 1                Hex

1:    0    01111111    00000000000000000000000     0x3F800000
      +     0 + 127    1.0

0.5:  0    01111110    00000000000000000000000     0x3F000000
      +    -1 + 127    1.0

Since the floating point numbers represented in this form are ordered in the same order as their binary representation, we only need to take the difference of the integral value of the binary representation and conclude that there are 0x800000 = 223, i.e. 8,388,608 single-precision floating point values in the interval [0.5, 1.0).

Similarly, the answer is 252 for double and 263 for long double.

like image 130
Kerrek SB Avatar answered Sep 17 '22 16:09

Kerrek SB