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

Make BigDecimal's comparisons exact #13554

Merged
merged 4 commits into from
Jun 18, 2023

Conversation

HertzDevil
Copy link
Contributor

Conversions from BigRational | Float to BigDecimal are inexact, but conversions from BigDecimal | Float to BigRational are exact, so the latter ones are used to implement #<=>.

Comparisons between BigDecimal and Float::Primitive now respect infinities and NaNs.

src/big/big_decimal.cr Outdated Show resolved Hide resolved
@straight-shoota straight-shoota added this to the 1.9.0 milestone Jun 15, 2023
@straight-shoota straight-shoota merged commit 3d64266 into crystal-lang:master Jun 18, 2023
50 checks passed
@Sija
Copy link
Contributor

Sija commented Oct 12, 2023

This PR broke the simple 1.1.to_big_d == 1.1 comparison, see https://github.com/crystal-money/money/actions/runs/6497907757/job/17648045178

@HertzDevil
Copy link
Contributor Author

That's because 1.1 is indeed not equal to 1.100000000000000088817841970012523233890533447265625. This is not a bug, use be_close or something similar

@straight-shoota
Copy link
Member

straight-shoota commented Oct 13, 2023

Hm, I suppose the confusing part of this is that 1.1.to_big_d is not an exact conversion of the fully precise floating point value but rounds to a nice decimal number approximation.

@HertzDevil HertzDevil deleted the bug/bigdecimal-cmp branch October 17, 2023 11:38
Blacksmoke16 pushed a commit to Blacksmoke16/crystal that referenced this pull request Dec 11, 2023
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants