Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions function-math-sum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# math.sum

## Summary

``` math.sum(...: number): number ```

Add a ```sum``` function to the ```math``` library, which adds the numbers together.

## Motivation

Numerical addition is one of the most fundamental mathematical operations.
Summing values is common in game development for damage calculations, currency totals, and statistical computations.

A variadic function is particularly useful when the number of values to sum is not known in advance.

A native implementation would improve ergonomics and could also enable implementation-specific optimizations.
## Design

Requesting a new function, ```math.sum```:

```lua

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

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

return total
end
```

## Drawbacks

This introduces another function to the standard library.

Additionally, any performance improvements may not be significant enough to justify the API expansion.

## Alternatives

Continue relying on user-defined helper functions.

While this is straightforward, it results in repeated implementations across projects and misses the opportunity to provide a standard,
discoverable API for a common operation.