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

Add decimal support #305

Open
MarkSwanson opened this issue Apr 26, 2022 · 8 comments
Open

Add decimal support #305

MarkSwanson opened this issue Apr 26, 2022 · 8 comments

Comments

@MarkSwanson
Copy link

I need to add quantum mechanics into the mix. Planck's constant is 6.62607015 × 10−34 joule second.
I've looked at a few arbitrary precision libraries but I'm not sure which one to recommend. I like 'scientific' because it parses e notation. The bigdecimal crate also seems like it would work.

Thanks!

@MarkSwanson
Copy link
Author

MarkSwanson commented Apr 26, 2022

Fyi f64 supports an accuracy of ~= 1.0 x 10 ^ 16. Folks doing scientific work need around 1.0 x 10 ^ 40.

@iliekturtles
Copy link
Owner

Does the bigrational (num::BigRational) underlying storage type meet your needs?

If not, I think the next step would be to review arbitrary precision libraries and figure out which to support.

@MarkSwanson
Copy link
Author

I tried BigRational but it just doesn't fit - I can't always specify my numbers as a fraction. Of the various arbitrary precision libraries out there the num-decimal crate provides some simple functionality that seems to work. Not sure of what limitations I'll face yet.

@iliekturtles
Copy link
Owner

A new decimal underlying storage type would be needed. See #287 for a similar effort to add complex numbers. I started a review of options. 3 year old summary: https://www.reddit.com/r/rust/comments/a7frqj/have_anyone_reviewed_any_of_the_decimal_crates/

Most popular options:

https://crates.io/crates/bigdecimal
https://crates.io/crates/rust_decimal
https://crates.io/crates/decimal

Less popular options:

https://crates.io/crates/fraction
https://crates.io/crates/rug
https://crates.io/crates/decimal-rs

@MarkSwanson
Copy link
Author

The problem with most of those (I'm not familiar with rug) is that the number type they provide does not support Copy.
Here's why it's bad, and how I worked around it with another unit/quantity crate:

mamrhein/quantities.rs#1 (comment)

Maybe this would be an easier / more ergonomic approach for uom as well.

@iliekturtles
Copy link
Owner

See #38 for implementing operators for references.

@MarkSwanson
Copy link
Author

I took a stab an implemented support for f256 using the quad library
[email protected]:MarkSwanson/uom_num_decimal.git branch=qd_f256

cargo test shows 67 warnings.
It breaks f64 and f32 a tiny bit. I left it like that as it's just a proof of concept.

Also, I'm unable to get even a simple example (examples/qd.rs) to work so it obviously doesn't work.

Any thoughts on why qd.rs gets so many errors?

@iliekturtles
Copy link
Owner

A number of your changes are to the existing f32 or f64 code. While you might be able to add Quad to the existing storage_types! { types: Float; ... } blocks, you'll probably need new blocks. Once you've done that feel free to open a PR and I can run the full test suite or if you could paste specific commands you're running and the first few errors I can review in more detail.

@iliekturtles iliekturtles changed the title Please support more precision Add decimal support Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants