Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add powi method for Sign enum #289

Open
feefladder opened this issue Jan 23, 2024 · 1 comment
Open

Add powi method for Sign enum #289

feefladder opened this issue Jan 23, 2024 · 1 comment

Comments

@feefladder
Copy link

feefladder commented Jan 23, 2024

The multiplication method is provided, but raising a sign to an integer power is also a common use-case I think?

#[inline]
fn sign_powi<T: Integer>(sign: Sign, pow: T) -> Sign {
  if pow.is_odd() {sign} else {Sign::Plus}
}

or in an impl:

#[inline]
impl Sign {
  fn powi<T: Integer>(&self, pow: T) -> Sign {
    if pow.is_odd() {*self} else {Sign::Plus}
  }
}

I came here, because fraction crate uses Sign, which I use.

@cuviper
Copy link
Member

cuviper commented Jan 23, 2024

It's not quite that simple since there's also NoSign, but we do have an internal function for this already:

/// Help function for pow
///
/// Computes the effect of the exponent on the sign.
#[inline]
fn powsign<T: Integer>(sign: Sign, other: &T) -> Sign {
if other.is_zero() {
Plus
} else if sign != Minus || other.is_odd() {
sign
} else {
-sign
}
}

I was hesitant to add public Sign powers during the initial Pow review, #54 (comment), and I'm still not sure about it. It's not really the intention for Sign to act like a complete mathematical entity on its own. Can you explain more about why fraction would want this? It looks like they have their own Sign anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants