cmpute / dashu

A library set of arbitrary precision numbers implemented in Rust.
Apache License 2.0
76 stars 9 forks source link

FBig and other dashu types should implement `num` crate traits #31

Closed TheQuantumPhysicist closed 1 year ago

TheQuantumPhysicist commented 1 year ago

The native, correct way to handle new types of floating point numbers (and integers, for that matter) is by implementing the de-facto traits of that: https://crates.io/crates/num

For example, instead of using fbig!(1), FBig should implement num::cast::FromPrimitive, where then this conversion can be done with something like FBig::from_u64(1).unwrap().

Also, FBig should implement num::Float.

It would be really nice to have these implemented, so that the crate types can be used like other floating-point numbers and integers in rust. Having special, isolated work for everything just makes it harder to integrate dashu into projects. Currently all my code that works with Float trait cannot be ported to dashu, so I have to duplicate things just for dashu to work.

cmpute commented 1 year ago

Thanks for the comments, support for FromPrimitive and ToPrimitive has been in plan for a long time and it is under development. It will probably be implemented after next major version bump, because I have to revisit the dependency structure (it's worth noting that num crates are all pre-1.0).

Regarding num::Float, I will argue that it's not the correct way to handle new types of floating point. It's not properly designed for arbitrary precision floats, it's not even satisfactory for normal floats (see the issues related to floats in num-traits, especially https://github.com/rust-num/num-traits/issues/98 and https://github.com/rust-num/num-traits/issues/178).

My advice will be creating your own traits if you want to generalize over floats. Only put the methods you need in the traits.

cmpute commented 1 year ago

Besides, I would also like to point out that the macro fbig!(1) is only intended for creating compile-time constants! It should not be used in exchange with runtime conversion methods (such as From::from() or FromPrimitive::from_u64).

TheQuantumPhysicist commented 1 year ago

Thank you for the information.

astrale-sharp commented 1 year ago

Hi, does that mean they will implement traits in num_traits then? Cause even with dashu-float = { version = "0.3.2", default-features = false, features = ["num-traits","num-traits_v02"] } in my Cargo.toml, it seems that Fbig doesn't implement num-trait::Float for instance. Am I doing something wrong?

cmpute commented 1 year ago

Hi, does that mean they will implement traits in num_traits then? Cause even with dashu-float = { version = "0.3.2", default-features = false, features = ["num-traits","num-traits_v02"] } in my Cargo.toml, it seems that Fbig doesn't implement num-trait::Float for instance. Am I doing something wrong?

I won't implement num-trait::Float for thr FBig, since that trait is somewhat incompatible with arbitrary precision floats.

astrale-sharp commented 1 year ago

Interesting! How come? (is it because of the copy bound?)

cmpute commented 1 year ago

I would like to close the issue due to the reason I explained in https://github.com/cmpute/dashu/issues/31#issuecomment-1505847223. The FromPrimitive and ToPrimitive traits are implemented in v0.4. Feel free to reopen it if you have any further questions.