diff --git a/nalgebra-glm/src/common.rs b/nalgebra-glm/src/common.rs index 09812ebe..ef93b608 100644 --- a/nalgebra-glm/src/common.rs +++ b/nalgebra-glm/src/common.rs @@ -317,10 +317,32 @@ pub fn round(x: &TVec) -> TVec // unimplemented!() //} -/// Returns 1 if `x > 0`, 0 if `x == 0`, or -1 if `x < 0`. +/// For each matrix or vector component `x`: 1 if `x > 0`, 0 if `x == 0`, or -1 if `x < 0`. +/// +/// # Examples: +/// +/// ``` +/// # use nalgebra_glm as glm; +/// let vec = glm::vec3(-2.0, 0.0, -0.0, 2.0); +/// assert_eq!(glm::vec3(-1.0, 0.0, 0.0, 1.0), glm::sign(&vec)); +/// +/// let mat = glm::mat2(-0.0, 5.0, -3.0, 2.0); +/// assert_eq!(glm::mat2(0.0, 1.0, -1.0, 1.0), glm::sign(&mat)); +/// ``` +/// +/// # See also: +/// +/// * [`abs`](fn.abs.html) +/// pub fn sign(x: &TVec) -> TVec where DefaultAllocator: Alloc { - x.map(|x| x.signum()) + x.map(|x| { + if x.is_zero() { + N::zero() + } else { + x.signum() + } + }) } /// Returns 0.0 if `x <= edge0` and `1.0 if x >= edge1` and performs smooth Hermite interpolation between 0 and 1 when `edge0 < x < edge1`.