simba icon indicating copy to clipboard operation
simba copied to clipboard

Add more methods from `num_traits::Float`

Open jannschu opened this issue 5 years ago • 3 comments

There are a couple of occasions where I regularly have to add an additional num_traits::Float bound.

Methods I need from num_traits::Float include the following:

  • min_positive_value
  • is_nan
  • nan
  • infinity and neg_infinity
  • max and min with the NaN semantics
  • copysign

Is the RealField suitable for these methods or is it intended to be more abstract, i.e., it not necessarily represents these floating point semantics?

jannschu avatar May 03 '20 07:05 jannschu

For what it is worth, here is what I am using for is_nan to avoid adding this bound:


#[inline]
fn is_nan<R: RealField>(x: R) -> bool {
    let zero = R::zero();
    if x < zero {
        false
    } else {
        if x >= zero {
            false
        } else {
            true
        }
    }
}

astraw avatar Nov 08 '20 10:11 astraw

same but more succinct

#[inline]
fn is_nan<R: RealField>(x: R) -> bool {
    x.partial_cmp(&R::zero()).is_none()
}

lelongg avatar Apr 14 '21 10:04 lelongg

Thanks @lelongg . Also clippy does not complain about your version.

astraw avatar Sep 22 '21 10:09 astraw