How many distinct numbers are from 1.5 x 10-45 to 3.4 x 1038 (IEE754 single precision floats)?
This isn't really programming.
bc says (for whole integers):
1.5*10^45
1500000000000000000000000000000000000000000000.0
3.4*10^38
340000000000000000000000000000000000000.0
1500000000000000000000000000000000000000000000.0-340000000000000000000000000000000000000.0
1499999660000000000000000000000000000000000000.0
I think you mean integer numbers. And also you mean between 3.4*10^38 and 1.5*10^45 because 1.5*10^45 is larger than the other one. Anyway the answer is the same as with smaller numbers. I'll assume you want to exclude these two numbers so :
How many numbers are there between 2 and 10? The answer is 10-2-1=7. Indeed 3,4,5,6,7,8,9 are 7 numbers. So the "formula" is :
How many numbers are between a and b? The answer is b-a-1
So 1.5*10^45-3.4*10^38 -1 = 15*10^44-34*10^37 -1 = (15*10^7)10^37-3410^37 -1 =(15*10^7-34)*10^37 -1 = (150000000-34) * 10^37 -1 = 149999966 * 10^37 -1 or 1499999659999999999999999999999999999999999999
Assuming that you are talking about the range in IEEE single-precision float (the 1.5 x 10^-45 is the smallest positive value it can represent that it can represent and 3.4 x 10^38 is the biggest positive value)
we would have the following possible layouts for the 4 bytes this number would occupy:
0 00000000 00000000000000000000000 = 0
0 00000000 00000000000000000000001 = 1.5 x 10^-45
......
0 11111110 11111111111111111111111 = 3.4 x 10^38
0 11111111 00000000000000000000000 = Infinity
0 11111111 xxxxxxxxxxxxxxxxxxxxxxx = NaNs
Which should give us 2139095037 numbers inbetween the two.
I'm trying to guess what your question really is about. 1.4E-45 is approximately the smallest number (sometimes known as epsilon) that can be represented in an IEEE 754 single. The largest number is approximately 3.4E38. A single is on a computer stored in a 32 bit value and one bit used for the sign. This leaves 31 bits to represent the numbers from epsilon to the maximum value. If we assume that all possible 31 bit numbers result in a valid single then the answer to your question is 2^31 or 2,147,483,648. As it has been pointed out this assumption is not correct as some values are Not a Number or NaN.
You can read more about floating point numbers on Wikipedia
Of course, this can be done programmaticaly, for any two float numbers in general. A "lexicographic index" is the ordered index of a float number, available among other things because IEEE 754 was designed in such a way to make it easy to produce.
The basic rule is, for any two floats, if (float1 > float2)
then (lexIndex1 > lexIndex2)
.
So calculating the number of IEEE 754 numbers between is a matter of subtracting the lexicographic indexes of the two numbers:
public class FloatUtil
{
public static uint ToLexicographicIndex(float value)
{
//transfer bits to an int variable
int signed32 = BitConverter.ToInt32(BitConverter.GetBytes(value), 0);
uint unsigned32 = (uint)signed32;
//(0x80000000 - unsigned32) returns
//appropriate index for negative numbers
return (signed32 >= 0)
? unsigned32
: 0x80000000 - unsigned32;
}
public static uint NumbersBetween(float value1, float value2)
{
if (float.IsNaN(value1) || float.IsInfinity(value1))
{
throw new ArgumentException("value1");
}
if (float.IsNaN(value2) || float.IsInfinity(value2))
{
throw new ArgumentException("value2");
}
uint li1 = ToLexicographicIndex(value1);
uint li2 = ToLexicographicIndex(value2);
//make sure return is positive
return value1 >= value2 ? li1 - li2 : li2 - li1;
}
}
And of course, usage in this case:
uint result = FloatUtil.NumbersBetween(1.5e-45f, 3.4e+38f);
In this case, the result is 2139081117
for these numbers in C#, since the 3.4e+38f
constant expression does not compile into the maximum of the float range. However, using float.MaxValue
(3.40282347E+38
) as the second number gives us the expected number, 2139095038
.
There are always uncountably many numbers between any two numbers.