Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions lib/bigdecimal/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ class Rational < Numeric
#
# Returns the value as a BigDecimal.
#
# The required +precision+ parameter is used to determine the number of
# significant digits for the result.
# The +precision+ parameter is used to determine the number of
# significant digits for the result. When +precision+ is set to +0+,
# the number of digits to represent the float being converted is determined
# automatically.
# The default +precision+ is +0+.
#
# require 'bigdecimal'
# require 'bigdecimal/util'
Expand All @@ -129,7 +132,7 @@ class Rational < Numeric
#
# See also Kernel.BigDecimal.
#
def to_d(precision)
def to_d(precision=0)
BigDecimal(self, precision)
end
end
Expand All @@ -141,29 +144,27 @@ class Complex < Numeric
# cmp.to_d(precision) -> bigdecimal
#
# Returns the value as a BigDecimal.
# If the imaginary part is not +0+, an error is raised
#
# The +precision+ parameter is required for a rational complex number.
# This parameter is used to determine the number of significant digits
# for the result.
# The +precision+ parameter is used to determine the number of
# significant digits for the result. When +precision+ is set to +0+,
# the number of digits to represent the float being converted is determined
# automatically.
# The default +precision+ is +0+.
#
# require 'bigdecimal'
# require 'bigdecimal/util'
#
# Complex(0.1234567, 0).to_d(4) # => 0.1235e0
# Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
# Complex(1, 1).to_d # raises ArgumentError
#
# See also Kernel.BigDecimal.
#
def to_d(*args)
def to_d(precision=0)
BigDecimal(self) unless self.imag.zero? # to raise error

if args.length == 0
case self.real
when Rational
BigDecimal(self.real) # to raise error
end
end
self.real.to_d(*args)
BigDecimal(self.real, precision)
end
end

Expand Down
7 changes: 6 additions & 1 deletion test/bigdecimal/test_bigdecimal_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def test_Rational_to_d
assert(355.quo(113).to_d(digits).frozen?)
end

def test_Rational_to_d_without_precision
assert_equal(BigDecimal("1.25"), Rational(5, 4).to_d)
assert_equal(BigDecimal(355.quo(113), 0), 355.quo(113).to_d)
end

def test_Rational_to_d_with_zero_precision
assert_equal(BigDecimal(355.quo(113), 0), 355.quo(113).to_d(0))
end
Expand All @@ -102,7 +107,7 @@ def test_Complex_to_d
assert_equal(BigDecimal("0.1234567"), Complex(0.1234567, 0).to_d)
assert_equal(BigDecimal("0.1235"), Complex(0.1234567, 0).to_d(4))

assert_raise_with_message(ArgumentError, "can't omit precision for a Rational.") { Complex(1.quo(3), 0).to_d }
assert_equal(BigDecimal("0.5"), Complex(1.quo(2), 0).to_d)

assert_raise_with_message(ArgumentError, "Unable to make a BigDecimal from non-zero imaginary number") { Complex(1, 1).to_d }
end
Expand Down
Loading