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

I want arithmetic ops which return error instead of losing precision #6

Closed
safinaskar opened this issue May 5, 2022 · 2 comments
Closed

Comments

@safinaskar
Copy link

I want lib for monetary calculations. decimal-rs sometimes lose precision, and I don't like that. For example, 1.0000000000000000000000000001 * 1.0000000000000000000000000001 is 1.0000000000000000000000000002 under decimal-rs (this is incorrect from mathematical point of view). (I used 0.1.36.) Even checked_mul gives same answer instead of returning error. I want some function (say, exact_mul and similar names for addition and subtraction), which returns Ok (or Some) when a result can be exactly represented and Err (or None) if not.

In my program (assuming my program is bug-free) such precision losing should never happen. But as we all know we can never be sure that there is no bugs! So I want this exact_mul. I would replace my checked_mul with exact_mul (with unwrap). And if I ever lose precision, I will discover this, because I will see panic

There is similar bug in rust-decimal paupino/rust-decimal#515

@safinaskar safinaskar changed the title I want arithmetic ops which never lose precision I want arithmetic ops which return error instead of losing precision May 5, 2022
@davidli2010
Copy link
Member

decimal-rs has 38 digits of precision, and the decimal will be rounded if it overflowed. This is a feature of decimal-rs and we have no plan to add exact_mul.

P.S.: pgnumeric has upto 1000 digits of precision, and 1.0000000000000000000000000001 * 1.0000000000000000000000000001 is 1.00000000000000000000000000020000000000000000000000000001.

@safinaskar
Copy link
Author

Ok, you may close the bug

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