Linear algebra library for Rust.
Go to file
Christopher Durham f578181351 Fix glm::is_normalized epsilon
The existing comparison bound of $\epsilon^2$ is improperly scaled for
testing an epsilon of the squared vector magnitude. Let $\epsilon$ be
our specified epsilon and $\delta$ be the permissible delta of the
squared magnitude. Thus, for a nearly-normalized vector, we have

$$\begin{align}
\sqrt{1 + \delta} &=  1 + \epsilon        \\
          \delta  &= (1 + \epsilon)^2 - 1 \\
          \delta  &=  \epsilon^2 + 2\epsilon
\text{ .}\end{align}$$

Since we only care about small epsilon, we can assume
that $\epsilon^2$ is small and just use $\delta = 2\epsilon$. And in
fact, [this is the bound used by GLM][GLM#isNormalized] (MIT license)
... except they're using `length` and not `length2` for some reason.

[GLM#isNormalized]: b06b775c1c/glm/gtx/vector_query.inl (L102)

If we stick an epsilon of `1.0e-6` into the current implementation,
this gives us a computed delta of `1.0e-12`: smaller than the `f32`
machine epsilon, and thus no different than direct float comparison
without epsilon. This also gives an effetive epsilon of `5.0e-13`;
*much* less than the intended `1.0e-6` of intended permitted slack!
By doing a bit more algebra, we can find the effective epsilon is
$\sqrt{\texttt{epsilon}^2 + 1} - 1$. This patch makes the effective
epsilon $\sqrt{2\times\texttt{epsilon} + 1} - 1$ which still isn't
*perfect*, but it's effectively linear in the domain we care about,
only really making a practical difference above an epsilon of 10%.

TL;DR: the existing `is_normalized` considers a vector normalized if
the squared magnitude is within `epsilon*epsilon` of `1`. This is wrong
and it should be testing if it's within `2*epsilon`. This PR fixes it.

For absence of doubt, a comparison epsilon of $\texttt{epsilon}^2$ is
correct when comparing squared magnitude against zero, such as when
testing if a displacement vector is nearly zero.
2024-01-13 00:02:26 -05:00
.github Remove CUDA support relying on abandoned toolchain 2024-01-11 23:09:17 -08:00
benches Use std::hint::black_box consistently. 2023-08-14 11:15:57 +07:00
examples docs: Fix unbalanced backticks. 2023-12-10 14:04:53 -08:00
nalgebra-glm Fix glm::is_normalized epsilon 2024-01-13 00:02:26 -05:00
nalgebra-lapack Fix and clarify license in crate metadata and add missing license files 2023-10-24 18:33:53 +02:00
nalgebra-macros Fix and clarify license in crate metadata and add missing license files 2023-10-24 18:33:53 +02:00
nalgebra-sparse Fix Clippy Warnings (#1300) 2023-12-16 13:54:38 -08:00
src Remove CUDA support relying on abandoned toolchain 2024-01-11 23:09:17 -08:00
tests Make OPoint call T::fmt to respect formatting modifiers (#1336) 2023-12-20 14:42:54 -08:00
.gitignore .gitignore: add proptest-regressions to the ignored list. 2021-02-28 18:40:33 +01:00
Cargo.toml Remove CUDA support relying on abandoned toolchain 2024-01-11 23:09:17 -08:00
CHANGELOG.md Remove CUDA support relying on abandoned toolchain 2024-01-11 23:09:17 -08:00
clippy.toml Fix some warnings 2021-07-27 19:24:55 -04:00
LICENSE Switch license to Apache v2.0 + update sponsor link. 2020-08-19 19:48:16 +02:00
README.md Release v0.32.3 2023-07-09 12:06:38 +02:00
rustfmt.toml Fix some warnings 2021-07-27 19:24:55 -04:00

crates.io

Build status crates.io

Users guide | Documentation | Forum


Linear algebra library for the Rust programming language.