Get position of x between two numbers?

I have a problem and I’m not sure how to solve it.
I need a function two give me a position of a number between two other numbers.
So for example,

function n(top, bottom, x)
    --return decimal

print(n(150,100,125)) -- expected output: 0.5. because 125 is halfway between 100 and 150

You should get the difference of the top and bottom and top and x, and divide them

return (top - x) / (top - bottom)
1 Like

So if i chose top= 100 bot=1 x=7
Output = 0.07?

Whoop, I made I mistake. I should’ve tested it with a number that isn’t exactly half. The first part should be x - bottom

return (x - bottom) / (top - bottom)

Yeah now it works
(X - bottom) / (top - bottom)


Another one

Also i advise you to do

If x < bot or x > top then
Print(“error, select a valid number”)