new function math.avg - #226
Conversation
I would like to request for a new function inside the math library math.avg takes in vardiac of numbers and return the average of them. This request can be compensated for if [math.sum](luau-lang#225) instead is implemented. More info inside the md file.
|
|
||
| The speciality about this function is the arguments, | ||
| it only takes one vardiac argument and automatically | ||
| calculates the average of it. |
There was a problem hiding this comment.
what if it is called without arguments? exception? 0?
There was a problem hiding this comment.
Should throw an exception imo, so that it has similar behaviour with the other variadic math functions, min and max.
There was a problem hiding this comment.
i would think it should return 0 personally (likely to be used with table.unpack, exception big footgun) but additionally it should be typed (number, ...number) if it is gonna throw an exception
There was a problem hiding this comment.
I have wrote down this concern and analyzed the possible outcomes so it should be more clear how the function should act under unexpected moments.
|
Any news on this? |
not yet with class sytax and new luau syntax they are quite busy |
|
in the future please include a rendered link, you can see examples of them in other rfcs |
| 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 | ||
|
|
There was a problem hiding this comment.
we should probably address potential accuracy gains - a naïve implementation like this will encounter floating point precision error, and a built-in library function has to
A: address that in motivation
B: decide how it should be implemented in the design
There was a problem hiding this comment.
Hard request so i had to google some methods and wrote them down in the commited file and in my opinion a balanced one should be sufficient.
But those optimizations are mostely in c++ and i don't know much about c++ so i just added something for the motivation tab.
|
|
||
| local total= 0 | ||
|
|
||
| for _, num in {string.byte("123", 1, 3)} do |
There was a problem hiding this comment.
we could come up with a better example use case than averaging the bytes present in a string
There was a problem hiding this comment.
Good point but i can promise i had to use that before anyways i commited the new changes.
is it enough for text based examples or should i replace the string.bytes example.
|
|
||
| ## Alternatives | ||
|
|
||
| Not implementing this will result in user-defined alternatives and This would keep the same problems as before. |
There was a problem hiding this comment.
What problem? You barely if at all describe a problem in the RFC body. The problem appears to be that it's code you write.
There was a problem hiding this comment.
Yes thanks, thats the only problem i can think of.
I changed the line.
Updated documentation for the math.avg function, including its purpose, design considerations, and potential drawbacks. Added examples and suggestions for handling edge cases.
rendered
I would like to request for a new function inside the math library math.avg takes in vardiac of numbers and return the average of them.
This request can be compensated for if math.sum is implemented.
More info inside the md file.