rust-num / num-rational

Generic Rational numbers for Rust
Apache License 2.0
142 stars 50 forks source link

easier interop with floats #85

Open benmkw opened 3 years ago

benmkw commented 3 years ago

Can TryFrom be implemented as well as FromPrimitive? This would make it easier to make conversions using .try_nto().

I would further propose to allow performing arithmetic between rationals and floats although this would mean that From would make more sense than TryFrom and errors would panic instead of returning an error.

cuviper commented 3 years ago

TryFrom seems fine. It will need to be tested for compiler support, but you can see how num-bigint did that.

I think arithmetic between rationals and floats is a lot more problematic, so I'd rather leave that to explicit conversions.

benmkw commented 3 years ago

Ok I see seems like I should be able to basically do exactly the same using cfg attributes and just forward to the FromPrimitive methods. The Results Error type would be empty though as they only return an Option.

benmkw commented 3 years ago

I added methods for converting from ints as well (some require try_from/ some can be done through from as they should never fail). In some generic code I'm currently using them as they make it a bit less noisy.

Maybe you can take a look and say if this is too much or seems appropriate?