From: Michael W Ryder Date: 2010-04-30T03:50:15+09:00 Subject: Re: flt gem for arbitrary precision Robert Klemme wrote: > On 04/28/2010 11:31 AM, Giampiero Zanchi wrote: >> ciao a tutti (hi to hall) >> >> I just discoverd a gem to have arbitrary precision numbers and want to >> share with the forum. >> >> Install the gem with >> >> gem install flt >> >> an example script follows; it computes the square root of 2 >> >> require 'flt' >> include Flt >> >> DecNum.context.precision = 200 >> >> p DecNum(2).sqrt >> >> produces: >> >> DecNum('1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605715') >> >> >> documentation here >> http://flt.rubyforge.org/ > > What's the difference (precision? performance?) vs. doing > > irb(main):001:0> require 'bigdecimal' > => true > irb(main):002:0> BigDecimal("2").sqrt(200).to_s > => > "0.1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970125E1" > > irb(main):003:0> BigDecimal("2").sqrt(200) > => # 6718753769 4807317667 9737990732 4784621070 3885038753 4327641572 > 7350138462 3091229702 4924836055 8507372126 4412149709 9935831413 > 2226659275 0559275579 9950501152 7820605714 7010955997 > 1605970125E1',224(224)> > irb(main):004:0> > > ? > > Kind regards > > robert > If my reading of the documentation is correct the difference is how the intermediate results are stored. Dividing 1 by 3 with a precision of 2 would result in 0.33 versus 0.3333333... If you print a commission rate of 33.33% on a form and a charge of $5,0000, you want the amount to show $1,666.50 (5000 * .3333), not $1,666.67 (5,0000 * 1/3) as a lot of people will complain that your figures are wrong. I use fixed precision for all my work as it deals with money and everything has to match to the penny.