From: Axel Etzold Date: 2008-09-16T00:41:33+09:00 Subject: Re: how do deal with huge numbers and keep them accurate? -------- Original-Nachricht -------- > Datum: Tue, 16 Sep 2008 00:34:15 +0900 > Von: "Todd Benson" > An: ruby-talk@ruby-lang.org > Betreff: Re: how do deal with huge numbers and keep them accurate? > On Mon, Sep 15, 2008 at 9:36 AM, Tomi Zzzz wrote: > > thanks u all for taking a few minutes to answer me. I'm really > > embarrassed cause I wanted to simplify the question so, I didn't wrote > > down the actual code. I also sorry for not posting any answer yesterday. > > so once more, your help would be very much appreciate... > > > > (I'll drop the 3 lines at the beginning cause I don't sure about them, > > and don't want to mislead you. (talking about: include... require... > > etc). > > > > this is what i want to achieve with that code: > > > > number = gets.chomp.to_f > > divider = 3.0 > > while number % divider != 0 > > divider += 1.0 > > end > > puts divider.to_s > > if divider == 3.0 > > puts "the number can be divided by " + divider.to_s > > else > > puts "can't be divided" > > end > > > > that's it. sorry for repeating myself... once more: > > i want the ability to input a number like this: > > 333333333333333333333333333333331 > > we all know that the answer will be something like that: > > 1111.....111.333333 > > but the code will give me the answer that it can be divide by 3, and > > that's the main problem in that code. > > > > Thanks in advanced, Tomiz. > > p.s. i have tried to implement all of your suggestions with no luck. > > I've add the code for your comfort. > > Once again, you are using floats... > > irb(main):001:0> require 'bigdecimal' > => true > irb(main):002:0> n = Bigdecimal('333333333333333333333333333333331') > => # > irb(main):003:0> d = Bigdecimal('3') > => # > irb(main):004:0> (n % d).to_f > => 1.0 > > Looks fine to me. > > Todd Dear Tomi, as Todd said, using Float is the problem.. Of course you can divide any Float by any other Float (excluding 0.0). But you could do the following, e.g., axel@alecrim:~$ irb irb(main):001:0> a=122222222222222222222222222222222222222 => 122222222222222222222222222222222222222 irb(main):002:0> b=333333333333333333 => 333333333333333333 irb(main):003:0> a%b => 111111111111111233 irb(main):004:0> a.class => Bignum irb(main):005:0> b.class => Bignum irb(main):006:0> Conversions are automatic there, ie. b=3 would be a Fixnum. Number a will be divisible by b if a%b==0. Best regards, Axel -- Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! Ideal f�r Modem und ISDN: http://www.gmx.net/de/go/smartsurfer