novacrazy / dual_num

Dual Number library for Rust
https://docs.rs/dual_num/
17 stars 4 forks source link

Bad `is_zero`? #25

Open benruijl opened 5 years ago

benruijl commented 5 years ago

I think is_zero should only check the real part instead of all the components:

    fn is_zero(&self) -> bool {
        self.iter().all(|x| x.is_zero())
    }
novacrazy commented 5 years ago

After some thought on this, I consider the current implementation to be correct.

The most obvious reason is that x.is_zero() should be true if and only if x == T::zero(). The methods of the Zero trait are reflexive.

Furthermore, for a multidimensional value such as with dual/hyperdual numbers, .is_zero() could be interpreted to mean it has a total magnitude of zero, for which the current implementation should also be sufficient.

It's simple enough to do x.real().is_zero() only when necessary.