|
| 1 | +require 'bigdecimal' |
| 2 | + |
| 3 | +# |
| 4 | +#-- |
| 5 | +# Contents: |
| 6 | +# sqrt(x, prec) |
| 7 | +# sin (x, prec) |
| 8 | +# cos (x, prec) |
| 9 | +# atan(x, prec) Note: |x|<1, x=0.9999 may not converge. |
| 10 | +# log (x, prec) |
| 11 | +# PI (prec) |
| 12 | +# E (prec) == exp(1.0,prec) |
| 13 | +# |
| 14 | +# where: |
| 15 | +# x ... BigDecimal number to be computed. |
| 16 | +# |x| must be small enough to get convergence. |
| 17 | +# prec ... Number of digits to be obtained. |
| 18 | +#++ |
| 19 | +# |
| 20 | +# Provides mathematical functions. |
| 21 | +# |
| 22 | +# Example: |
| 23 | +# |
| 24 | +# require "bigdecimal" |
| 25 | +# require "bigdecimal/math" |
| 26 | +# |
| 27 | +# include BigMath |
| 28 | +# |
| 29 | +# a = BigDecimal((PI(100)/2).to_s) |
| 30 | +# puts sin(a,100) # -> 0.10000000000000000000......E1 |
| 31 | +# |
| 32 | +module BigMath |
| 33 | + module_function |
| 34 | + |
| 35 | + # Computes the square root of x to the specified number of digits of |
| 36 | + # precision. |
| 37 | + # |
| 38 | + # BigDecimal.new('2').sqrt(16).to_s |
| 39 | + # -> "0.14142135623730950488016887242096975E1" |
| 40 | + # |
| 41 | + def sqrt(x,prec) |
| 42 | + x.sqrt(prec) |
| 43 | + end |
| 44 | + |
| 45 | + # Computes the sine of x to the specified number of digits of precision. |
| 46 | + # |
| 47 | + # If x is infinite or NaN, returns NaN. |
| 48 | + def sin(x, prec) |
| 49 | + raise ArgumentError, "Zero or negative precision for sin" if prec <= 0 |
| 50 | + return BigDecimal("NaN") if x.infinite? || x.nan? |
| 51 | + n = prec + BigDecimal.double_fig |
| 52 | + one = BigDecimal("1") |
| 53 | + two = BigDecimal("2") |
| 54 | + x = -x if neg = x < 0 |
| 55 | + if x > (twopi = two * BigMath.PI(prec)) |
| 56 | + if x > 30 |
| 57 | + x %= twopi |
| 58 | + else |
| 59 | + x -= twopi while x > twopi |
| 60 | + end |
| 61 | + end |
| 62 | + x1 = x |
| 63 | + x2 = x.mult(x,n) |
| 64 | + sign = 1 |
| 65 | + y = x |
| 66 | + d = y |
| 67 | + i = one |
| 68 | + z = one |
| 69 | + while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0) |
| 70 | + m = BigDecimal.double_fig if m < BigDecimal.double_fig |
| 71 | + sign = -sign |
| 72 | + x1 = x2.mult(x1,n) |
| 73 | + i += two |
| 74 | + z *= (i-one) * i |
| 75 | + d = sign * x1.div(z,m) |
| 76 | + y += d |
| 77 | + end |
| 78 | + neg ? -y : y |
| 79 | + end |
| 80 | + |
| 81 | + # Computes the cosine of x to the specified number of digits of precision. |
| 82 | + # |
| 83 | + # If x is infinite or NaN, returns NaN. |
| 84 | + def cos(x, prec) |
| 85 | + raise ArgumentError, "Zero or negative precision for cos" if prec <= 0 |
| 86 | + return BigDecimal("NaN") if x.infinite? || x.nan? |
| 87 | + n = prec + BigDecimal.double_fig |
| 88 | + one = BigDecimal("1") |
| 89 | + two = BigDecimal("2") |
| 90 | + x = -x if x < 0 |
| 91 | + if x > (twopi = two * BigMath.PI(prec)) |
| 92 | + if x > 30 |
| 93 | + x %= twopi |
| 94 | + else |
| 95 | + x -= twopi while x > twopi |
| 96 | + end |
| 97 | + end |
| 98 | + x1 = one |
| 99 | + x2 = x.mult(x,n) |
| 100 | + sign = 1 |
| 101 | + y = one |
| 102 | + d = y |
| 103 | + i = BigDecimal("0") |
| 104 | + z = one |
| 105 | + while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0) |
| 106 | + m = BigDecimal.double_fig if m < BigDecimal.double_fig |
| 107 | + sign = -sign |
| 108 | + x1 = x2.mult(x1,n) |
| 109 | + i += two |
| 110 | + z *= (i-one) * i |
| 111 | + d = sign * x1.div(z,m) |
| 112 | + y += d |
| 113 | + end |
| 114 | + y |
| 115 | + end |
| 116 | + |
| 117 | + # Computes the arctangent of x to the specified number of digits of precision. |
| 118 | + # |
| 119 | + # If x is NaN, returns NaN. |
| 120 | + def atan(x, prec) |
| 121 | + raise ArgumentError, "Zero or negative precision for atan" if prec <= 0 |
| 122 | + return BigDecimal("NaN") if x.nan? |
| 123 | + pi = PI(prec) |
| 124 | + x = -x if neg = x < 0 |
| 125 | + return pi.div(neg ? -2 : 2, prec) if x.infinite? |
| 126 | + return pi / (neg ? -4 : 4) if x.round(prec) == 1 |
| 127 | + x = BigDecimal("1").div(x, prec) if inv = x > 1 |
| 128 | + x = (-1 + sqrt(1 + x**2, prec))/x if dbl = x > 0.5 |
| 129 | + n = prec + BigDecimal.double_fig |
| 130 | + y = x |
| 131 | + d = y |
| 132 | + t = x |
| 133 | + r = BigDecimal("3") |
| 134 | + x2 = x.mult(x,n) |
| 135 | + while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0) |
| 136 | + m = BigDecimal.double_fig if m < BigDecimal.double_fig |
| 137 | + t = -t.mult(x2,n) |
| 138 | + d = t.div(r,m) |
| 139 | + y += d |
| 140 | + r += 2 |
| 141 | + end |
| 142 | + y *= 2 if dbl |
| 143 | + y = pi / 2 - y if inv |
| 144 | + y = -y if neg |
| 145 | + y |
| 146 | + end |
| 147 | + |
| 148 | + # Computes the value of pi to the specified number of digits of precision. |
| 149 | + def PI(prec) |
| 150 | + raise ArgumentError, "Zero or negative argument for PI" if prec <= 0 |
| 151 | + n = prec + BigDecimal.double_fig |
| 152 | + zero = BigDecimal("0") |
| 153 | + one = BigDecimal("1") |
| 154 | + two = BigDecimal("2") |
| 155 | + |
| 156 | + m25 = BigDecimal("-0.04") |
| 157 | + m57121 = BigDecimal("-57121") |
| 158 | + |
| 159 | + pi = zero |
| 160 | + |
| 161 | + d = one |
| 162 | + k = one |
| 163 | + w = one |
| 164 | + t = BigDecimal("-80") |
| 165 | + while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0) |
| 166 | + m = BigDecimal.double_fig if m < BigDecimal.double_fig |
| 167 | + t = t*m25 |
| 168 | + d = t.div(k,m) |
| 169 | + k = k+two |
| 170 | + pi = pi + d |
| 171 | + end |
| 172 | + |
| 173 | + d = one |
| 174 | + k = one |
| 175 | + w = one |
| 176 | + t = BigDecimal("956") |
| 177 | + while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0) |
| 178 | + m = BigDecimal.double_fig if m < BigDecimal.double_fig |
| 179 | + t = t.div(m57121,n) |
| 180 | + d = t.div(k,m) |
| 181 | + pi = pi + d |
| 182 | + k = k+two |
| 183 | + end |
| 184 | + pi |
| 185 | + end |
| 186 | + |
| 187 | + # Computes e (the base of natural logarithms) to the specified number of |
| 188 | + # digits of precision. |
| 189 | + def E(prec) |
| 190 | + raise ArgumentError, "Zero or negative precision for E" if prec <= 0 |
| 191 | + n = prec + BigDecimal.double_fig |
| 192 | + one = BigDecimal("1") |
| 193 | + y = one |
| 194 | + d = y |
| 195 | + z = one |
| 196 | + i = 0 |
| 197 | + while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0) |
| 198 | + m = BigDecimal.double_fig if m < BigDecimal.double_fig |
| 199 | + i += 1 |
| 200 | + z *= i |
| 201 | + d = one.div(z,m) |
| 202 | + y += d |
| 203 | + end |
| 204 | + y |
| 205 | + end |
| 206 | +end |
0 commit comments