Skip to content

new function math.avg - #226

Open
HarmosCreations wants to merge 2 commits into
luau-lang:masterfrom
HarmosCreations:patch-2
Open

new function math.avg#226
HarmosCreations wants to merge 2 commits into
luau-lang:masterfrom
HarmosCreations:patch-2

Conversation

@HarmosCreations

@HarmosCreations HarmosCreations commented Jul 12, 2026

Copy link
Copy Markdown

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.

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.
Comment thread function-math-avg.md

The speciality about this function is the arguments,
it only takes one vardiac argument and automatically
calculates the average of it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if it is called without arguments? exception? 0?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should throw an exception imo, so that it has similar behaviour with the other variadic math functions, min and max.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@razvanpacku

Copy link
Copy Markdown

Any news on this?

@HarmosCreations

Copy link
Copy Markdown
Author

Any news on this?

not yet with class sytax and new luau syntax they are quite busy

@nnullcolumn

Copy link
Copy Markdown

in the future please include a rendered link, you can see examples of them in other rfcs

Comment thread function-math-avg.md
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

@nnullcolumn nnullcolumn Jul 26, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@HarmosCreations HarmosCreations Jul 30, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread function-math-avg.md

local total= 0

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

@nnullcolumn nnullcolumn Jul 26, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could come up with a better example use case than averaging the bytes present in a string

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread function-math-avg.md Outdated

## Alternatives

Not implementing this will result in user-defined alternatives and This would keep the same problems as before.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants