1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

sqrt() & atan() added.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4394 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shigek 2003-08-15 14:13:49 +00:00
parent 6b0e6a7ce7
commit 3fe728e76b
2 changed files with 46 additions and 5 deletions

View file

@ -1,3 +1,10 @@
Fri Aug 15 23:15:00 2003 Shigeo Kobayashi <shigek@ruby-lang.org>
* ext/bigdecimal/bigdecimal.c .h: Bug in combination of limit & div
method fixed.
* ext/bigdecimal/lib/bigdecimal/math.rb: atan() & sqrt() added.
Fri Aug 15 12:01:43 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* configure.in (HUGE_ST_INO): check whether struct stat.st_ino

View file

@ -1,16 +1,29 @@
#
# Contents:
# sin(x, prec)
# cos(x, prec)
# exp(x, prec)
# log(x, prec)
# PI (prec)
# sqrt(x, prec)
# sin (x, prec)
# cos (x, prec)
# atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
# exp (x, prec)
# log (x, prec)
# PI (prec)
#
# where:
# x ... BigDecimal number to be computed.
# prec ... Number of digits to be obtained.
#
# Usage:
# require "bigdecimal"
# require "bigdecimal/math.rb"
# include BigMath
# a = BigDecimal((PI(1000)/2).to_s)
# puts sin(a,1000) # => 0.10000000000000000000......E1
#
module BigMath
def sqrt(x,prec)
x.sqrt(prec)
end
def sin(x, prec)
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
return BigDecimal("NaN") if x.infinite? || x.nan?
@ -63,6 +76,27 @@ module BigMath
y
end
def atan(x, prec)
raise ArgumentError, "Zero or negative precision for sin" 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
n2 = n+n
y = x;
d = y;
t = x;
r = BigDecimal("3");
x2 = x*x
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
m = BigDecimal.double_fig if m < BigDecimal.double_fig
t = -t.mult(x2,n2)
d = t.div(r,m)
y += d
r += 2
end
y
end
def exp(x, prec)
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
return BigDecimal("NaN") if x.infinite? || x.nan?