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

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

Le mardi 19 septembre 2006 Ã 21:52 +0900, William C. 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

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

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.