Add math.lerp function

Theres currently a luau RFC open for math.lerp

1 Like

How is this useless, I find myself writing the same function over and over again and I would like a function that eases this need and speeds my productivity. Such a bold and thoughtless claim.

5 Likes

I agree with this request. If there’s a cframe.lerp, surely we can have a math.lerp

i’m not so sure about this one
idk what kind of code you work on but there are definitely places where lerp gets used a lot

poor reasoning
this update should take like 1-2 minutes max
what is 1-2 minutes gonna do to the bigger updates? practically almost nothing. the “tiny bit” of delay probably won’t even amount to 0.1% of the time

as for why they’re slow with big updates, it’s partially a lack of “wanting to do it” (aka procrastinating), partially a lack of idea on what they want to do,
or maybe they’re encountering errors (those things are physically painful in c and c++, they will spam multiple verbose compile errors up your face all because you forgot a semicolon)

math.map is already live in studio. It is an extension of lerp.

--format
local output = math.map(input, input_min, input_max, output_min, output_max)
--lerp
local value = math.map(alpha, 0, 1, from, to)
--example
local celsius = math.map(fahrenheit, 32, 212, 0, 100)
4 Likes

Let’s go, they need to add it to the documentation and maybe present it officially.

1 Like

Wow. This mapping will definitely simplify many scripts in the near future—sliders specifically come to mind.

Out of curiosity, how did you discover math.map?

It has existed since October 18th. Funny that it was in RFC hell for 4 weeks but was added in just two days.

math.lerp is still useful both for performance and showing intent. I’d also like to see a vector.lerp to mirror Vector3:Lerp.

Yeah I figured it would be in Roblox’s GitHub repository; I was wondering how they initially found out about it. I don’t think it was formally announced in the Forum, so is it just by checking the repository frequently you and @Prototrode discover these things?

This shouldn’t be a problem; to restore the pretty slight performance edge of a normal lerp, the math.map function can check if they’re only three arguments. If so, then

return A + ((B - A) * D)

Yeah, I agree with this. It would make more sense linguistically than “map” in certain cases where it’s only functioning as a lerp. Plus it maintains consistency with other functions using the term lerp.

They’re also mentioned in new #updates:release-notes

We just see them on the releases (that be Roblox or Luau).

Alternatively, if you’d like to be on top of everything Luau related, you can watch the repository or join the Roblox OSS Discord Server.

Friggin’ schweet update, can’t wait for math.lerp next

I literally said math.map is an extension of lerp why would we still need lerp lol
It does what lerp does and then some more

Mb, I read it incorrectly, didn’t even notice the lerp example :sob:

I don’t see math.map as a replacement for math.lerp, specifically because of the “and then some more” part of it. There’s no reason to have that extra overhead when it explicitly is not needed.

2 Likes

The way I see it, math.map automates the extra steps you would do anyways when using lerp. The lerp function accepts a scalar called alpha that is usually in the interval [0, 1]. How do people get any aribitrary value to be in that interval? They use inverse lerp first, and then use lerp again. math.map is just that; ilerp and lerp glued together. But I get it, not everyone does this, and many times you immediately decide what alpha should be in the code.

Yes, I agree, but abstracting lerp into a function will still carry an overhead. Most people use lerp repeatedly for the same input parameters where the only changing value is the alpha. Common example is tweening without using the TweenService. If you take a look at the formula for linear interpolation:

value = start + (finish - start) * alpha

The expression (finish - start) is a constant. There is no need to recompute it over and over again if start and finish are the same. So if you truly cared about performance and overheads, you wouldn’t want to turn lerp into its own function; it gives you no room to optimize it for your specific use cases. If you took the extra effort to write the entire formula out, you at least have the opportunity to precompute finish - start.

value = start + delta * alpha

TL:DR; math.map is a practical replacement for lerp, and if you care about performance, you still wouldn’t want lerp to be its own function.

math.lerp is still helpful
especially for people that don’t know their math as well or are following a tutorial of some sort and boom, an easy implementation of lerp.

What’s a bit disappointing with math.map is its roughly 10.5x slower than a native implementation (because it’s a raw VM call, not fastcalled).

Honestly, since a math.lerp function would suffer the same problem, there would be no point in adding it either. Even if they fastcalled it (highly unlikely), it still would probably perform worse.

1 Like

I really don’t understand the frustration behind this. This is a very convenient feature.

2 Likes