Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numbers within a range to numbers within another range [duplicate]

Tags:

c#

math

scaling

Possible Duplicate:
Convert a number range to another range, maintaining ratio

So I have a function that returns values within 0 and 255 and I need to convert these values to something between -255 and 255 So 200 would be roughly 145, 150 would be roughly 45 and so on.. I have looked at Convert a number range to another range, maintaining ratio but the formulas there won't work. Any other formula I could use?

like image 618
Perbert Avatar asked Nov 19 '10 21:11

Perbert


People also ask

What function is used to convert the values of one range of numbers to another?

To convert a number range to another range, maintaining the ratio the simple way would be use linear conversion.

How do you convert a number into a range?

Click anywhere in the table and then go to Table Tools > Design on the Ribbon. In the Tools group, click Convert to Range. Right-click the table, then in the shortcut menu, click Table > Convert to Range. Note: Table features are no longer available after you convert the table back to a range.

How do I convert multiple cells to numbers?

Use Paste Special and Multiply Select the cells that have numbers stored as text. On the Home tab, click Paste > Paste Special. Click Multiply, and then click OK. Excel multiplies each cell by 1, and in doing so, converts the text to numbers.

How do you map a value to another value?

If you need to map or translate inputs to arbitrary values, you can use the VLOOKUP function. Since there is no way to derive the output (i.e. it's arbitrary), we need to do some kind of lookup. The VLOOKUP function provides an easy way to do this.


2 Answers

public static int ConvertRange(
    int originalStart, int originalEnd, // original range
    int newStart, int newEnd, // desired range
    int value) // value to convert
{
    double scale = (double)(newEnd - newStart) / (originalEnd - originalStart);
    return (int)(newStart + ((value - originalStart) * scale));
}
like image 147
Wim Coenen Avatar answered Oct 19 '22 07:10

Wim Coenen


Try this:

int Adjust( int num )
{
    return num * 2 - 255;
}
like image 44
Jim Fell Avatar answered Oct 19 '22 07:10

Jim Fell