I'm writing a computer program with a user interface that contains sliders with differing ranges. The range can be only positive, e.g. [1, 2]
, or contain negative values, e.g. [-1,1]
.
So I need to be able to calculate the percentage of a number inside of a range, where the numbers can be negative or positive.
For example I can have a range [-127, 127]
, and if the value is 0
, it would be 50%
.
Another example using only positive numbers would be [0, 127]
, where 0
would be 0%
, but 63.5
would be 50%
.
I would also like to be be able to calculate a number on a range from a percentage, so I think this would be the inverse.
I've been able to write functions that work for example 1 or 2, but not both. Introducing the negative numbers seems to add a lot of complexity (at least or me!)
Many thanks.