diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index a707295..dda56e6 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -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) @@ -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; diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index fab9622..1ceae55 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -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) } @@ -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) {