Math Time!

A

Ari Brown

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

Emilio Tagua

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
 
J

James Edward Gray II

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 Gray II
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Correcting complex math 10
Ruby Math (nPr, nCr) 4
Trig value errors 57
require and include?? 7
[SUMMARY] Math Captcha (#48) 0
Learning the fine points of Ruby 5
Wrapping paper, anyone ? 9
OpenGL 3D gears demo for Ruby 1

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top