I need help for math calcul (ex : 2000 to 4000 into 0 to 1)

Hello i’m Natchxel and i resquet help because i am really really stuck on my code.

  1. i try to do math for do exemple: 2000 to 4000 into 0 to 1 that will made result 3000 into 0.5

  2. the problem all i try do not work, that always start with -0.4 and end with 0

  3. i try over and over but i don’t get the result i want

what i try is sometime like (Position-Start) / (Goal-Start ) * 1

Normal Video :

SlowedVideo : (Mute the audio or you ear died)

Make it like this:

local number = 3000
local maxnumber = 4000
local minnumber = 2000
local sub = (number/minnumber) - 1 -- Comments: (3000/2000) - 1 = (1.5) - 1 = 0.5

If case of 4000, 2000

local number = 4000
local maxnumber = 4000
local minnumber = 2000
local sub = (number/minnumber) - 1 -- Comments: (4000/2000) = 2 = 2- 1 = 1
local number = 3000
local maxnumber = 4000
local minnumber = 2000
local sub = (number/minnumber) - 1 -- Comments: (2000/2000) - 1 = 1 - 1 = 0

Where is this 2000 and 4000 coming from? Is it the time position and end time position of a note in milliseconds?

The solution may be to take the lerp equation, a + (b - a) * d, and solve for d, which is the percent which is between 0 and 1.
Otherwise, you should look up how to scale numbers between ranges.

You are looking for a scaling function. So for the math, you can do something like this:

local function numberScale(low, high, number, factor)
	return ((number - low) / (high - low)) * factor
end

This does not check for the signs of the numbers or if numbers are out of a range. But the way it works is that you have to subtract the low end of the range from the high number and the number in question. Then you divide the results. What you are looking at is a percentage. If you want 0-1, then you set factor to 1. The factor is included if you want to scale numbers to a range other than 0-1. You could do 0-2 which would mean that 3000 in the interval of [2000, 4000] would result in 1.

1 Like
local function Map(number, inMin, inMax, outMin, outMax)
	return (number - inMin) * (outMax - outMin) / (inMax - inMin) + outMin
end

print(Map(3000, 2000, 4000, 0, 1)) -- 0.5

Hello :wave: and sorry for the late reply, I was eating. to start I wanted to say thank you to all of you for trying to help me and specifically thank you to @Maelstorm_1973 I tried each of your solutions but the only one that works perfectly for the moment is the one made by Maelstorm_1973, after that it may be me doing something wrong with Blueberry and Woodx’s solution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.