E added. Typo corrected.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4411 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c41b3b96ab
commit
caf213d9ad
@ -7,6 +7,7 @@
|
|||||||
# exp (x, prec)
|
# exp (x, prec)
|
||||||
# log (x, prec)
|
# log (x, prec)
|
||||||
# PI (prec)
|
# PI (prec)
|
||||||
|
# E (prec) == exp(1.0,prec)
|
||||||
#
|
#
|
||||||
# where:
|
# where:
|
||||||
# x ... BigDecimal number to be computed.
|
# x ... BigDecimal number to be computed.
|
||||||
@ -51,7 +52,7 @@ module BigMath
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cos(x, prec)
|
def cos(x, prec)
|
||||||
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
|
raise ArgumentError, "Zero or negative precision for cos" if prec <= 0
|
||||||
return BigDecimal("NaN") if x.infinite? || x.nan?
|
return BigDecimal("NaN") if x.infinite? || x.nan?
|
||||||
n = prec + BigDecimal.double_fig
|
n = prec + BigDecimal.double_fig
|
||||||
one = BigDecimal("1")
|
one = BigDecimal("1")
|
||||||
@ -76,7 +77,7 @@ module BigMath
|
|||||||
end
|
end
|
||||||
|
|
||||||
def atan(x, prec)
|
def atan(x, prec)
|
||||||
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
|
raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
|
||||||
return BigDecimal("NaN") if x.infinite? || x.nan?
|
return BigDecimal("NaN") if x.infinite? || x.nan?
|
||||||
raise ArgumentError, "x.abs must be less than 1.0" if x.abs>=1
|
raise ArgumentError, "x.abs must be less than 1.0" if x.abs>=1
|
||||||
n = prec + BigDecimal.double_fig
|
n = prec + BigDecimal.double_fig
|
||||||
@ -96,7 +97,7 @@ module BigMath
|
|||||||
end
|
end
|
||||||
|
|
||||||
def exp(x, prec)
|
def exp(x, prec)
|
||||||
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
|
raise ArgumentError, "Zero or negative precision for exp" if prec <= 0
|
||||||
return BigDecimal("NaN") if x.infinite? || x.nan?
|
return BigDecimal("NaN") if x.infinite? || x.nan?
|
||||||
n = prec + BigDecimal.double_fig
|
n = prec + BigDecimal.double_fig
|
||||||
one = BigDecimal("1")
|
one = BigDecimal("1")
|
||||||
@ -174,4 +175,22 @@ module BigMath
|
|||||||
end
|
end
|
||||||
pi
|
pi
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def E(prec)
|
||||||
|
raise ArgumentError, "Zero or negative precision for E" if prec <= 0
|
||||||
|
n = prec + BigDecimal.double_fig
|
||||||
|
one = BigDecimal("1")
|
||||||
|
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
|
||||||
|
i += 1
|
||||||
|
z *= i
|
||||||
|
d = one.div(z,m)
|
||||||
|
y += d
|
||||||
|
end
|
||||||
|
y
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user