Calculate percentage of number between min and max

divisibilityfractionspercentagespython

Apologies that this is going to be a simple question for mathematicians, but i'm struggling to write some Python code because I can't work out the maths on paper.

Basically I have a function where I want to pass in a number between 0 and 10 and get the X/10 of the values between a minimum and maximum.

For clarity, i currently have (which is wrong):

getValueOutOfTen(min, max, betweenOneAndTen):
    return (max/betweenOneAndTen)

So if I have a min of of 0 and a max of 100 my function would easily be:

getValueOutOfTen(0, 100, 10) = 100
getValueOutOfTen(0, 100, 0) = 0
getValueOutOfTen(0, 100, 1) = 10

Great. But if I have

getValueOutOfTen(20, 100, 0) = Actual 0 ... I want it to be 20
getValueOutOfTen(20, 100, 1) = Actual 10 ... I want it to be 28?
getValueOutOfTen(20, 100, 10) = Actual 100 ... I want it to be 100

What's the formulae I need to use in my function?

I've tried things like:

((max - min) / 10) * betweenOneAndTen + (max - min)

which is clearly wrong.

Thanks

Background:

The reason for this function is that it controls a motor, of which there are high and low values for the PWM control. These high and lows are different per different motors. I want the user (actually another program) to be able to simply control that motor by saying give me 1/10th of the speed, or give me 9/10ths of the speed rather than having to pass in the exact value. In my example I show passing in the min and max but in reality these values will be outside of the function.

Best Answer

If $m$ is the minimum, $M$ is the maximum, and $t$ is the number of tenths you want, it appears you want $m+\frac t{10}(M-m)$. This gives $m$ when $t=0$ and $M$ when $t=10$.

Related Question