You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created some implementations, but I don't want to check, which of them should be functions, methods or trait implementations.
I made all of them trait implementations for now.
If the value can not
Here are the implementations:
// Pow by ratio// I only tested this function; I use it in my codefnpow_ratio<I:PrimInt + Roots>(a:Ratio<I>,b:Ratio<u32>) -> Option<Ratio<I>>{let(an, ad) = a.into();let(bn, bd) = b.into();let pown = an.pow(bn);let powd = ad.pow(bn);let n = pown.nth_root(bd);let d = powd.nth_root(bd);// If there does not exist a rational power, return noneif n.pow(bd) != pown || d.pow(bd) != powd {None}else{Some(Ratio::new(n, d))}}// Pow by integerfnpow_integer<I:PrimInt>(a:Ratio<I>,b:u32) -> Ratio<I>{let(an, ad) = a.into();let pown = an.pow(b);let powd = ad.pow(b);Ratio::new(n, d)}// Root by integerfnroot_integer<I:PrimInt + Roots>(a:Ratio<I>,b:u32) -> Option<Ratio<I>>{// add checks if b is zero and either return none or some result for more expressivenesslet(an, ad) = a.into();let n = an.nth_root(b);let d = ad.nth_root(b);// If there does not exist a rational root, return noneif n.pow(b) != pown || d.pow(b) != powd {None}else{Some(Ratio::new(n, d))}}// Root by ratiofnroot_ratio<I:PrimInt + Roots>(a:Ratio<I>,b:Ratio<u32>) -> Option<Ratio<I>>{// add checks if b is zero and either return none or some result for more expressivenesspow_ratio(a, b.inv())}// Square rootfnsqrt<I:PrimInt + Roots>(a:Ratio<I>,b:Ratio<u32>) -> Option<Ratio<I>>{// use a version of this function without the zero check for argument broot_integer(a,2)}// Cubic rootfncbrt<I:PrimInt + Roots>(a:Ratio<I>,b:Ratio<u32>) -> Option<Ratio<I>>{// use a version of this function without the zero check for argument broot_integer(a,3)}
pow_integerand root_integer could be implemented using pow_ratio and root_ratio by supplying constant values.
The compiler should be able to optimize, at least if the functions are declared as inline.
Should all of these functions be added anyway?
The text was updated successfully, but these errors were encountered:
The pow_integer case should already be covered by Ratio::pow and the many variations of impl Pow.
I'm more hesitant about the roots for pow_ratio. Numerical functions that return an Option are usually called checked_, but that also usually refers to overflowing the type range. Maybe there's another prefix that would convey "exact roots only", but nothing comes to mind.
Sometimes people may want a close approximation instead, like √2 ≌ 99/70, as discussed in #35.
These should be easy to implement.
I created some implementations, but I don't want to check, which of them should be functions, methods or trait implementations.
I made all of them trait implementations for now.
If the value can not
Here are the implementations:
pow_integer
androot_integer
could be implemented usingpow_ratio
androot_ratio
by supplying constant values.The compiler should be able to optimize, at least if the functions are declared as
inline
.Should all of these functions be added anyway?
The text was updated successfully, but these errors were encountered: