Skip to content

Commit 77c6048

Browse files
committed
Add major math functions to BigMath
See also: ruby/bigdecimal#336
1 parent 85ba59b commit 77c6048

File tree

2 files changed

+247
-0
lines changed

2 files changed

+247
-0
lines changed

stdlib/bigdecimal-math/0/big_math.rbs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,46 @@ module BigMath
3535
#
3636
def self?.PI: (Numeric prec) -> BigDecimal
3737

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+
3878
# <!--
3979
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
4080
# - atan(decimal, numeric) -> BigDecimal
@@ -49,6 +89,32 @@ module BigMath
4989
#
5090
def self?.atan: (BigDecimal x, Numeric prec) -> BigDecimal
5191

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+
52118
# <!--
53119
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
54120
# - cos(decimal, numeric) -> BigDecimal
@@ -65,6 +131,16 @@ module BigMath
65131

66132
# <!--
67133
# rdoc-file=ext/bigdecimal/bigdecimal.c
134+
# Computes the hyperbolic cosine of `decimal` to the specified number of digits
135+
# of precision, `numeric`.
136+
#
137+
# If `decimal` is NaN, returns NaN.
138+
#
139+
# BigMath.cosh(BigDecimal('1'), 32).to_s
140+
# #=> "0.15430806348152437784779056207571e1"
141+
#
142+
def self?.cosh: (BigDecimal, Numeric) -> BigDecimal
143+
68144
# - BigMath.exp(decimal, numeric) -> BigDecimal
69145
# -->
70146
# Computes the value of e (the base of natural logarithms) raised to the power
@@ -78,6 +154,14 @@ module BigMath
78154

79155
# <!--
80156
# rdoc-file=ext/bigdecimal/bigdecimal.c
157+
# Returns sqrt(x**2 + y**2) to the specified number of digits of precision,
158+
# `numeric`.
159+
#
160+
# BigMath.hypot(BigDecimal('1'), BigDecimal('2'), 32).to_s
161+
# #=> "0.22360679774997896964091736687313e1"
162+
#
163+
def self?.hypot: (BigDecimal, BigDecimal, Numeric) -> BigDecimal
164+
81165
# - BigMath.log(decimal, numeric) -> BigDecimal
82166
# -->
83167
# Computes the natural logarithm of `decimal` to the specified number of digits
@@ -91,6 +175,20 @@ module BigMath
91175
#
92176
def self?.log: (BigDecimal, Numeric prec) -> BigDecimal
93177

178+
# Computes the base 2 logarithm of `decimal` to the specified number of digits
179+
# of precision, `numeric`.
180+
#
181+
# If `decimal` is zero or negative, raises Math::DomainError.
182+
#
183+
# If `decimal` is positive infinity, returns Infinity.
184+
#
185+
# If `decimal` is NaN, returns NaN.
186+
#
187+
# BigMath.log2(BigDecimal('3'), 32).to_s
188+
# #=> "0.15849625007211561814537389439478e1"
189+
#
190+
def self?.log2: (BigDecimal, Numeric) -> BigDecimal
191+
94192
# <!--
95193
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
96194
# - sin(decimal, numeric) -> BigDecimal
@@ -105,6 +203,16 @@ module BigMath
105203
#
106204
def self?.sin: (BigDecimal x, Numeric prec) -> BigDecimal
107205

206+
# Computes the hyperbolic sine of `decimal` to the specified number of digits of
207+
# precision, `numeric`.
208+
#
209+
# If `decimal` is NaN, returns NaN.
210+
#
211+
# BigMath.sinh(BigDecimal('1'), 32).to_s
212+
# #=> "0.11752011936438014568823818505956e1"
213+
#
214+
def self?.sinh: (BigDecimal, Numeric) -> BigDecimal
215+
108216
# <!--
109217
# rdoc-file=ext/bigdecimal/lib/bigdecimal/math.rb
110218
# - sqrt(decimal, numeric) -> BigDecimal
@@ -116,4 +224,14 @@ module BigMath
116224
# #=> "0.1414213562373095048801688724e1"
117225
#
118226
def self?.sqrt: (BigDecimal x, Numeric prec) -> BigDecimal
227+
228+
# Computes the hyperbolic tangent of `decimal` to the specified number of digits
229+
# of precision, `numeric`.
230+
#
231+
# If `decimal` is NaN, returns NaN.
232+
#
233+
# BigMath.tanh(BigDecimal('1'), 32).to_s
234+
# #=> "0.76159415595576488811945828260479e0"
235+
#
236+
def self?.tanh: (BigDecimal, Numeric) -> BigDecimal
119237
end

test/stdlib/BigMath_test.rb

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,95 @@ def test_PI
1717
BigMath, :PI, 10
1818
end
1919

20+
def test_acos
21+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
22+
BigMath, :acos, BigDecimal('0.5'), 32
23+
end
24+
25+
def test_acosh
26+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
27+
BigMath, :acosh, BigDecimal('2'), 32
28+
end
29+
30+
def test_asin
31+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
32+
BigMath, :asin, BigDecimal('0.5'), 32
33+
end
34+
35+
def test_asinh
36+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
37+
BigMath, :asinh, BigDecimal('1'), 32
38+
end
39+
2040
def test_atan
2141
assert_send_type "(::BigDecimal x, ::Numeric prec) -> ::BigDecimal",
2242
BigMath, :atan, BigDecimal('1.23'), 10
2343
end
2444

