mamrhein / quantities.rs

Unit-safe computations with quantities.
BSD 3-Clause "New" or "Revised" License
25 stars 10 forks source link

Please consider more precision #1

Closed MarkSwanson closed 2 years ago

MarkSwanson commented 2 years ago

I'd like to use this library for some quantum mechanics work, but I'm stuck at the 18 decimal precision provided by fpdec. It would be nice if your library instead used something like the scientific crate - which seems to support arbitrary precision and scientific e notation.

Thanks!

mamrhein commented 2 years ago

Hi Mark, why do you want to use fpdec at all? As stated in the README, fpdec is targeted at typical business applications. For scientific calculations quantities.rs can be used with f64 as the type of the numerical part. Is f64 not not appropriate in your use case?

MarkSwanson commented 2 years ago

I need to add quantum mechanics into the mix. Planck's constant is 6.62607015 × 10−34 joule second. Hmmm... wait a minute. I read the exponent min/max wrong. f64 is fine! Sorry for the noise. Thanks for making quantities!

MarkSwanson commented 2 years ago

Lol. I need to stop coding after 1 AM. f64 isn't anywhere near good enough. f64 is only accurate ~= 1.0 x 10^16. Folks working on scientific apps need around 1.0 x 10^40.

mamrhein commented 2 years ago

Increasing the maximum number of fractional digits in fpdec.rs would not provide an appropriate solution, because the coefficient is stored in an i128 and thus limited to 38 decimal digits.

mamrhein commented 2 years ago

I will check the crate 'scientific'. Maybe it provides a type which can be used for the numerical part (beside Decimal and f64/f32) with reasonable effort.

MarkSwanson commented 2 years ago

I looked at a number of these libraries and tried to integrate num-decimal with quantities. After some macro wrangling I bumped up against a limitation of the current rust compiler - autorefs do not work with math ops. This is bad because none of the arbitrary precision libraries support Copy - and all of the math ops take self == doom.

I then found a couple of f256 solutions, and integrated qd with quantities to provide f256 support. I checked the changes into a branch of my fork. I'll play with it later tonight to see if it actually survives some real-world testing.

mamrhein commented 2 years ago

Hi Mark, I recently discovered the new crate 'bnum'. At the moment it provides arbitrary fixed sized integers using const generics, compatible with the native integer types. According to the README, work is underway to implement also arbitrary sized floats: "This library aims to provide arbitrary precision equivalents of Rust's 3 built-in number types: signed integers (BInt), unsigned integers (BUint) and floats. Signed and unsigned integers have been implemented and nearly fully tested, and will aim to keep up to date with Rust's integer interface (e.g. when a new method is implemented on a Rust primitive integer, this library will attempt to keep in step to include that method as well). Currently, arbitrary precision fixed size floats are being worked on but are incomplete. Most of the basic methods have been implemented but are not fully tested, and at the moment there is no implementation of the transcendental floating point methods such as sin, exp, log, etc." If you are still interested, I'll watch the progress of this crate and try to integrate a "float256" into "quantities" if possible. Best regards, Michael

MarkSwanson commented 2 years ago

I've switched to using a similar library: dimensioned. This works well with the qd crate f256 type. I'm unlikely to switch now. Thank you for thinking of my use case.

mamrhein commented 2 years ago

So I'm closing this.