Skip to content

Make BigDecimal's comparisons exact#13554

Merged
straight-shoota merged 4 commits intocrystal-lang:masterfrom
HertzDevil:bug/bigdecimal-cmp
Jun 18, 2023
Merged

Make BigDecimal's comparisons exact#13554
straight-shoota merged 4 commits intocrystal-lang:masterfrom
HertzDevil:bug/bigdecimal-cmp

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.

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric labels Jun 12, 2023
@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
@HertzDevil HertzDevil added the breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. label Jun 30, 2023
@straight-shoota straight-shoota added kind:breaking Intentional breaking change with significant impact. Shows up on top of the changelog. and removed breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. labels Jul 4, 2023
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:breaking Intentional breaking change with significant impact. Shows up on top of the changelog. kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants