Fix the glm::sign function to match its documentation.

Fix #422.
This commit is contained in:
sebcrozet 2018-10-04 21:04:37 +02:00 committed by Sébastien Crozet
parent e2736caff6
commit 87c97497d2
1 changed files with 24 additions and 2 deletions

View File

@ -317,10 +317,32 @@ pub fn round<N: Real, D: Dimension>(x: &TVec<N, D>) -> TVec<N, D>
// unimplemented!() // 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<N: Number, D: Dimension>(x: &TVec<N, D>) -> TVec<N, D> pub fn sign<N: Number, D: Dimension>(x: &TVec<N, D>) -> TVec<N, D>
where DefaultAllocator: Alloc<N, D> { where DefaultAllocator: Alloc<N, D> {
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`. /// 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`.