1.5 x 10 -45から 3.4 x 10 38 (IEE754 単精度浮動小数点数)までの異なる数はいくつありますか?
5 に答える
IEEE単精度浮動小数点数の範囲について話していると仮定します(1.5 x 10 ^ -45は、表現できる最小の正の値であり、3.4 x 10 ^ 38は最大の正の値です)
この数値が占める 4 バイトのレイアウトは次のようになります。
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
これにより、2 つの間に 2139095037 の数字が表示されます。
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
.
私はあなたの質問が実際に何であるかを推測しようとしています. 1.4E-45 は、IEEE 754 シングルで表現できるほぼ最小の数値 ( epsilonとも呼ばれます) です。最大数は約 3.4E38 です。シングルは 32 ビット値で格納されたコンピューター上にあり、符号には 1 ビットが使用されます。これにより、イプシロンから最大値までの数値を表すために 31 ビットが残ります。考えられるすべての 31 ビット数が有効なシングルになると仮定すると、質問に対する答えは 2^31 または 2,147,483,648 になります。指摘されているように、一部の値は非数値またはNaNであるため、この仮定は正しくありません。
ウィキペディアで浮動小数点数の詳細を読むことができます
これは実際にはプログラミングではありません。
bc は言います(整数全体の場合):
1.5*10^45
1500000000000000000000000000000000000000000000.0
3.4*10^38
340000000000000000000000000000000000000.0
1500000000000000000000000000000000000000000000.0-340000000000000000000000000000000000000.0
1499999660000000000000000000000000000000000000.0
あなたは整数を意味していると思います。また、1.5*10^45 は他の値よりも大きいため、3.4*10^38 と 1.5*10^45 の間を意味します。とにかく、答えは小さい数の場合と同じです。これらの 2 つの数値を除外する必要があると仮定します。
2から10までの数はいくつある? 答えは10-2-1=7です。確かに 3,4,5,6,7,8,9 は 7 つの数です。したがって、「式」は次のとおりです。
aとbの間にある数字はいくつ?答えはba-1
したがって、1.5*10^45-3.4*10^38 -1 = 15*10^44-34*10^37 -1 = (15*10^7)*10^37-34*10^37 -1 =( 15*10^7-34)*10^37 -1 = (150000000-34) * 10^37 -1 = 149999966 * 10^37 -1 または 14999996599999999999999999999999999999999999