From: Kenta Murata Date: 2011-01-11T15:11:14+09:00 Subject: [ruby-core:34330] Re: ext/bigdecimal/lib/bigdecimal/util.rb Hi, 2011/1/11 Aaron Patterson : > On Tue, Jan 11, 2011 at 01:23:47PM +0900, Kenta Murata wrote: >> Hi Aaron and Matz, >> >> 2011/1/11 Yukihiro Matsumoto : >> > Hi, >> > >> > In message "Re: [ruby-core:34318] ext/bigdecimal/lib/bigdecimal/util.rb" >> > on Tue, 11 Jan 2011 09:17:15 +0900, Aaron Patterson writes: >> > >> > |It looks like ext/bugdecimal/lib/bigdecimal/util.rb is only >> > |conditionally required by bigdecimal.c: >> > | >> > | https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L148-151 >> > | >> > |Could we possibly change this to just require 'util.rb' all the time? >> > >> > I'd rather like to remove the dependency of util.rb from the >> > bigdecimal core. >> >> I've wrote an another patch without requiring bigdecimal/util.rb. >> >> https://gist.github.com/774012 >> >> If it doesn't have any problems, I'll commit tonight. > > This doesn't do what I want. I want a generic way to convert Numeric > types to BigDecimal. > > util.rb half-way implements Numeric => BigDecimal conversion. See > [ruby-core:34326] for examples. > > If you like, I can write tests along with a patch to demonstrate. :-) https://gist.github.com/774077 This patch support to generate a BigDecimal from any Numeric classes excepting Float. Float is difficult. The radix of Float may be different from BigDecimal so some Floats aren't able to convert to BigDecimal without error. For example 10.1 isn't represented in Float precisely: 10.1.to_r #=> (5685794529555251/562949953421312) This is not 10.1. You have to specify a precision for conversion if you convert 10.1 to BigDecimal. For any Float values, we have to specify precisions for converting to BigDecimal. I'll implement that, please wait to finish, or please give me a patch. -- Kenta Murata OpenPGP FP = 1D69 ADDE 081C 9CC2 2E54 98C1 CEFE 8AFB 6081 B062 本を書きました!! 『Ruby 逆引きレシピ』 http://www.amazon.co.jp/dp/4798119881/mrkn-22 E-mail: mrkn@mrkn.jp twitter: http://twitter.com/mrkn/ blog: http://d.hatena.ne.jp/mrkn/