-
Notifications
You must be signed in to change notification settings - Fork 82
RFC: Add vector.sqrmagnitude function #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||
| # Squared Magnitude | ||||||
|
|
||||||
Ramdoys marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| ## Summary | ||||||
|
|
||||||
| Introduce a `vector.sqrmagnitude` function to the vector library. This function will return the squared magnitude of a vector. | ||||||
|
|
||||||
| ## Motivation | ||||||
|
|
||||||
| The squared magnitude of a vector is commonly used in game development. It is used for distance checks, hitbox detection, and more. In all of these cases, the actual magnitude is unnecessary. Only the relative comparison matters. This makes the square root in `vector.magnitude` an avoidable cost. Currently, the idiomatic way to compute squared magnitude in Luau is to use `vector.dot(vector, vector).` While this produces the correct result, there are problems with this: | ||||||
|
||||||
| The squared magnitude of a vector is commonly used in game development. It is used for distance checks, hitbox detection, and more. In all of these cases, the actual magnitude is unnecessary. Only the relative comparison matters. This makes the square root in `vector.magnitude` an avoidable cost. Currently, the idiomatic way to compute squared magnitude in Luau is to use `vector.dot(vector, vector).` While this produces the correct result, there are problems with this: | |
| The squared magnitude of a vector is commonly used in game development. It is used for distance checks, hitbox detection, and more. In all of these cases, the actual magnitude is unnecessary. Only the relative comparison matters. This makes the square root in `vector.magnitude` an avoidable cost. Currently, the idiomatic way to compute squared magnitude in Luau is to use `vector.dot(vector, vector)`. While this produces the correct result, there are problems with this: |
Outdated
Copilot
AI
Feb 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the proposed signature, the parameter name is vector (vector.sqrmagnitude(vector: vector): number). This is confusing because it matches both the library name and the type name; other vector library signatures use vec/vec1/vec2 (see docs/vector-library.md). Rename the parameter to vec for consistency and readability.
| `vector.sqrmagnitude(vector: vector): number` | |
| `vector.sqrmagnitude(vec: vector): number` |
Outdated
Copilot
AI
Feb 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The design section doesn’t specify 4-wide mode behavior. vector.magnitude explicitly says it includes the 4th component in 4-wide mode (see docs/vector-library.md), and vector.dot notes its 4-wide behavior as well. Please clarify whether vector.sqrmagnitude also includes the 4th component in 4-wide mode, to avoid ambiguity for implementers and users.
| Returns the squared magnitude of the given vector. This is equivalent to the sum of the squares of the vector's components. | |
| Returns the squared magnitude of the given vector. This is equivalent to the sum of the squares of all of the vector's components (in 4-wide mode, this includes the fourth component). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The document title is "Squared Magnitude", but other function RFCs use the fully-qualified function name as the H1 (e.g.
# vector.lerp). Consider renaming the H1 tovector.sqrmagnitude(and optionally renaming the file to thedocs/function-vector-*.mdpattern) to keep the docs consistent and easier to find.