From: Rick DeNatale Date: 2008-09-15T00:30:49+09:00 Subject: Re: how do deal with huge numbers and keep them accurate? ------=_Part_89585_1128978.1221406667510 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Sun, Sep 14, 2008 at 10:24 AM, Axel Etzold wrote: > > > > odd=1231231 > > result=410410.333333333 > > > > odd=123123123123121 (15 digits) > > result=41041041041040.3 > > > > so far so good,now: > > odd=1231231231231231 (16 digits) > > result=4.1041041041041e+014 (given with no remainder) > > [It's a wrong answer! the result should have been --- > > 410410410410410.33333] > > > > What do i need to chance in the code to make it happen? > > in the future i should deal with larger numbers and the accuracy is > > essential. > > > > thanks in advanced, tomi. > > -- > > Posted via http://www.ruby-forum.com/. > > Dear Tomi, > > in addition to Rick's post, I#d like to say that in Ruby, you can easily > calculate correctly with fractions, using > the built-in Rational class: > > Yes, I should have pointed out that even 1231231 / 3 => 410410.333333333 Isn't really the 'correct answer'. Since the correct answer has an infinite number of 3's after the decimail point. A big decimal is still a float, albeit a float with lots of digits and in decimal rather than binary, but it still has the characteristics of a float, there are an infinite number of numbers which can't be exactly represented. Of course neither Floats nor Rationals can represent irrational numbers exactly. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------=_Part_89585_1128978.1221406667510--