-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigmath.rb
220 lines (183 loc) · 6.82 KB
/
bigmath.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
require 'bigdecimal/math'
require 'bigdecimal/util'
module BigMath
module_function
def log2(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return -BigDecimal::INFINITY if x.zero?
if x.positive?
return BigDecimal::INFINITY if x.infinite?
y = log(x, prec) / log(2, prec)
return y
end
BigDecimal::NAN
end
def log10(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return -BigDecimal::INFINITY if x.zero?
if x.positive?
return BigDecimal::INFINITY if x.infinite?
y = log(x, prec) / log(10, prec)
return y
end
BigDecimal::NAN
end
def cbrt(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return x if (x.respond_to?(:nan?) && x.nan?) || x.infinite?
sign = x.negative? ? -1 : 1
y = sign * BigDecimal((sign * x).to_s, prec) ** Rational(1, 3).to_d(prec)
y
end
def exp2(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return x if x.respond_to?(:nan?) && x.nan?
x *= log(BigDecimal('2'), prec)
y = exp(x, prec)
y
end
def tan(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return x if (x.respond_to?(:nan?) && x.nan?) || x.infinite?
y = sin(x, prec) / cos(x, prec)
y
end
def asin(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
if (x >= -1 && x <= 1)
atan((x / sqrt(1 - x * x, prec)), prec)
else
BigDecimal::NAN
end
end
def acos(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
if (x >= -1 && x <= 1)
if x == 1
BigDecimal('0', prec)
elsif x == -1
PI(prec)
else
PI(prec) / 2 - asin(x, prec)
end
else
BigDecimal::NAN
end
end
def sinh(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return x if ((x.respond_to?(:nan?) && x.nan?) || x.infinite?)
exppx = exp( x, prec)
expmx = exp(-x, prec)
exppx / 2 - expmx / 2
end
def cosh(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return x if x.nan?
return BigDecimal::INFINITY if x.infinite?
exppx = exp( x, prec)
expmx = exp(-x, prec)
expmx / 2 + exppx / 2
end
def tanh(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return x if x.nan?
if x.infinite?
return x < 0 ? BigDecimal('-1', prec) : BigDecimal('1', prec)
end
exppx = exp( x, prec)
expmx = exp(-x, prec)
exppx / (expmx + exppx) - expmx / (expmx + exppx)
end
def asinh(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return x if ((x.respond_to?(:nan?) && x.nan?) || x.infinite?)
log(sqrt(x * x + 1, prec) + x, prec)
end
def acosh(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
if x >= 1
return x if x.infinite?
log(x + sqrt(x - 1, prec) * sqrt(x + 1, prec), prec)
else
BigDecimal::NAN
end
end
def atanh(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
if x >= -1 && x <= 1
half = 1/2r
logp1 = (x + 1) == 0 ? -BigDecimal::INFINITY : log(x + 1, prec)
logm1 = (1 - x) == 0 ? -BigDecimal::INFINITY : log(1 - x, prec)
y = half * logp1 - half * logm1
return y
end
BigDecimal::NAN
end
def hypot(x, y, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return BigDecimal::NAN if (x.respond_to?(:nan?) && x.nan?) || (y.respond_to?(:nan?) && y.nan?)
return BigDecimal::INFINITE if x.infinite? || y.infinite?
sqrt(x * x + y * y, prec)
end
def lgamma(x, prec)
raise ArgumentError, "precision must be an Integer" unless prec.class == Integer
raise ArgumentError, "Zero or negative precision for #{__method__}" if prec <= 0
return BigDecimal('0', prec) if x == 1 or x == 2
# Positive Argument Routine (Reference: Haruhiko Okumura: C-gengo ni yoru hyoujun algorithm jiten)
positive = lambda do
log_2pi = log(PI(prec) * 2, prec) # $\log 2\pi$
n = 8
coef = [
1/12r, # B_2 / ( 2 * 1) = 1/12
-1/360r, # B_4 / ( 4 * 3) = -1/360
1/1260r, # B_6 / ( 6 * 5) = 1/1260
-1/1680r, # B_8 / ( 8 * 7) = -1/1680
1/1188r, # B_10 / (10 * 9) = 1/1188
-691/360360r, # B_12 / (12 * 11) = -691/360360
1/156r, # B_14 / (14 * 13) = 1/156
-3617/122400r # B_16 / (16 * 15) = -3617/122400
]
v = 1
while (x < n); v *= x; x += 1; end
w = 1 / (x * x)
y = (((((((coef[7] * w + coef[6]) * w + coef[5]) * w + coef[4]) * w\
+ coef[3]) * w + coef[2]) * w + coef[1]) * w + coef[0]) / x\
+ 0.5 * log_2pi - log(v, prec) - x + (x - 0.5) * log(x, prec)
y
end
# Negative Argument Routine
negative = lambda do
s = sin(PI(prec) * x, prec)
[s.round(prec)].each {|s1| return 1 / s1 if s1.zero?}
s = (1 / s).abs
x = 1 - x
y = log(s, prec) - positive.call + log(PI(prec), prec)
y
end
case x
when BigDecimal::NAN
BigDecimal::NAN
when BigDecimal::INFINITY
x.negative? ? BigDecimal::NAN : BigDecimal::INFINITY
when BigDecimal('0')
BigDecimal::INFINITY
else # BigDecimal # Finite
x.negative? ? negative.call : positive.call
end
end
end