summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/lib/bigdecimal
diff options
context:
space:
mode:
authorshigek <shigek@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-08-19 14:16:09 +0000
committershigek <shigek@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-08-19 14:16:09 +0000
commitcaf213d9ad1fd277d1cc615306d3eb662c2283af (patch)
tree97f4ba2a081bde542b91db9b9d092ad9fea76939 /ext/bigdecimal/lib/bigdecimal
parentc41b3b96ab097540bcf66a43639233ce47b8e219 (diff)
E added. Typo corrected.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4411 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal/lib/bigdecimal')
-rw-r--r--ext/bigdecimal/lib/bigdecimal/math.rb25
1 files changed, 22 insertions, 3 deletions
diff --git a/ext/bigdecimal/lib/bigdecimal/math.rb b/ext/bigdecimal/lib/bigdecimal/math.rb
index f6a3ff6a62c..7b3f46ed1a3 100644
--- a/ext/bigdecimal/lib/bigdecimal/math.rb
+++ b/ext/bigdecimal/lib/bigdecimal/math.rb
@@ -7,6 +7,7 @@
# exp (x, prec)
# log (x, prec)
# PI (prec)
+# E (prec) == exp(1.0,prec)
#
# where:
# x ... BigDecimal number to be computed.
@@ -51,7 +52,7 @@ module BigMath
end
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?
n = prec + BigDecimal.double_fig
one = BigDecimal("1")
@@ -76,7 +77,7 @@ module BigMath
end
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?
raise ArgumentError, "x.abs must be less than 1.0" if x.abs>=1
n = prec + BigDecimal.double_fig
@@ -96,7 +97,7 @@ module BigMath
end
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?
n = prec + BigDecimal.double_fig
one = BigDecimal("1")
@@ -174,4 +175,22 @@ module BigMath
end
pi
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