Pi limited to 15 digits ?

Discussion in 'Ruby' started by Vincent Arnoux, Sep 19, 2006.

  1. Hello,
    I am trying to calculate pi in an arithmetical way with a big number of
    decimal digits.
    Unfortunately, the code below only displays 15 digits. How can I ask for
    more digits ?

    require 'bigdecimal'

    puts("Enter precision : ")
    precision = gets.chomp.to_i

    i, sign = 1.0, 1
    value = BigDecimal.new("0")
    pi = BigDecimal.new("0")

    precision.times do
    value += 1/i * sign
    sign *= -1
    i += 2
    end

    pi = 4 * value
    puts pi.to_s

    Vincent
     
    Vincent Arnoux, Sep 19, 2006
    #1
    1. Advertising

  2. Vincent Arnoux wrote:
    > Hello,
    > I am trying to calculate pi in an arithmetical way with a big number of
    > decimal digits.


    That's this week's Code Golf challenge.

    http://www.ruby-forum.com/topic/81689#new

    I'm not sure you'll get any answers until that is over, since that would
    be 'cheating' for all of them. I'm assuming you aren't doing that and
    trying to cheat... So it's just bad luck for you that it happened at
    exactly the same time.

    --
    Posted via http://www.ruby-forum.com/.
     
    William Crawford, Sep 19, 2006
    #2
    1. Advertising

  3. Vincent Arnoux

    MonkeeSage Guest

    Hi Vincent,

    You need to initialize all your ints/floats that will interact with any
    bigdecimal numbers, as bigdecimals, or else you'll cast the bigdecimals
    back to ints/floats:

    require 'bigdecimal'
    puts("Enter precision : ")
    precision = gets.chomp.to_i
    i, sign = BigDecimal.new('1.0'), BigDecimal.new('1.0')
    value = BigDecimal.new('0')
    pi = BigDecimal.new('4.0')
    precision.times do
    value += 1/i * sign
    sign *= -1
    i += 2
    end
    pi = pi * value
    puts pi

    Ps. Don't worry William, this wont help us code golf, since one of the
    rules is that we can't use require, so no bigdecimal. ;)

    Regards,
    Jordan
     
    MonkeeSage, Sep 19, 2006
    #3
  4. Le mardi 19 septembre 2006 à 21:52 +0900, William Crawford a écrit :
    > I'm not sure you'll get any answers until that is over, since that would
    > be 'cheating' for all of them. I'm assuming you aren't doing that and
    > trying to cheat... So it's just bad luck for you that it happened at
    > exactly the same time.

    Sorry William, it was not my goal to cheat: I am not experienced enough
    to follow challenges yet (and that would have been a pretty obvious and
    stupid way to cheat anyway... ;-) )

    Vincent
     
    Vincent Arnoux, Sep 19, 2006
    #4
  5. Jordan Callicoat wrote:
    > Ps. Don't worry William, this wont help us code golf, since one of the
    > rules is that we can't use require, so no bigdecimal. ;)
    >
    > Regards,
    > Jordan


    Ohh, right. Missed that. (I obviously haven't gotten up the nerve to
    try to compete yet.)

    --
    Posted via http://www.ruby-forum.com/.
     
    William Crawford, Sep 19, 2006
    #5
  6. Hi,

    > I'm not sure you'll get any answers until that is over, since that would
    > be 'cheating' for all of them. I'm assuming you aren't doing that and
    > trying to cheat... So it's just bad luck for you that it happened at
    > exactly the same time.


    I don't consider this to be cheating - Asking for help with the
    algorithm is well within the rules, and in fact, we have forums on the
    site for exactly that purpose (Not that they are particularly well used
    at the moment - Hint, hint!)

    Although part of the challenge will always be implementing it, the point
    is getting really short code - Giving people help to get a working
    solution won't necessarily help them with that.

    Regards,
    Carl.
     
    Carl Drinkwater, Sep 19, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?Q?Kai_Br=E4utigam?=

    MailMessage.Body limited?

    =?iso-8859-1?Q?Kai_Br=E4utigam?=, Aug 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    549
    =?iso-8859-1?Q?Kai_Br=E4utigam?=
    Aug 28, 2003
  2. Peter L.
    Replies:
    0
    Views:
    348
    Peter L.
    Oct 20, 2003
  3. Stig Johansen

    Session_End not firing or limited?

    Stig Johansen, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,784
    Jim Cheshire [MSFT]
    Dec 5, 2003
  4. DotNet Ed
    Replies:
    2
    Views:
    497
    Nicole Calinoiu
    Oct 12, 2004
  5. Kenneth Keeley

    Retirn only a limited number of results.

    Kenneth Keeley, Oct 28, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    301
    Anton Sokolovsky
    Oct 28, 2004
Loading...

Share This Page