garrysmod icon indicating copy to clipboard operation
garrysmod copied to clipboard

Math lib update

Open Kefta opened this issue 6 years ago • 6 comments

  • Added math.BitCount( number num ) - returns the least amount of bits needed to represent an integer in binary. This function can be used with networking indexes for shared tables in the most network-efficient manner
  • Added math.IntToString( number num, number [2, 36] base, boolean caps = false ) - this is the opposite of tonumber and will convert a decimal number to a base [2, 36] string. You can now achieve the same effect as math.IntToBin (be it a little less efficient) with this. The caps argument determines if the output string's characters should be capitalised (base 11+)
  • Added math.DistanceSqr( number x1, number x2, number y1, number y2 ) - returns the distance squared between two points (more efficient than math.Distance)
  • math.IntToBin's string return no longer has leading zeros
  • Fixed infinite loop with bad arguments to math.calcBSplineN/BSplinePoint
  • Localised global/library functions
  • Overall optimisations

Kefta avatar Apr 19 '18 13:04 Kefta

A few useful ones I use commonly; Not important, but might be useful to some. math.step - http://www.wolframalpha.com/input/?i=H(x) math.sign - http://www.wolframalpha.com/input/?i=2*H(x)-1 (except x=0 should be 0)

thegrb93 avatar Apr 19 '18 21:04 thegrb93

Those would be more efficiently implemented inline, and they're just one-liners that wouldn't save much time in typing either - math.Sign was denied for this purpose: https://github.com/Facepunch/garrysmod/pull/1229

Kefta avatar Apr 20 '18 00:04 Kefta

There's quite a few things in there that are more efficient inline. The point is that its easier to type math.sign rather than doing the inline everytime.

I guess they are too trivial to be added, but still; they make writing some things faster.

thegrb93 avatar Apr 20 '18 01:04 thegrb93

Wouldn't math.Clamp better implemented inline as well? So why is it in again?

Grocel avatar May 24 '18 19:05 Grocel

It already is inlined from LuaJIT (math.max and min at least).

Kefta avatar May 24 '18 19:05 Kefta

IntToString now checks its base's bounds like tonumber. Lua 5.2 does not error for non-integer numeral representations like 5.3, so I left the unsigned int cast.

Kefta avatar May 29 '18 15:05 Kefta