2

I am working on program that requires me to iterate through all single precision floating point (23 fractions bits) numbers in the range of [1,2). I am not quite sure how to go about this. I am writing this program in C#.

If someone could give me some help with this, that would be awesome. Thank you!

  • Start with a handy reference like [this](https://msdn.microsoft.com/en-us/library/0b34tf65.aspx). Convert as Dmitry suggests and separate out the mantissa and exponent. Increment the mantissa. (Overflow will require updating the exponent.) Put the pieces back together and reverse the conversion. – HABO Sep 20 '15 at 20:55
  • @HABO is making this a little more complicated than it needs to be. As long as you stay in the positive finite number range, you can get the next float by integer increment of the bit pattern. The entire range [1,2) uses the same exponent. For other ranges, the natural integer carry would increment the exponent as needed.. – Patricia Shanahan Sep 20 '15 at 21:20

1 Answers1

2

You could use the BitConverter static class to convert float value to int and back. Thus you can access its bits.

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
}
Dmitry
  • 13,797
  • 6
  • 32
  • 48