New Lua Math functions [16/5/17]


  • As some of you may already be aware, TheGamer101 had recently announced on the devforum that two new math functions arriving for the developers of ROBLOX to use in their creations.

    What are these functions?

    The two new functions are Math.Sign, and Math.Clamp. These are already things you can do in Lua with the aid of math knowledge, although they provide a simpler, cleaner & much more efficient way of presenting them.

    Below are examples of how they work:

    math.clamp:

    math.clamp(number x, number min, number max)
    
    • If min is greater than max, an error is thrown indicating that min must be less than max
    • If x is less than min, it returns min
    • If x is greater than max, it returns max
    • If any argument is missing, nil, or otherwise not a number, an error will be thrown stating this
      Otherwise, it returns val

    math.sign:

     math.sign(number x)
    
    • Returns 1 if x is greater than 0, -1 if x is less than 0, or 0 if x is 0.

    Please be aware that I do not take credit for any of this & intend for this to be purely informational for those that don't go onto the devforum often.

    -TeaAndCopy


  • @sunwatt Where would you use these?


  • @kingdom5 math.sign isn't all too useful, but the clamp function is. I used to find myself always recreating that function, and now I won't have to.


  • @kingdom5 I use sign all the time simply to manipulate random numbers. Like with a unit vector. I used math.abs(num)/num didn't even realize it would error with 0 D:


  • @cabbler It just returns 0 with 0.

    That's useful I guess.


  • They're not major additions to LUA although it will help to keep your code cleaner & save time.


  • @sunwatt I think you mean Lua.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.