num-rational icon indicating copy to clipboard operation
num-rational copied to clipboard

easier interop with floats

Open benmkw opened this issue 4 years ago • 3 comments

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.

benmkw avatar Oct 22 '20 19:10 benmkw

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.

cuviper avatar Oct 22 '20 21:10 cuviper

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 avatar Oct 22 '20 22:10 benmkw

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?

benmkw avatar Nov 19 '20 10:11 benmkw