From: Todd Benson Date: 2008-09-16T00:34:15+09:00 Subject: 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