Skip to content

Commit

Permalink
Allow BigDecimal accept Float without precision
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzasa committed Feb 4, 2025
1 parent a015c8b commit 8850458
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
12 changes: 4 additions & 8 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3458,11 +3458,7 @@ rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
}

if (digs == SIZE_MAX) {
if (!raise_exception)
return Qnil;
rb_raise(rb_eArgError,
"can't omit precision for a %"PRIsVALUE".",
CLASS_OF(val));
digs = 0;
}
else if (digs > BIGDECIMAL_DOUBLE_FIGURES) {
if (!raise_exception)
Expand Down Expand Up @@ -3712,12 +3708,12 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
*
* - Integer, Float, Rational, Complex, or BigDecimal: converted directly:
*
* # Integer, Complex, or BigDecimal value does not require ndigits; ignored if given.
* # Integer, Complex, Float, or BigDecimal value does not require ndigits; ignored if given.
* BigDecimal(2) # => 0.2e1
* BigDecimal(Complex(2, 0)) # => 0.2e1
* BigDecimal(BigDecimal(2)) # => 0.2e1
* # Float or Rational value requires ndigits.
* BigDecimal(2.0, 0) # => 0.2e1
* BigDecimal(2.0) # => 0.2e1
* # Rational value requires ndigits.
* BigDecimal(Rational(2, 1), 0) # => 0.2e1
*
* - String: converted by parsing if it contains an integer or floating-point literal;
Expand Down
7 changes: 4 additions & 3 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def test_BigDecimal_with_float
assert_equal(BigDecimal("0.1235"), BigDecimal(0.1234567, 4))
assert_equal(BigDecimal("-0.1235"), BigDecimal(-0.1234567, 4))
assert_equal(BigDecimal("0.01"), BigDecimal(0.01, Float::DIG + 1))
assert_raise_with_message(ArgumentError, "can't omit precision for a Float.") { BigDecimal(4.2) }
assert_nothing_raised { BigDecimal(4.2) }
assert_equal(BigDecimal(4.2), BigDecimal('4.2'))
assert_raise(ArgumentError) { BigDecimal(0.1, Float::DIG + 2) }
assert_nothing_raised { BigDecimal(0.1, Float::DIG + 1) }

Expand Down Expand Up @@ -242,10 +243,10 @@ def test_BigDecimal_with_exception_keyword
assert_equal(nil, BigDecimal(42.quo(7), exception: false))
}
assert_raise(ArgumentError) {
BigDecimal(4.2, exception: true)
BigDecimal(4.2, Float::DIG + 2, exception: true)
}
assert_nothing_raised(ArgumentError) {
assert_equal(nil, BigDecimal(4.2, exception: false))
assert_equal(nil, BigDecimal(4.2, Float::DIG + 2, exception: false))
}
# TODO: support conversion from complex
# assert_raise(RangeError) {
Expand Down

0 comments on commit 8850458

Please sign in to comment.