# Pi limited to 15 digits?

**URL:** <https://www.ruby-forum.com/t/pi-limited-to-15-digits/69222>\
**Category:** Ruby\
**Created:** [September 19, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/pi-limited-to-15-digits/69222 "2006-09-19T14:42:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@Vincent\_A](https://www.ruby-forum.com/u/Vincent_A)\
**Post date:** [September 19, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/pi-limited-to-15-digits/69222/1 "2006-09-19T14:42:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vincent\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@Vincent\_A](https://www.ruby-forum.com/u/Vincent_A)\
**Post date:** [September 19, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/pi-limited-to-15-digits/69222/2 "2006-09-19T14:52:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vincent\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@Vincent\_A](https://www.ruby-forum.com/u/Vincent_A)\
**Post date:** [September 19, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/pi-limited-to-15-digits/69222/3 "2006-09-19T15:10:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vincent\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@Vincent\_A](https://www.ruby-forum.com/u/Vincent_A)\
**Post date:** [September 19, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/pi-limited-to-15-digits/69222/4 "2006-09-19T17:09:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vincent\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@Vincent\_A](https://www.ruby-forum.com/u/Vincent_A)\
**Post date:** [September 19, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/pi-limited-to-15-digits/69222/5 "2006-09-19T17:10:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vincent\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@Vincent\_A](https://www.ruby-forum.com/u/Vincent_A)\
**Post date:** [September 19, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/pi-limited-to-15-digits/69222/6 "2006-09-19T18:22:27Z")

</div>

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.
