-
Notifications
You must be signed in to change notification settings - Fork 185
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
Calculation order affect result #561
Comments
Unfortunately, this is expected behavior in the current implementation of Rust Decimal due to each operation being evaluated, feeding the result into the next one. Effectively, without brackets order of operations applies at the The current workaround is to use brackets. In this particular example the first assertion is equivalent to the All that said, brackets won't solve everything. For example: println!("{}", dec!(1) / dec!(3) * dec!(3));
println!("{}", (dec!(1) / dec!(3)) * dec!(3)); Due to operator evaluation we will get println!("{}", dec!(1) * dec!(3) / dec!(3)); // Outputs 1 The longer answer which has been discussed though yet to be implemented, is to delay evaluation until absolutely required instead of performing an evaluation each operation (e.g. #511 (comment)). This would be useful in many different scenarios - in particular when performing mathematical functions. My initial thinking is that this would use an intermediary format to be used to maintain maximum precision (i.e. perhaps a ratio)- all in all, it's an experiment I want to take a look into when we start work on v2 of the library. Anyway, does this help at all? If I've parsed your question incorrectly, please let me know! |
@z-Wind That's the definition of 28-digit precision. It means that the 29th digit does not need to be correct. Technically speaking, it can even come up with a different number for the same calculation; it won't be deterministic, but it will be correct to the set precision. Therefore, truncate the number to the max precision before you do any comparisons. A more technical explanation: There is not enough bits in order to represent 0-9 fully for the 29th position (if I remember correctly, the digits 8 and 9 cannot be represented). So it depends on luck whether any intermediate calculation results in an ending 8 or 9. |
Thanks a lot. |
I guess that the precision of 28 causes that result.
Are there any methods to avoid this?
The text was updated successfully, but these errors were encountered: