Skip to content
Open
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions docs/squaredmagnitude.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Squared Magnitude
Copy link

Copilot AI Feb 28, 2026

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 to vector.sqrmagnitude (and optionally renaming the file to the docs/function-vector-*.md pattern) to keep the docs consistent and easier to find.

Suggested change
# Squared Magnitude
# vector.sqrmagnitude

Copilot uses AI. Check for mistakes.

## 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:
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

The inline code snippet includes the trailing period inside the backticks: vector.dot(vector, vector).. That makes the expression incorrect/misleading; the period should be punctuation outside the code span.

Suggested change
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:

Copilot uses AI. Check for mistakes.

First, ergonomics. The most common use case for squared magnitude is comparing the squared distance between two points, which looks like this:

```luau
local difference = vector1 - vector2
local range = 100

if vector.dot(difference, difference) < range^2 then
--Within the range.
end
```

Developers are forced to store the difference in a local variable because `vector.dot` requires two arguments, or must subtract the two vectors twice for the arguments. With `vector.sqrmagnitude`, this becomes a single inline expression:

```luau
local range = 100
if vector.sqrmagnitude(vector1 - vector2) < range^2 then
--Within the range.
end
```

Even outside of distance comparisons, it is much easier and more understandable to write `vector.sqrmagnitude(vector)` than `vector.dot(vector, vector)`.

Second, performance. When computing the squared distance between two points, the alternative `vector.dot(vector1 - vector2, vector1 - vector2)` computes the subtraction twice. A single parameter `vector.sqrmagnitude(vector1 - vector2)` avoids this redundancy. The subtraction is computed once and operated on directly. In case of `vector.dot`, even if someone stored the difference in a local, `vector.sqrmagnitude` would still potentially be faster since it is handled as a single intrinsic rather than going through the dot product way.

Third, ubiquity. Squared magnitude is widely used in code where only relative comparisons are required. Many math libraries and game engines expose it due to its frequency.

## Design

Add one new function to the vector library:

`vector.sqrmagnitude(vector: vector): number`
Copy link

Copilot AI Feb 28, 2026

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.

Suggested change
`vector.sqrmagnitude(vector: vector): number`
`vector.sqrmagnitude(vec: vector): number`

Copilot uses AI. Check for mistakes.

Returns the squared magnitude of the given vector. This is equivalent to the sum of the squares of the vector's components.
Copy link

Copilot AI Feb 28, 2026

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.

Suggested change
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).

Copilot uses AI. Check for mistakes.

## Drawbacks

Just like all the other vector library functions, it is safe to assume this will use a fastcall slot. Adding this function will also increase the size of the standard library.

## Alternatives

Do not implement the function. People can continue using `vector.dot(vector, vector)` for the same result.
Loading