[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how do deal with huge numbers and keep them accurate?

Tomi Zzzz

9/14/2008 12:19:00 PM

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-....

10 Answers

Rick DeNatale

9/14/2008 2:08:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Sun, Sep 14, 2008 at 8:18 AM, Tomi Zzzz <tomi_z@nana.co.il> wrote:

> Hello all, I wrote the following code:
>
> require "bigdecimal"
> require "bigdecimal/math"
> include BigMath
> odd = gets.chomp.to_f


This gives you a float, NOT a bigdecimal.


>
> result = odd/3.0
> puts result.to_f


Try

odd = BigDecimal.new(gets.chomp)
puts odd/3 # or odd/3.0


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Axel Etzold

9/14/2008 2:25:00 PM

0


-------- 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-....

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ür Modem und ISDN: http://www.gmx.net/de/go/s...

Rick DeNatale

9/14/2008 3:31:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Sun, Sep 14, 2008 at 10:24 AM, Axel Etzold <AEtzold@gmx.de> 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-....
>
> 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.denh...

Todd Benson

9/14/2008 3:40:00 PM

0

On Sun, Sep 14, 2008 at 7:18 AM, Tomi Zzzz <tomi_z@nana.co.il> wrote:
> 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.

require 'bigdecimal'
require 'bigdecimal/math'
include BigMath
a = BigDecimal('1231231231231231')
b = 3.0
puts a/b
b = BigDecimal('3')
puts a/b

Todd

Todd Benson

9/14/2008 4:09:00 PM

0

On Sun, Sep 14, 2008 at 10:47 AM, Todd Benson <caduceass@gmail.com> wrote:
> On Sun, Sep 14, 2008 at 7:18 AM, Tomi Zzzz <tomi_z@nana.co.il> wrote:
>> 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.
>
> require 'bigdecimal'
> require 'bigdecimal/math'
> include BigMath
> a = BigDecimal('1231231231231231')
> b = 3.0
> puts a/b
> b = BigDecimal('3')
> puts a/b

Oops, didn't clarify. What Rick said, but make sure your denominator
is also a BigDecimal instance. Least common denominator operation
(Float object), I suppose.

Todd

Tomi Zzzz

9/15/2008 2:37:00 PM

0

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.

Attachments:
http://www.ruby-...attachment/268...

--
Posted via http://www.ruby-....

Todd Benson

9/15/2008 3:34:00 PM

0

On Mon, Sep 15, 2008 at 9:36 AM, Tomi Zzzz <tomi_z@nana.co.il> 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')
=> #<BigDecimal:b7cf7418,'0.3333333333 3333333333 333333333 331E33,36(40)>
irb(main):003:0> d = Bigdecimal('3')
=> #<BigDecimal:b7d08754,'0.3E1',4(8)>
irb(main):004:0> (n % d).to_f
=> 1.0

Looks fine to me.

Todd

Axel Etzold

9/15/2008 3:42:00 PM

0


-------- Original-Nachricht --------
> Datum: Tue, 16 Sep 2008 00:34:15 +0900
> Von: "Todd Benson" <caduceass@gmail.com>
> 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 <tomi_z@nana.co.il> 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')
> => #<BigDecimal:b7cf7418,'0.3333333333 3333333333 333333333 331E33,36(40)>
> irb(main):003:0> d = Bigdecimal('3')
> => #<BigDecimal:b7d08754,'0.3E1',4(8)>
> 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/s...

Tomi Zzzz

9/16/2008 3:07:00 PM

0

to Rick, Axle and todd) ... thank u very much for helping me, the code
is running.
I've deleted the float. earlier i thought that this format - 0.3E1 - in
the output was wrong. but now it's good.
U R the BEST! thanks, tomiz.
--
Posted via http://www.ruby-....

Blackwater

11/5/2011 3:19:00 AM

0

"6313 Dead, 1456 since 1/20/09" <dead@gone.com> wrote:

>On Fri, 04 Nov 2011 02:38:09 -0400, Mr.B1ack wrote:
>
>> "6313 Dead, 1456 since 1/20/09" <dead@gone.com> wrote:
>>
>>>On Thu, 03 Nov 2011 23:11:51 -0400, Mr.B1ack wrote:
>>>
>>>> "6313 Dead, 1456 since 1/20/09" <dead@dead.net> wrote:
>>>>
>>>>>Space Weather News for Nov. 3, 201
>>>>>http://spacew...
>>>>>
>>>>>HUGE SUNSPOT: One of the biggest sunspot groups in many years has just
>>>>>emerged over the sun's eastern limb. The sunspot's magnetic canopy is
>>>>>crackling with M-class (medium-sized) solar flares and seems poised to
>>>>>launch even stronger X-class eruptions. The sunspot, named AR1339, is
>>>>>not yet directly facing Earth but it will be turning toward our planet
>>>>>in the days ahead. Check http://spacew... for images of the
>>>>>behemoth and updates.
>>>>>
>>>>>SUNSPOT TELESCOPE: Sunspot AR1339 looks magnificent when viewed
>>>>>through Explore Scientific's White Light Solar Observing System. This
>>>>>safely-filtered telescope is designed for high-quality imaging of
>>>>>sunspots and it is now available in the Space Weather Store:
>>>>>http://www.shopspacewe...
>>>>
>>>>
>>>> Aaarrgh !!! We're all DOOMED !!!
>>>>
>>>> Why ... why ... satellite broadcasts of FOOTBALL GAMES might be
>>>> interrupted !!! Twitter might stop working !!! Panic ! Riots !
>>>> Cannibalism ! The end of global civilization !!!!
>>>
>>>So if Faux News goes off the air, do right wingers panic, scream, and
>>>descend into madness and cannibalism (the moderate route) or do they
>>>just simply stop moving (the extremist route)?
>>
>> They take it as a Sign From Gawd that they're supposed to eat the
>> 'liberals' :-)
>
>No problem. They just do what they historically always do and just eat
>their own children instead.


Aw, those soft tofu-weakened 'liberals' are far
easier to catch :-)