遍历 [1,2) 之间的单精度浮点数

Iterate through single precision floating point numbers between [1,2)

我正在开发的程序要求我遍历 [1,2) 范围内的所有单精度浮点数(23 个小数位)。我不太清楚该怎么做。我正在用 C# 编写这个程序。

如果有人可以帮我解决这个问题,那就太好了。谢谢!

您可以使用 BitConverter 静态 class 将 float 值转换为 int 并返回。因此你可以访问它的位。

int one = BitConverter.ToInt32(BitConverter.GetBytes(1f), 0);
int two = BitConverter.ToInt32(BitConverter.GetBytes(2f), 0);

for (int i = one; i < two; i++)
{
    float f = BitConverter.ToSingle(BitConverter.GetBytes(i), 0);
    // Your stuff
}