[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Pi limited to 15 digits ?

Vincent Arnoux

9/19/2006 12:42:00 PM

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




5 Answers

William Crawford

9/19/2006 12:52:00 PM

0

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

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

MonkeeSage

9/19/2006 1:08:00 PM

0

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

Vincent Arnoux

9/19/2006 3:09:00 PM

0

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




William Crawford

9/19/2006 3:10:00 PM

0

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

Carl Drinkwater

9/19/2006 4:21:00 PM

0

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.