pub fn norm<T, U>(a: U) -> Twhere
T: Float,
U: QuaternionOps<T>,
Expand description
Calculate L2 norm of Vector3 or Quaternion.
Compared to dot(a, a).sqrt()
, this function is less likely
to cause overflow and underflow.
When the norm-sqrt
feature is enabled, the default
implementation is compiled with dot(a, a).sqrt()
instead.
ยงExamples
// --- Vector3 --- //
let v: Vector3<f64> = [1.0, 2.0, 3.0];
assert!( (14.0_f64.sqrt() - norm(v)).abs() < 1e-12 );
// --- Quaternion --- //
let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
assert!( (30.0_f64.sqrt() - norm(q)).abs() < 1e-12 );
// --- Check about overflow --- //
let v: Vector3<f32> = [1e15, 2e20, -3e15];
assert_eq!( dot(v, v).sqrt(), f32::INFINITY ); // Oh...
#[cfg(not(feature = "norm-sqrt"))]
assert_eq!( norm(v), 2e20 ); // Excellent!