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

.pow() and .root() functions for fraction types #60

Open
Gip-Gip opened this issue Jun 19, 2022 · 3 comments
Open

.pow() and .root() functions for fraction types #60

Gip-Gip opened this issue Jun 19, 2022 · 3 comments

Comments

@Gip-Gip
Copy link

Gip-Gip commented Jun 19, 2022

I'm currently writing a basic calculator with this library and I noticed at the moment there is no .pow() or .root() style functions for fractions.

Also when performing square roots should they simply be approximated or should numbers simply represent simplified square roots, popping all of the irrational roots in a vector or something like that. For example, something like this

let foo = Fraction::from(75u32);
let result = foo.sqrt();
assert_eq!(5, result.get_base_multiplicand()); // Not actually functional code just here to get a point across
assert_eq!(3, result.get_root_multipliers()[0]);
println!("{:9}", result); // -> 8.660254037

I would like to contribute I'm just unsure how as to go about tackling this problem, at the moment

@dnsl48
Copy link
Owner

dnsl48 commented Jun 21, 2022

Hi @Gip-Gip, thank you for your interest and your support!

Regarding pow, I feel like we may consider falling back to num-rational pow implementation. Fraction is built on top of Rational type already, so this may be the eastiest path forward

Not too sure about sqrt. That feels somewhat more challenging. I guess you may have a real-world problem on your hands, so we'd be happy to consider a way that would solve your current challenge.

One of the primary goals of this library is to provide lossless math, so I am not too sure if an approximation would follow that direction. However, if we explicitly call that out in the function name (e.g. sqrt_approx), I think that should be totally alright.

@Gip-Gip
Copy link
Author

Gip-Gip commented Jun 24, 2022

I'll look into experimenting with a few options over the next few days or so. A good .pow function would look something like

fn pow(left: Fraction, right: Fraction) -> Fraction {
    let exp = right.numer().unwrap().clone();
    let root = right.denom().unwrap().clone();
    
    // Insert code here where result = nthroot(left^exponent, root)

    return result
}

@feefladder
Copy link
Contributor

I've been working on a crate heavily building on top of the fraction crate for exact arithmetic. the relevant code is here
first handle special cases such as infinity or nan, then do the calculation. It's inside match statements, but should be quite copy-pasteable into this crate.

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

No branches or pull requests

3 participants