module BigMath
def sin(x, prec)
#=> "0.70710678118654752440082036563292800375e0"
BigMath.sin(BigMath.PI(5)/4, 5).to_s
If +decimal+ is Infinity or NaN, returns NaN.
precision, +numeric+.
Computes the sine of +decimal+ to the specified number of digits of
sin(decimal, numeric) -> BigDecimal
call-seq:
def sin(x, prec) raise ArgumentError, "Zero or negative precision for sin" if prec <= 0 return BigDecimal("NaN") if x.infinite? || x.nan? n = prec + BigDecimal.double_fig one = BigDecimal("1") two = BigDecimal("2") x = -x if neg = x < 0 if x > (twopi = two * BigMath.PI(prec)) if x > 30 x %= twopi else x -= twopi while x > twopi end end x1 = x x2 = x.mult(x,n) sign = 1 y = x d = y i = one z = one while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0) m = BigDecimal.double_fig if m < BigDecimal.double_fig sign = -sign x1 = x2.mult(x1,n) i += two z *= (i-one) * i d = sign * x1.div(z,m) y += d end neg ? -y : y end