Math library expantion

This post is extention of my last post

motivation

I have had quite the experince with mathematical projects like (perlin noise, cryptography and statistics) now those topics are already challenging last thing you would want, is if something is missing…

So i made my last post to speak about it and thanks to a comment. I thought why not make another post not about me and my opinions, but about your experiences with roblox math challenges and what would you have wanted for the math libary to have. That would have made it easier on you.

The purpose of this is to talk about it so someone may take notice and request for it in rfcs github page i will be doing that myself but, I am not experienced with github so my request may take longer.

Requests that i have made:

function math.sum(…: number) → number

Status: Not implemented

A fundemental function who adds numbers together and returns it, very useful when the
numbers are vardiac and the values are not known in advanced.

Before:

local total = 0

for _, num in {string.byte("123", 1, 3)} do
 total += num
end

print(total) --> 150

After:

function math.sum(...: number) : number
 local total = 0

 for _, num in {...} do
  total += num
 end

 return total
end

print(math.sum(1,2,4,6)) --> 13
print(math.sum(table.unpack({5, 5, 3, 2})) --> 15
print(math.sum(string.byte("123", 1, 3)) --> 150

Pros:
globalization of math.sum no more repetition of code,
improves readability and less prone to errors,
potential optimizations.

Cons:
Introduces another function to the math library.

function math.avg(…: number) : number

Status: Not implemented

Another fundamental function that takes in number vardiacts and returns
the avarage of them all.

Before:

local total= 0

for _, num in {string.byte("123", 1, 3)} do
 total += num
end

local avg = total/#{string.byte("123", 1, 3)}

print(avg) --> 50

After:

function math.avg(...: number) : number
 -- without using math.sum function
 local total = 0

 for _, num in {...} do
  total += num
 end

 return total/#{...}
end

print(math.avg(1,2,4,6)) --> 3.25
print(math.avg(table.unpack({5, 5, 3, 2})) --> 3.75
print(math.avg(string.byte("123", 1, 3)) --> 50

Pros:
globalization of math.avg no more repetition of code,
improves readability and less prone to errors,
potential optimizations.

Cons:
Introduces another function to the math library.

etc

Question: numbers are infinite does that mean the same for operational functions like those ?

My friends those are only 2 of my most used and also requested by some members of the ecosystem.

So what are your thoughts do you think there are other useful functions that are also fundemental, that should be taken into consideration?

2 Likes

That just bloat without real justification for an implementation.
I’m 99% sure this case is already handled by native code compilation if you use buffers instead of tables, making it effectively useless.

2 Likes

I feel like they aren’t all too useful as they are trivial to write yourself. One actual thing that would be very nice to see is a function to generate random numbers in a normal distribution.

Some library methods like abs and lerp are also objectively rather trivial to write yourself and yet they are present in the first-party library. What should matter more is “how commonly is this task performed?” and at least averages is probably a fairly common thing to be doing which probably justifies it.

1 Like

That’s fair enough I guess. Although I can’t personally say I’ve ever needed to sum a ton of numbers, I could see average being useful.

actually i sugested them to add math.sum because with this function we don’t need a seperate function for average.

The idea:

local data = {1,2,3,5,7}
local avg_data = math.sum(table.unpack(data))/#data

can you clearify your opinion mabey give us some examples i can’t see how we can ditch mathimatical operations for buffers?

These take up fastcall slots for functions with trivial implementation.

do you know how many functions are inside math library which i have never ever used?

answer: too many to count.

but i also understand your point, the issue is the solution; depricate math library and remake a new one with more catagories. This solution is also ineffective since we will have a whole library depricated just to remake it till someone finds another flaw. so for now expanding the math library in this way is our best choice.

and i don’t believe a summation function could be trivial, but math.avg will be if they add math.sum;

to be honest i don’t think math.sum should be considered added into math library. Native implementations is already simple enough and can work dynamically.

For me, I would rather suggest fast trigonometry functions to be implemented to improve game performance. Such as math.sincos (compute sin and cos altogether), math.fastsin, math.fastcos and etc.

that would be quite large but if you have a good reason to convince them go ahead

this post will guide you to make your own luau request.