1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/bigdecimal/lib/bigdecimal-rational.rb
shigek 7744351708 Copied from rough/bigdecimal,documents & some sample programs added.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3625 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2003-03-28 05:00:21 +00:00

30 lines
716 B
Ruby

#
# BigDecimal <-> Rational
#
class BigDecimal
# Convert BigDecimal to Rational
def to_r
sign,digits,base,power = self.to_parts
numerator = sign*digits.to_i
denomi_power = power - digits.size # base is always 10
if denomi_power < 0
denominator = base ** (-denomi_power)
else
denominator = base ** denomi_power
end
Rational.new(numerator,denominator)
end
end
class Rational
# Convert Rational to BigDecimal
# to_d returns an array [quotient,residue]
def to_d(nFig=0)
num = self.numerator.to_s
if nFig<=0
nFig = BigDecimal.double_fig*2+1
end
BigDecimal.new(num).div(self.denominator,nFig)
end
end