title: Convert/Quantize Float Range to Integer Range
body: <p>Say I have a float in the range of [0, 1] and I want to quantize and store it in an unsigned byte. Sounds like a no-brainer, but in fact it's quite complicated:</p>
  
  <p>The obvious solution looks like this: </p>
  
  <pre><code>unsigned char QuantizeFloat(float a)
  {
    return (unsigned char)(a * 255.0f);
  }
  </code></pre>
  
  <p>This works in so far that I get all numbers from 0 to 255, but the distribution of the integers is not even. The function only returns <code>255</code> if a is exactly <code>1.0f</code>. Not a good solution.</p>
  
  <p>If I do proper rounding I just shift the problem:</p>
  
  <pre><code>unsigned char QuantizeFloat(float a)
  {
    return (unsigned char)(a * 255.0f + 0.5f);
  }
  </code></pre>
  
  <p>Here the the result <code>0</code> only covers half of the float-range than any other number.</p>
  
  <p>How do I do a quantization with equal distribution of the floating point range? Ideally I would like to get a equal distribution of integers if I quantize equally distributed random floats.</p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <p>Btw: Also my code is in C the problem is language-agnostic. For the non-C people: Just assume that <code>float</code> to <code>int</code> conversion truncates the float.</p>
  
  <p><strong>EDIT:</strong> Since we had some confusion here: I need a mapping that maps the smallest input float (0) to the smallest unsigned char, and the highest float of my range (1.0f) to the highest unsigned byte (255).</p>