45+
def test_atan2
46+
assert_send_type "(::BigDecimal, ::BigDecimal, ::Numeric) -> ::BigDecimal",
47+
BigMath, :atan2, BigDecimal('-1'), BigDecimal('1'), 32
48+
end
49+
50+
def test_atanh
51+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
52+
BigMath, :atanh, BigDecimal('0.5'), 32
53+
end
54+
55+
def test_cbrt
56+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
57+
BigMath, :cbrt, BigDecimal('2'), 32
58+
end
59+
2560
def test_cos
2661
assert_send_type "(::BigDecimal x, ::Numeric prec) -> ::BigDecimal",
2762
BigMath, :cos, BigDecimal('1.23'), 10
2863
end
2964

65+
def test_cosh
66+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
67+
BigMath, :cosh, BigDecimal('1'), 32
68+
end
69+
3070
def test_exp
3171
assert_send_type "(::BigDecimal, ::Numeric prec) -> ::BigDecimal",
3272
BigMath, :exp, BigDecimal('1.23'), 10
3373
end
3474

75+
def test_hypot
76+
assert_send_type "(::BigDecimal, ::BigDecimal, ::Numeric) -> ::BigDecimal",
77+
BigMath, :hypot, BigDecimal('1'), BigDecimal('2'), 32
78+
end
79+
3580
def test_log
3681
assert_send_type "(::BigDecimal, ::Numeric prec) -> ::BigDecimal",
3782
BigMath, :log, BigDecimal('1.23'), 10
3883
end
3984

85+
def test_log2
86+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
87+
BigMath, :log2, BigDecimal('3'), 32
88+
end
89+
4090
def test_sin
4191
assert_send_type "(::BigDecimal x, ::Numeric prec) -> ::BigDecimal",
4292
BigMath, :sin, BigDecimal('1.23'), 10
4393
end
4494

95+
def test_sinh
96+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
97+
BigMath, :sinh, BigDecimal('1'), 32
98+
end
99+
45100
def test_sqrt
46101
assert_send_type "(::BigDecimal x, ::Numeric prec) -> ::BigDecimal",
47102
BigMath, :sqrt, BigDecimal('1.23'), 10
48103
end
104+
105+
def test_tanh
106+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
107+
BigMath, :tanh, BigDecimal('1'), 32
108+
end
49109
end
50110

51111
class BigMathTest < Test::Unit::TestCase
@@ -67,23 +127,92 @@ def test_PI
67127
TestClass.new, :PI, 10
68128
end
69129

130+
def test_acos
131+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
132+
TestClass.new, :acos, BigDecimal('0.5'), 32
133+
end
134+
135+
def test_acosh
136+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
137+
TestClass.new, :acosh, BigDecimal('2'), 32
138+
end
139+
140+
def test_asin
141+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
142+
TestClass.new, :asin, BigDecimal('0.5'), 32
143+
end
144+
145+
def test_asinh
146+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
147+
TestClass.new, :asinh, BigDecimal('1'), 32
148+
end
149+
70150
def test_atan
71151
assert_send_type "(::BigDecimal x, ::Numeric prec) -> ::BigDecimal",
72152
TestClass.new, :atan, BigDecimal('1.23'), 10
73153
end
74154

155+
def test_atan2
156+
assert_send_type "(::BigDecimal, ::BigDecimal, ::Numeric) -> ::BigDecimal",
157+
TestClass.new, :atan2, BigDecimal('-1'), BigDecimal('1'), 32
158+
end
159+
160+
def test_atanh
161+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
162+
TestClass.new, :atanh, BigDecimal('0.5'), 32
163+
end
164+
165+
def test_cbrt
166+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
167+
TestClass.new, :cbrt, BigDecimal('2'), 32
168+
end
169+
75170
def test_cos
76171
assert_send_type "(::BigDecimal x, ::Numeric prec) -> ::BigDecimal",
77172
TestClass.new, :cos, BigDecimal('1.23'), 10
78173
end
79174

175+
def test_cosh
176+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
177+
TestClass.new, :cosh, BigDecimal('1'), 32
178+
end
179+
180+
def test_exp
181+
assert_send_type "(::BigDecimal, ::Numeric prec) -> ::BigDecimal",
182+
TestClass.new, :exp, BigDecimal('1.23'), 10
183+
end
184+
185+
def test_hypot
186+
assert_send_type "(::BigDecimal, ::BigDecimal, ::Numeric) -> ::BigDecimal",
187+
TestClass.new, :hypot, BigDecimal('1'), BigDecimal('2'), 32
188+
end
189+
190+
def test_log
191+
assert_send_type "(::BigDecimal, ::Numeric prec) -> ::BigDecimal",
192+
TestClass.new, :log, BigDecimal('1.23'), 10
193+
end
194+
195+
def test_log2
196+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
197+
TestClass.new, :log2, BigDecimal('3'), 32
198+
end
80199
def test_sin
81200
assert_send_type "(::BigDecimal x, ::Numeric prec) -> ::BigDecimal",
82201
TestClass.new, :sin, BigDecimal('1.23'), 10
83202
end
84203

204+
def test_sinh
205+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
206+
TestClass.new, :sinh, BigDecimal('1'), 32
207+
end
208+
85209
def test_sqrt
86210
assert_send_type "(::BigDecimal x, ::Numeric prec) -> ::BigDecimal",
87211
TestClass.new, :sqrt, BigDecimal('1.23'), 10
88212
end
213+
214+
def test_tanh
215+
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
216+
TestClass.new, :tanh, BigDecimal('1'), 32
217+
end
89218
end

0 commit comments

Comments
 (0)