MonkeeSage
9/19/2006 1:08:00 PM
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