Skip to content

Commit 57e0d40

Browse files
mrzasatimcraft
andauthored
Allow BigDecimal accept Float without precision (#314)
* Allow BigDecimal accept Float without precision * Fix typo in BigDecimal#scale comment (#348) * Added tests for float --------- Co-authored-by: Tim Craft <email@timcraft.com>
1 parent 641ee7e commit 57e0d40

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

ext/bigdecimal/bigdecimal.c

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

34303430
if (digs == SIZE_MAX) {
3431-
if (!raise_exception)
3432-
return Qnil;
3433-
rb_raise(rb_eArgError,
3434-
"can't omit precision for a %"PRIsVALUE".",
3435-
CLASS_OF(val));
3431+
digs = 0;
34363432
}
34373433
else if (digs > BIGDECIMAL_DOUBLE_FIGURES) {
34383434
if (!raise_exception)
@@ -3682,12 +3678,12 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
36823678
*
36833679
* - Integer, Float, Rational, Complex, or BigDecimal: converted directly:
36843680
*
3685-
* # Integer, Complex, or BigDecimal value does not require ndigits; ignored if given.
3681+
* # Integer, Complex, Float, or BigDecimal value does not require ndigits; ignored if given.
36863682
* BigDecimal(2) # => 0.2e1
36873683
* BigDecimal(Complex(2, 0)) # => 0.2e1
36883684
* BigDecimal(BigDecimal(2)) # => 0.2e1
3689-
* # Float or Rational value requires ndigits.
3690-
* BigDecimal(2.0, 0) # => 0.2e1
3685+
* BigDecimal(2.0) # => 0.2e1
3686+
* # Rational value requires ndigits.
36913687
* BigDecimal(Rational(2, 1), 0) # => 0.2e1
36923688
*
36933689
* - String: converted by parsing if it contains an integer or floating-point literal;

test/bigdecimal/test_bigdecimal.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ 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'))
171+
assert_equal(BigDecimal("0.12345"), BigDecimal(0.12345, 0))
172+
assert_equal(BigDecimal("0.12345"), BigDecimal(0.12345))
170173
assert_raise(ArgumentError) { BigDecimal(0.1, Float::DIG + 2) }
171174
assert_nothing_raised { BigDecimal(0.1, Float::DIG + 1) }
172175

@@ -242,10 +245,10 @@ def test_BigDecimal_with_exception_keyword
242245
assert_equal(nil, BigDecimal(42.quo(7), exception: false))
243246
}
244247
assert_raise(ArgumentError) {
245-
BigDecimal(4.2, exception: true)
248+
BigDecimal(4.2, Float::DIG + 2, exception: true)
246249
}
247250
assert_nothing_raised(ArgumentError) {
248-
assert_equal(nil, BigDecimal(4.2, exception: false))
251+
assert_equal(nil, BigDecimal(4.2, Float::DIG + 2, exception: false))
249252
}
250253
# TODO: support conversion from complex
251254
# assert_raise(RangeError) {

0 commit comments

Comments
 (0)