-------- Original-Nachricht -------- > Datum: Sun, 14 Sep 2008 21:18:45 +0900 > Von: Tomi Zzzz <tomi_z / nana.co.il> > An: ruby-talk / ruby-lang.org > Betreff: how do deal with huge numbers and keep them accurate? > Hello all, I wrote the following code: > > require "bigdecimal" > require "bigdecimal/math" > include BigMath > odd = gets.chomp.to_f > result = odd/3.0 > puts result.to_f > blank = gets.chomp > > the code works just fine when the input is upto 15 digits, but > afterwards the code fails to give the correct output. > > a few good examples: > 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: require "rational" a_numerator=1233444534455555555555 a_denominator=3 a=Rational(a_numerator,1)+Rational(1,a_denominator) p a p ((a.numerator-1)/3)==a_numerator Conversion between Fixnum and Bignum is automatic. Best regards, Axel -- Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! Ideal f Modem und ISDN: http://www.gmx.net/de/go/smartsurfer