Skip to content

Conversation

tompng
Copy link
Member

@tompng tompng commented Oct 9, 2025

BigDecimal v3.3.1

Fix for new feature BigDecimal(float)

BigDecimal(1.2)
# before: ArgumentError
# after:  0.12e1

Fix assertion of BigMath.log not to expect incorrect random digits to be appended.

BigMath.log(2, 10)
# log(2): 0.693147180559945309417232121458176568075500...
# before: 0.693147180559945309417232112588603776354688e0
# after:  0.6931471806e0

Fix assertion of too-strict precision and assertion that expecting precision to be too loose.

BigDecimal('1.2')**-1
# before: 0.833333333333333333333333333333333333333333333e0
# after:  0.83333333333333333333333333333333e0

BigDecimal(0) + 1/3r
# before: 0.333333333e0
# after:  0.33333333333333333333333333333333e0

Fix divmod, modulo and remainder assertion which was expecting a bug inconsistent with Float as a specification.

[BigDecimal(1).divmod(-BigDecimal::INFINITY), 1.divmod(-Float::INFINITY)]
# before: [[0.0, 0.1e1], [-1, -Infinity]]
# after:  [[-0.1e1, -Infinity], [-1, -Infinity]]

BigDecimal(1).remainder BigDecimal(0)
# before: NaN
# after:  ZeroDivisionError
# Float#remainder and Integer#remainder: ZeroDivisionError

Remove coerce expectation to add/sub/mult which was dropped because of a bug, and never implemented correctly in BigDecimal.

BigDecimal(2).add(0i, 0).class
# before: Calls coerce and returns Complex
# after:  Raise TypeError without calling coerce

Unlike + - *, add sub mult is required to calculate within BigDecimal and to return BigDecimal.
a.coerce(b) #=> [b2, a2] only guarantees that a2.class == b2.class. Numeric's default behavior is to return [Float, Float]. It's not suitable to convert something to BigDecimal.

@tompng tompng force-pushed the fix_bigdecimal_spec branch from 80050d8 to 0833f22 Compare October 9, 2025 17:38
it "raises ArgumentError when Float is used without precision" do
-> { BigDecimal(1.0) }.should raise_error(ArgumentError)
it "allows Float without precision" do
skip if BigDecimal::VERSION < "3.3.0"
Copy link
Member

@eregon eregon Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a version_is guard instead? (see usages of that for examples)
skip should only be used as last resort if there is no other way, i.e. skip should be avoided as much as possible.
Maybe we should doc that better for skip, how did you find skip BTW?

Copy link
Member

@eregon eregon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, it looks good in general.

@mixed.remainder(@zero).should.nan?
@zero.remainder(@zero).should.nan?
it "raises ZeroDivisionError used with zero" do
skip if BigDecimal::VERSION < "3.3.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use version_is instead, and same for other usages of skip

Fix for new feature BigDecimal(float).
Fix assertion of BigMath.log not to expect incorrect random digits to be appended.
Fix assertion of too-strict precision and assertion that expecting precision to be too loose.
Fix divmod, modulo and remainder assertion which was expecting a bug inconsistent with Float as a specification.
Remove coerce expectation to add/sub/mult which was dropped because of a bug, and never implemented correctly in BigDecmal.
@tompng tompng force-pushed the fix_bigdecimal_spec branch from 0833f22 to 4ba0080 Compare October 10, 2025 14:25
@tompng tompng merged commit bf7d662 into ruby:master Oct 10, 2025
12 checks passed
@tompng tompng deleted the fix_bigdecimal_spec branch October 10, 2025 14:29
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

Successfully merging this pull request may close these issues.

2 participants