14

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?

Community
  • 1
  • 1
Perbert
  • 465
  • 1
  • 7
  • 17
  • Why won't the formulas there work for you? – jball Nov 19 '10 at 21:46
  • 4
    The link supplied is really the simplest way of doing this. Can you give an example of why it is not working? – linuxuser27 Nov 19 '10 at 21:46
  • var oldRange = high - low; var newRange = newHigh - newLow; var newNumber = oldNumber * (oldRange/newRange); Is this right? If so, your math about 200 -> 145 and 150 -> 45 is off :) – Holystream Nov 19 '10 at 21:48
  • 0 - 255 is already in the range of -255 - 255...I don't see the problem. :-P – Justin Niessner Nov 19 '10 at 21:48
  • @Holystream, nope, 200 -> 145 and 150 -> 45 are correct, perhaps you're forgetting to adjust for the new minimum? – jball Nov 19 '10 at 21:52
  • jball and linuxuser27, it won't work on the first formula because (OldMax - OldMin)) + NewMin = 0 and you can't divide by 0 and the second comes as new_value=old_value/255*500, please correct me if I am wrong? – Perbert Nov 19 '10 at 21:57
  • Voting to close as duplicate because I'm struggling to see how implementing the (correct) answers to the above question in C# is not trivial. – jball Nov 19 '10 at 22:00
  • 1
    @user425291: You're reading the formula incorrectly in the other question's answer. It says to divide by (OldMax - OldMin), and THEN add NewMin. – mokus Nov 19 '10 at 22:25
  • @mokus yes, that gives you: ((255-0) + (-255)) <=> 0 – Perbert Nov 19 '10 at 22:54
  • @Spre3: I don't exactly know whether you're agreeing or disagreeing with me. Either way, I can probably be clearer myself. What I'm saying is that there is no division by zero if the formula is applied correctly. Worked out with values from this question's conversion, it comes out to: `NewValue = (((OldValue - 0) * (255 - (-255))) / (255 - 0)) + (-255)`, which simplifies to `((OldValue * 510) / 255) - 255`. It could simplify further if using floating point types, but in any case it should be clear that there is no division by zero in this expression. – mokus Nov 19 '10 at 23:02
  • also, if not using floating point types, 255 should be replaced by 256 in several places to make up for the fact that the range is not continuous, which leads to Jim Fell's (correct) answer. – mokus Nov 19 '10 at 23:05
  • In that case I guess the problem was that I missed the enclosing parentheses that make the division precede the last subtraction, thanks – Perbert Nov 20 '10 at 00:19

5 Answers5

42
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));
}
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • This is the best answer to this question as written. Note that this is simply a C# implementation of the (correct) formulas that are in [the question the OP linked to](http://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio) – jball Nov 19 '10 at 21:58
  • This is a good answer. If the value to convert is above and below the original range then how to do I clip it at min or max value of desired range. for example 'int val = ConvertRange(20, 110, 0, 100, 10);' then val is 0 'int val = ConvertRange(20, 110, 0, 100, 111); then the val is 100' – katta Jun 06 '13 at 16:18
4

Try this:

int Adjust( int num )
{
    return num * 2 - 255;
}
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
1

General solution for arbitrary range...

var val1 = 200;
var min1 = 0;
var max1 = 255;
var range1 = max1 - min1;

var min2 = -255;
var max2 = 255;
var range2 = max2 - min2;

var val2 = val1*range2/range1 + min2;
James Kovacs
  • 11,549
  • 40
  • 44
  • This is incomplete, it works because your _min1_ is 0, when it's higher _val2_ may be out of the new range... The formula is `(OldValue - OldMin) * NewRange / OldRange + NewMin` (from [python's thread](https://stackoverflow.com/q/929103/6225838)). – CPHPython Jul 11 '22 at 13:26
1
public int ConvertRange(
           int originalStart, int originalEnd,
           int newStart, int newEnd,
           int value)
{

  int originalDiff = originalEnd - originalStart;
  int newDiff = newEnd - newStart;
  int ratio = newDiff / originalDiff;
  int newProduct = value * ratio;
  int finalValue = newProduct + newStart;
  return finalValue; 

}
Aaron Hathaway
  • 4,280
  • 2
  • 19
  • 17
  • This is incomplete, to prevent the _finalValue_ being out of the new range, you need to subtract from _value_... The formula is `(value - originalStart) * ratio / originalDiff + newStart` (from [python's thread](https://stackoverflow.com/q/929103/6225838)). – CPHPython Jul 11 '22 at 13:36
0

Adjusted = original / 255 * 510 - 255

145 = 200 / 255 * 510 - 255
 45 = 145 / 255 * 510 - 255
Sir Robert
  • 4,686
  • 7
  • 41
  • 57
  • I fail to see how this will fix the signing issue. – Jim Fell Nov 19 '10 at 21:50
  • @Jim Fell, what signing issue? – jball Nov 19 '10 at 21:53
  • 1
    What's the signing issue? Plug in an original value of 1 and get -253, plug in 200 and get 145. Seems to work. – Sir Robert Nov 19 '10 at 21:53
  • @Sir Robert: It looks like the OP is wanting to rescale a given number between (0 and 255) to (-255 and 255). Your solution does not appear to address converting numbers less than 128 into the negative range. – Jim Fell Nov 19 '10 at 21:57
  • @Jim Fell - order of operations says the final `-255` happens after all other operations, correctly adjusting originally `(0 to 127)` values into the new negative `(-255 to -1)` values – jball Nov 19 '10 at 22:02
  • @Sir Robert: I see. You're solving this with ratios. – Jim Fell Nov 19 '10 at 23:40