Skip to content

Commit a5024cf

Browse files
authored
Merge pull request #956 from ksss/bigdecimal-4
Add BigDecimal v4
2 parents 3a33fa6 + dda768f commit a5024cf

File tree

3 files changed

+1922
-0
lines changed

3 files changed

+1922
-0
lines changed

gems/bigdecimal/4.0/_test/test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Write Ruby code to test the RBS.
2+
# It is type checked by `steep check` command.
3+
4+
require "bigdecimal"
5+
require "bigdecimal/util"
6+
7+
BigDecimal("1.23")
8+
9+
BigDecimal("1.23") + BigDecimal("1.23")
10+
BigDecimal("1.23") - BigDecimal("1.23")
11+
BigDecimal("1.23") * BigDecimal("1.23")
12+
BigDecimal("1.23") / BigDecimal("1.23")
13+
14+
BigDecimal("1.23").to_d
15+
BigDecimal("1.23").to_f
16+
BigDecimal("1.23").to_i
17+
BigDecimal("1.23").to_r
18+
19+
123.to_d
20+
12.3.to_d
21+
12r.to_d(3)
22+
0i.to_d
23+
24+
require "bigdecimal/math"
25+
26+
BigMath.E(10)
27+
BigMath.PI(10)
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
# <!-- rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb -->
2+
# Provides mathematical functions.
3+
#
4+
# Example:
5+
#
6+
# require "bigdecimal/math"
7+
#
8+
# include BigMath
9+
#
10+
# a = BigDecimal((PI(100)/2).to_s)
11+
# puts sin(a,100) # => 0.99999999999999999999......e0
12+
#
13+
module BigMath
14+
# <!--
15+
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
16+
# - E(numeric) -> BigDecimal
17+
# -->
18+
# Computes e (the base of natural logarithms) to the specified number of digits
19+
# of precision, `numeric`.
20+
#
21+
# BigMath.E(10).to_s
22+
# #=> "0.271828182845904523536028752390026306410273e1"
23+
#
24+
def self?.E: (Numeric prec) -> BigDecimal
25+
26+
# <!--
27+
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
28+
# - PI(numeric) -> BigDecimal
29+
# -->
30+
# Computes the value of pi to the specified number of digits of precision,
31+
# `numeric`.
32+
#
33+
# BigMath.PI(10).to_s
34+
# #=> "0.3141592653589793238462643388813853786957412e1"
35+
#
36+
def self?.PI: (Numeric prec) -> BigDecimal
37+
38+
# Computes the arccosine of `decimal` to the specified number of digits of
39+
# precision, `numeric`.
40+
#
41+
# If `decimal` is NaN, returns NaN.
42+
#
43+
# BigMath.acos(BigDecimal('0.5'), 32).to_s
44+
# #=> "0.10471975511965977461542144610932e1"
45+
#
46+
def self?.acos: (BigDecimal, Numeric) -> BigDecimal
47+
48+
# Computes the inverse hyperbolic cosine of `decimal` to the specified number of
49+
# digits of precision, `numeric`.
50+
#
51+
# If `decimal` is NaN, returns NaN.
52+
#
53+
# BigMath.acosh(BigDecimal('2'), 32).to_s
54+
# #=> "0.1316957896924816708625046347308e1"
55+
#
56+
def self?.acosh: (BigDecimal, Numeric) -> BigDecimal
57+
58+
# Computes the arcsine of `decimal` to the specified number of digits of
59+
# precision, `numeric`.
60+
#
61+
# If `decimal` is NaN, returns NaN.
62+
#
63+
# BigMath.asin(BigDecimal('0.5'), 32).to_s
64+
# #=> "0.52359877559829887307710723054658e0"
65+
#
66+
def self?.asin: (BigDecimal, Numeric) -> BigDecimal
67+
68+
# Computes the inverse hyperbolic sine of `decimal` to the specified number of
69+
# digits of precision, `numeric`.
70+
#
71+
# If `decimal` is NaN, returns NaN.
72+
#
73+
# BigMath.asinh(BigDecimal('1'), 32).to_s
74+
# #=> "0.88137358701954302523260932497979e0"
75+
#
76+
def self?.asinh: (BigDecimal, Numeric) -> BigDecimal
77+
78+
# <!--
79+
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
80+
# - atan(decimal, numeric) -> BigDecimal
81+
# -->
82+
# Computes the arctangent of `decimal` to the specified number of digits of
83+
# precision, `numeric`.
84+
#
85+
# If `decimal` is NaN, returns NaN.
86+
#
87+
# BigMath.atan(BigDecimal('-1'), 16).to_s
88+
# #=> "-0.785398163397448309615660845819878471907514682065e0"
89+
#
90+
def self?.atan: (BigDecimal x, Numeric prec) -> BigDecimal
91+
92+
# Computes the arctangent of y and x to the specified number of digits of
93+
# precision, `numeric`.
94+
#
95+
# BigMath.atan2(BigDecimal('-1'), BigDecimal('1'), 32).to_s
96+
# #=> "-0.78539816339744830961566084581988e0"
97+
#
98+
def self?.atan2: (BigDecimal, BigDecimal, Numeric) -> BigDecimal
99+
100+
# Computes the inverse hyperbolic tangent of `decimal` to the specified number
101+
# of digits of precision, `numeric`.
102+
#
103+
# If `decimal` is NaN, returns NaN.
104+
#
105+
# BigMath.atanh(BigDecimal('0.5'), 32).to_s
106+
# #=> "0.54930614433405484569762261846126e0"
107+
#
108+
def self?.atanh: (BigDecimal, Numeric) -> BigDecimal
109+
110+
# Computes the cube root of `decimal` to the specified number of digits of
111+
# precision, `numeric`.
112+
#
113+
# BigMath.cbrt(BigDecimal('2'), 32).to_s
114+
# #=> "0.12599210498948731647672106072782e1"
115+
#
116+
def self?.cbrt: (BigDecimal, Numeric) -> BigDecimal
117+
118+
# <!--
119+
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
120+
# - cos(decimal, numeric) -> BigDecimal
121+
# -->
122+
# Computes the cosine of `decimal` to the specified number of digits of
123+
# precision, `numeric`.
124+
#
125+
# If `decimal` is Infinity or NaN, returns NaN.
126+
#
127+
# BigMath.cos(BigMath.PI(4), 16).to_s
128+
# #=> "-0.999999999999999999999999999999856613163740061349e0"
129+
#
130+
def self?.cos: (BigDecimal x, Numeric prec) -> BigDecimal
131+
132+
# Computes the hyperbolic cosine of `decimal` to the specified number of digits
133+
# of precision, `numeric`.
134+
#
135+
# If `decimal` is NaN, returns NaN.
136+
#
137+
# BigMath.cosh(BigDecimal('1'), 32).to_s
138+
# #=> "0.15430806348152437784779056207571e1"
139+
#
140+
def self?.cosh: (BigDecimal, Numeric) -> BigDecimal
141+
142+
# Computes the error function of +decimal+ to the specified number of digits of
143+
# precision, +numeric+.
144+
#
145+
# If +decimal+ is NaN, returns NaN.
146+
#
147+
# BigMath.erf(BigDecimal('1'), 32).to_s
148+
# #=> "0.84270079294971486934122063508261e0"
149+
#
150+
def self?.erf: (BigDecimal, Numeric) -> BigDecimal
151+
152+
# Computes the complementary error function of +decimal+ to the specified number of digits of
153+
# precision, +numeric+.
154+
#
155+
# If +decimal+ is NaN, returns NaN.
156+
#
157+
# BigMath.erfc(BigDecimal('10'), 32).to_s
158+
# #=> "0.20884875837625447570007862949578e-44"
159+
#
160+
def self?.erfc: (BigDecimal, Numeric) -> BigDecimal
161+
162+
# Computes the value of e (the base of natural logarithms) raised to the power
163+
# of `decimal`, to the specified number of digits of precision.
164+
#
165+
# If `decimal` is infinity, returns Infinity.
166+
#
167+
# If `decimal` is NaN, returns NaN.
168+
#
169+
def self?.exp: (BigDecimal, Numeric prec) -> BigDecimal
170+
171+
# Decomposes +x+ into a normalized fraction and an integral power of ten.
172+
#
173+
# BigMath.frexp(BigDecimal(123.456))
174+
# #=> [0.123456e0, 3]
175+
#
176+
def self?.frexp: (BigDecimal) -> [ BigDecimal, Integer ]
177+
178+
# Computes the gamma function of +decimal+ to the specified number of
179+
# digits of precision, +numeric+.
180+
#
181+
# BigMath.gamma(BigDecimal('0.5'), 32).to_s
182+
# #=> "0.17724538509055160272981674833411e1"
183+
#
184+
def self?.gamma: (BigDecimal, Numeric) -> BigDecimal
185+
186+
# Returns sqrt(x**2 + y**2) to the specified number of digits of precision,
187+
# `numeric`.
188+
#
189+
# BigMath.hypot(BigDecimal('1'), BigDecimal('2'), 32).to_s
190+
# #=> "0.22360679774997896964091736687313e1"
191+
#
192+
def self?.hypot: (BigDecimal, BigDecimal, Numeric) -> BigDecimal
193+
194+
# Inverse of +frexp+.
195+
# Returns the value of fraction * 10**exponent.
196+
#
197+
# BigMath.ldexp(BigDecimal("0.123456e0"), 3)
198+
# #=> 0.123456e3
199+
#
200+
def self?.ldexp: (BigDecimal, Integer) -> BigDecimal
201+
202+
# Computes the natural logarithm of the absolute value of the gamma function
203+
# of +decimal+ to the specified number of digits of precision, +numeric+ and its sign.
204+
#
205+
# BigMath.lgamma(BigDecimal('0.5'), 32)
206+
# #=> [0.57236494292470008707171367567653e0, 1]
207+
#
208+
def self?.lgamma: (BigDecimal, Numeric) -> [BigDecimal, Integer]
209+
210+
# Computes the natural logarithm of `decimal` to the specified number of digits
211+
# of precision, `numeric`.
212+
#
213+
# If `decimal` is zero or negative, raises Math::DomainError.
214+
#
215+
# If `decimal` is positive infinity, returns Infinity.
216+
#
217+
# If `decimal` is NaN, returns NaN.
218+
#
219+
def self?.log: (BigDecimal, Numeric prec) -> BigDecimal
220+
221+
# Computes the base 2 logarithm of `decimal` to the specified number of digits
222+
# of precision, `numeric`.
223+
#
224+
# If `decimal` is zero or negative, raises Math::DomainError.
225+
#
226+
# If `decimal` is positive infinity, returns Infinity.
227+
#
228+
# If `decimal` is NaN, returns NaN.
229+
#
230+
# BigMath.log2(BigDecimal('3'), 32).to_s
231+
# #=> "0.15849625007211561814537389439478e1"
232+
#
233+
def self?.log2: (BigDecimal, Numeric) -> BigDecimal
234+
235+
# <!--
236+
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
237+
# - sin(decimal, numeric) -> BigDecimal
238+
# -->
239+
# Computes the sine of `decimal` to the specified number of digits of precision,
240+
# `numeric`.
241+
#
242+
# If `decimal` is Infinity or NaN, returns NaN.
243+
#
244+
# BigMath.sin(BigMath.PI(5)/4, 5).to_s
245+
# #=> "0.70710678118654752440082036563292800375e0"
246+
#
247+
def self?.sin: (BigDecimal x, Numeric prec) -> BigDecimal
248+
249+
# Computes the hyperbolic sine of `decimal` to the specified number of digits of
250+
# precision, `numeric`.
251+
#
252+
# If `decimal` is NaN, returns NaN.
253+
#
254+
# BigMath.sinh(BigDecimal('1'), 32).to_s
255+
# #=> "0.11752011936438014568823818505956e1"
256+
#
257+
def self?.sinh: (BigDecimal, Numeric) -> BigDecimal
258+
259+
# <!--
260+
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
261+
# - sqrt(decimal, numeric) -> BigDecimal
262+
# -->
263+
# Computes the square root of `decimal` to the specified number of digits of
264+
# precision, `numeric`.
265+
#
266+
# BigMath.sqrt(BigDecimal('2'), 16).to_s
267+
# #=> "0.1414213562373095048801688724e1"
268+
#
269+
def self?.sqrt: (BigDecimal x, Numeric prec) -> BigDecimal
270+
271+
# Computes the hyperbolic tangent of `decimal` to the specified number of digits
272+
# of precision, `numeric`.
273+
#
274+
# If `decimal` is NaN, returns NaN.
275+
#
276+
# BigMath.tanh(BigDecimal('1'), 32).to_s
277+
# #=> "0.76159415595576488811945828260479e0"
278+
#
279+
def self?.tanh: (BigDecimal, Numeric) -> BigDecimal
280+
end

0 commit comments

Comments
 (0)