Skip to content

Commit 8cdf5cd

Browse files
mrzasamrkn
authored andcommitted
Allow BigDecimal accept Float without precision
1 parent 0b85aa6 commit 8cdf5cd

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,11 +3458,7 @@ rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
34583458
}
34593459

34603460
if (digs == SIZE_MAX) {
3461-
if (!raise_exception)
3462-
return Qnil;
3463-
rb_raise(rb_eArgError,
3464-
"can't omit precision for a %"PRIsVALUE".",
3465-
CLASS_OF(val));
3461+
digs = 0;
34663462
}
34673463
else if (digs > BIGDECIMAL_DOUBLE_FIGURES) {
34683464
if (!raise_exception)
@@ -3712,12 +3708,12 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
37123708
*
37133709
* - Integer, Float, Rational, Complex, or BigDecimal: converted directly:
37143710
*
3715-
* # Integer, Complex, or BigDecimal value does not require ndigits; ignored if given.
3711+
* # Integer, Complex, Float, or BigDecimal value does not require ndigits; ignored if given.
37163712
* BigDecimal(2) # => 0.2e1
37173713
* BigDecimal(Complex(2, 0)) # => 0.2e1
37183714
* BigDecimal(BigDecimal(2)) # => 0.2e1
3719-
* # Float or Rational value requires ndigits.
3720-
* BigDecimal(2.0, 0) # => 0.2e1
3715+
* BigDecimal(2.0) # => 0.2e1
3716+
* # Rational value requires ndigits.
37213717
* BigDecimal(Rational(2, 1), 0) # => 0.2e1
37223718
*
37233719
* - String: converted by parsing if it contains an integer or floating-point literal;

test/bigdecimal/test_bigdecimal.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ def test_BigDecimal_with_float
166166
assert_equal(BigDecimal("0.1235"), BigDecimal(0.1234567, 4))
167167
assert_equal(BigDecimal("-0.1235"), BigDecimal(-0.1234567, 4))
168168
assert_equal(BigDecimal("0.01"), BigDecimal(0.01, Float::DIG + 1))
169-
assert_raise_with_message(ArgumentError, "can't omit precision for a Float.") { BigDecimal(4.2) }
169+
assert_nothing_raised { BigDecimal(4.2) }
170+
assert_equal(BigDecimal(4.2), BigDecimal('4.2'))
170171
assert_raise(ArgumentError) { BigDecimal(0.1, Float::DIG + 2) }
171172
assert_nothing_raised { BigDecimal(0.1, Float::DIG + 1) }
172173

@@ -242,10 +243,10 @@ def test_BigDecimal_with_exception_keyword
242243
assert_equal(nil, BigDecimal(42.quo(7), exception: false))
243244
}
244245
assert_raise(ArgumentError) {
245-
BigDecimal(4.2, exception: true)
246+
BigDecimal(4.2, Float::DIG + 2, exception: true)
246247
}
247248
assert_nothing_raised(ArgumentError) {
248-
assert_equal(nil, BigDecimal(4.2, exception: false))
249+
assert_equal(nil, BigDecimal(4.2, Float::DIG + 2, exception: false))
249250
}
250251
# TODO: support conversion from complex
251252
# assert_raise(RangeError) {

0 commit comments

Comments
 (0)