Convert linear range to another range, maintaining ratio, but without knowing the original minimum and maximum

So, I am working with a dataset that contains a variable which can have a value of various ranges that I am unable to predetermine. The range is a value that can range from -x to x, with the median at 0, and x could be a range such as -15 to 15, or it could be -1 to 1. The data I receive is more based on the deviation from 0 than from the range min to the range max, and I believe this is vital to the formula.

I am trying to convert the range of -x to x, to be either in the range of 0 to 100, or -1 to 1, all while maintaining the original ratios. I have taken a look at this stack overflow question: “Convert a number range to another range, maintaining ratio” but the formulas there require a known and predetermined range min and max to start with. Math is certainly not my strong suit and I am at a total loss of how to convert this data, so any help here would be greatly appreciated.

If it does help any, the data I receive is in a list/array, something like:

x:{ 1.47f, 1.21f, 0.49f, -0.05f, -0.69f, -1.8f, -2.35f, -1.2f, -0.42f, 4.56f }

It sounds like you’re just looking for Mathf.Lerp and Mathf.InverseLerp (aka linear interpolation and inverse linear interpolation). Lerp maps 0-1 to any other range while InverseLerp maps any range to 0-1. By combining the two, you can easily map any range to any other range:

var t = Mathf.InverseLerp(oldMin, oldMax, value);
value = Mathf.Lerp(newMin, newMax, t);

The math behind it is not too complicated:

Lerp(a, b, t) = (1 - t) * a + t * b
InverseLerp(a, b, v) = (v - a) / (b - a)

IMO the best way to understand why this works is to check the following requirements by inserting the values in the formulas above:

When t = 0, Lerp must be a.

When t = 1, Lerp must be b.

When v = a, InverseLerp must be 0.

When v = b, InverseLerp must be 1.


After reading your question again, what exactly do you mean by “without knowing the original minimum and maximum”? You know the values, right? So what’s stopping you from determining the minimum and maximum?

var min = Mathf.Min(array);
var max = Mathf.Max(array);