Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to correctly scale integers between intervals?

I have uint16_t integers in the range [ 0 .. 4092 ] and I need to scale them into [ 0 .. 255 ] interval.

What’s the correct formula for that?

The obvious one y = x * 255 / 4092 doesn’t work because the scaling factor is 15.988 so I need 15-16 (usually 16, rarely 15) input values mapped into every output value. With that formula the output 255 only produced for a single input value.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The less obvious formula, which uses an offset to round to nearest, y = ( x * 255 + 2045 ) / 4092, doesn’t work either because the output 255 corresponds to 9 input values [ 4084 .. 4092 ], again that’s not what I want, I want 15 or 16 input numbers mapped into each output number.

BTW, I have gigabytes of these numbers in memory, for this reason I’d rather not use floating point math for this.

>Solution :

Try multiplying and dividing by one more than you currently are. This code seems to produce your expected results:

#include <iostream>
using namespace std;
int main() {
    for (int i = 0; i <= 4092; ++i)
        cout << (i<<8)/4093 << endl; // i<<8 is equivalent to i*256
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading