From: Florian Gross Date: 2004-12-01T04:07:46+09:00 Subject: Re: [OFF] Python 2.4 released --------------090405000302030407070402 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Austin Ziegler wrote: > I think that BigDecimal handles this just fine. Note as well that > with the Rational class (require 'rational'), you get support for > real rational numbers. If you add 'mathn', you get full support > including with division, etc. > > require 'rational' > Rational.reduce(22, 7) # => Rational(22, 7) > > require 'mathn' > "%0.9f" % [ 22 / 7 ] # => "3.142857143" > (22 / 7).class # => Rational > > BigDecimal works with Numerics, BigIntegers, and Floats, I think. It's impressive how much you can do with Standard Ruby. Add some glue (see attachment) to it and you can do things like this one and get exact results: (1 .. 100).inject(0) { |state, item| state + 1 / item }.to_s(50) # => "5.18737751763962026080511767565825315790897212670845" Where 50 is the number of floating point digits. Increase it and you get more and more detail. It's wonderful how simple things can be with Ruby. It all kind of makes me wonder if Rationals could be added with a different notation to Ruby -- maybe 0.5R + 0.4R. You would always need to specify the precision on output, but I think that is okay. --------------090405000302030407070402 Content-Type: text/plain; name="floatn.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="floatn.rb" require 'mathn' require 'bigdecimal' require 'bigdecimal/util' class Rational alias :old_to_s :to_s def to_s(precision = nil) if precision then if precision == 0 then self.to_d(1).to_s("F").to_i.to_s else self.to_d(precision + 1).to_s("F") end else old_to_s end end end --------------090405000302030407070402--