# File ext/bigdecimal/lib/bigdecimal/math.rb, line 156
def exp(x, prec)
raise ArgumentError, "Zero or negative precision for exp" if prec <= 0
return BigDecimal("NaN") if x.infinite? || x.nan?
n = prec + BigDecimal.double_fig
one = BigDecimal("1")
x = -x if neg = x < 0
x1 = one
y = one
d = y
z = one
i = 0
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
m = BigDecimal.double_fig if m < BigDecimal.double_fig
x1 = x1.mult(x,n)
i += 1
z *= i
d = x1.div(z,m)
y += d
end
if neg
one.div(y, prec)
else
y.round(prec - y.exponent)
end
end