# Math Time!

**URL:** <https://www.ruby-forum.com/t/math-time/103123>\
**Category:** Ruby\
**Created:** [May 2, 2007, 2:23am UTC](https://www.ruby-forum.com/t/math-time/103123 "2007-05-02T02:23:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [May 2, 2007, 2:23am UTC](https://www.ruby-forum.com/t/math-time/103123/1 "2007-05-02T02:23:17Z")

</div>

Alright everybody! Math time!

So today I tried to write a program which would print out, say, 1000  
digits of pi. I figured I’d do this by assigning a… variable…  
to be a bignum… crap, Ruby is duck typing! I mean, yay?

Ok, so to the point. How do I tell Ruby to print a value (.to\_f) to a  
certain number of integers? .to\_f(1000)? And also, how do I write  
tan, cos, sin, etc. in Ruby?

Thanks for the help,  
math rulez  
--------------------------------------------|  
If you’re not living on the edge,  
then you’re just wasting space.

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [May 2, 2007, 2:39am UTC](https://www.ruby-forum.com/t/math-time/103123/2 "2007-05-02T02:39:18Z")

</div>

On 5/1/07, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Alright everybody! Math time!
> 
> So today I tried to write a program which would print out, say, 1000  
> digits of pi. I figured I’d do this by assigning a… variable…  
> to be a bignum… crap, Ruby is duck typing! I mean, yay?
> 
> Ok, so to the point. How do I tell Ruby to print a value (.to\_f) to a  
> certain number of integers? .to\_f(1000)? And also, how do I write  
> tan, cos, sin, etc. in Ruby?

I havent done this with numbers but you could use:

number.to\_s[0…1000]

If u can do that without the convertion it should be better, but if  
not i hope my solution helps you.

About maths check the Ruby builins Math module:

[http://www.rubycentral.com/book/ref\_m\_math.html](http://www.rubycentral.com/book/ref_m_math.html)

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [May 2, 2007, 2:44am UTC](https://www.ruby-forum.com/t/math-time/103123/3 "2007-05-02T02:44:18Z")

</div>

On May 1, 2007, at 7:22 PM, Ari B. wrote:

> So today I tried to write a program which would print out, say,  
> 1000 digits of pi.

ruby -rbigdecimal -rbigdecimal/math -e ‘include BigMath; digits =  
1000; puts “3.”+BigDecimal.PI(digits).to\_s[3,digits]’

> Ok, so to the point. How do I tell Ruby to print a value (.to\_f) to  
> a certain number of integers?

printf “%.20f”, flt

> .to\_f(1000)? And also, how do I write tan, cos, sin, etc. in Ruby?

Math.tan(num) # etc.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [May 2, 2007, 4:32am UTC](https://www.ruby-forum.com/t/math-time/103123/4 "2007-05-02T04:32:24Z")

</div>

— Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts pi( 1000 )

Oops! I meant → puts pi( 1e1000 )

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [May 2, 2007, 4:29am UTC](https://www.ruby-forum.com/t/math-time/103123/5 "2007-05-02T04:29:24Z")

</div>

— James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> James Edward G. II

Or you could do it the hard way (yes, reinventing the  
wheel is one of my faults)

#### 

# we’ll just assume all computed variables

# are BigDecimals

def pi( precision )  
(0…precision).inject(0) { |sum, i| i\*=4; sum +  
1.0/(i+1) - 1.0/(i+3) } \* 4  
end

puts pi( 1000 )

#### 

# 900 times the age of the universe

# later on my slow system …

# 3.14159…

# this series takes my system about 6 seconds

# just to get to 3.14159 (precision = 1\_000\_000)

# and goes up about a factor of 10 for each additional

# digit

# 

# for 1000 digits, you would need a precision

# value of 1 followed by 1000 0’s (1e1000)

#### 

# cos() converges almost immediately

def fact( i )  
return i == 0 ? 1 : fact( i - 1 ) \* i  
end

def cos( angle, precision )  
(0…precision).inject(0) { |sum, i| i\*=4; sum +  
angle**i/fact(i) - angle**(i+2)/fact(i+2) }  
end

puts cos( 1.0, 3 )

# 0.540302305868092

require ‘mathn’  
puts Math::cos(1.0)

# 0.54030230586814
