Add math.lerp function

I very frequently need to use a simple lerp function and am constantly rewriting it. It would be very useful if this was built into the math library with math.lerp

function math.lerp(A: number, B: number, D: number): number
	return A + ((B - A) * D)
end

image

56 Likes

Why? This is such a useless feature request. It barely takes half a minute to write…

1 Like

You could say the same about math.min, math.clamp, math.max, and many other math operations. It isn’t about how easy it is to write, it’s about using basic math operations without needing custom functions.

40 Likes

100%

We use so many useful functions that aren’t very complicated. A key principle of scripting is DRY, and I highly believe that, as you said, built-in functions for basic math operations shouldn’t be considered useless.

Look at how many people thought it already existed by googling

"math.lerp" site:roblox.com

7 Likes

PLEASE!!! I have to write this function constantly everywhere!
It would be a massive convenience to have this

1 Like

There’s already an RFC for Luau on this, along with math.map (which should be arriving soon).

9 Likes

Math, min max and clamp are useful because making separated functions for them is more difficult than writing 10 letters. Like for min and max you need to loop through the table and for clamp you need to use mon and max so clearly mot as super easy as lerping. Lerping is also less used so it further adds on to the uselessness of math.lerp.

1 Like

just because some people don’t use it often doesn’t make it useless, i use lerp functions all the time, way more than probably any math library call besides sin/cos; math.rad and math.deg can also be written in like 7 chars but it’s still a useful function

10 Likes

I still find it useless. I barely see people ending up using lerp. I do use it sometimes but it’s not like I want a dedicated function for it. I actually end up using sin/cos, rad/deg more! Maybe because I am focused on CFrame manipulation more?? Or something?

Also lerping would only be like useful to just lerp some number!

Also you may wonder why I am so against it? Well because the roblox platform is already so limiting that I think they should focus on adding more bigger stuff than adding this extension.

1 Like

You can make min, max, and clamp in around 10 letters too, so I don’t know what this point is aimed at.

It isn’t less used though?

Adding one function for a math operation isn’t going to ruin their addition of “bigger stuff.”

3 Likes

You’ll have more luck creating an RFC (Request for comments) on the Luau RFC repository. This will get you directly in contact with those in charge (The Luau team).

On the repository you can see the precedent of adding math.map in docs/function-math-map.md.

Maybe I am wrong but the simplest method I see is:

local function Min(...)
	local minNum = math.huge
	for _,num in {...} do
		if num < minNum then minNum = num end
	end
	return minNum
end

I assume you would use it like:

if x < y then
print("x is smaller")
end

But if we talk about the use of math.min then it’s not a mere operation to be done in “10 characters”.

In my case and people I see around, it is. Maybe for a group of people it is, but for me it isn’t.

It could slow it a bit? I could be wrong about it but just saying that they should focus on fixing the bajillion bugs with studio and the website they have rather than adding minuscule QOL features.

Your method is correct for multiple numbers but I was thinking of a traditional two-number input. Apologies for the misunderstanding.

I don’t believe it would, especially since it’s one function. Even with the addition of all the current Luau libraries, the performance of code has not been affected negatively.

There are multiple teams of engineers within Roblox. The team that works on the studio app and the team that works on the website is not the team that works on the Luau codebase. That’s why I believe adding this function shouldn’t redirect focus of Studio and website matters.

2 Likes

Yeah it’s fine. I wanted to replicate the flexibility of the function. For a traditional two or three number system using “<” and “>” would be more than enough.

I meant as in for the engineers. Would slow down the progress on up-coming features by a bit.

Ohhh, that makes a lot of sense! I didn’t really knew that. Sorry for that. I guess you win on this then. I thought there was only a single engineering team, seeing how slow roblox is with most stuff.

2 Likes

I don’t know if this works for every case, but you can also do this:

local function math_min(...: number): number
   local t = {...}
   table.sort(t)
   return t[1]
end

print(math_min(1, 2, -3, 4, 5, -6, -99, 87)) --> -99

@awry_y hates cool roblox features

2 Likes

That’s a clever method of doing so.

No, I just don’t find this feature as cool and needed as people think it is. ¯\_(ツ)_/¯

1 Like

I used to have this Bookmarked:

, but I’ve written the exact same expression so many times that I’ve completely memorised it. I really dislike having to write “utility” functions that have nothing to do with the rest of my code, I agree this should be in the language.

1 Like

My reason for wanting this isn’t to cut down on the utility functions I need to write but rather to abstract away any potential optimisations over the engine. I use lerping a lot in my code and, as such, need the most performant solution. Not only would moving the function over to the C-side likely result in natural optimisations but it would also allow the engine developers, who are likely more experienced in optimisation than many developers, to implement those optimisations for us.