Skip to content

Commit 8101bbf

Browse files
committed
Add BigMath.frexp and ldexp with exponent of 10
See also: ruby/bigdecimal#448
1 parent c2edc97 commit 8101bbf

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

stdlib/bigdecimal-math/0/big_math.rbs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ module BigMath
172172
#
173173
def self?.exp: (BigDecimal, Numeric prec) -> BigDecimal
174174

175+
# Decomposes +x+ into a normalized fraction and an integral power of ten.
176+
#
177+
# BigMath.frexp(BigDecimal(123.456))
178+
# #=> [0.123456e0, 3]
179+
#
180+
def self?.frexp: (BigDecimal) -> [ BigDecimal, Integer ]
181+
175182
# Computes the gamma function of +decimal+ to the specified number of
176183
# digits of precision, +numeric+.
177184
#
@@ -190,6 +197,14 @@ module BigMath
190197
#
191198
def self?.hypot: (BigDecimal, BigDecimal, Numeric) -> BigDecimal
192199

200+
# Inverse of +frexp+.
201+
# Returns the value of fraction * 10**exponent.
202+
#
203+
# BigMath.ldexp(BigDecimal("0.123456e0"), 3)
204+
# #=> 0.123456e3
205+
#
206+
def self?.ldexp: (BigDecimal, Integer) -> BigDecimal
207+
193208
# Computes the natural logarithm of the absolute value of the gamma function
194209
# of +decimal+ to the specified number of digits of precision, +numeric+ and its sign.
195210
#

test/stdlib/BigMath_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ def test_exp
8282
BigMath, :exp, BigDecimal('1.23'), 10
8383
end
8484

85+
def test_frexp
86+
assert_send_type "(::BigDecimal) -> [::BigDecimal, ::Integer]",
87+
BigMath, :frexp, BigDecimal(123.456)
88+
end
89+
8590
def test_gamma
8691
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
8792
BigMath, :gamma, BigDecimal('0.5'), 32
@@ -92,6 +97,11 @@ def test_hypot
9297
BigMath, :hypot, BigDecimal('1'), BigDecimal('2'), 32
9398
end
9499

100+
def test_ldexp
101+
assert_send_type "(::BigDecimal, ::Integer) -> ::BigDecimal",
102+
BigMath, :ldexp, BigDecimal("0.123456e0"), 3
103+
end
104+
95105
def test_lgamma
96106
assert_send_type "(::BigDecimal, ::Numeric) -> [::BigDecimal, ::Integer]",
97107
BigMath, :lgamma, BigDecimal('0.5'), 32
@@ -212,6 +222,11 @@ def test_exp
212222
TestClass.new, :exp, BigDecimal('1.23'), 10
213223
end
214224

225+
def test_frexp
226+
assert_send_type "(::BigDecimal) -> [::BigDecimal, ::Integer]",
227+
TestClass.new, :frexp, BigDecimal(123.456)
228+
end
229+
215230
def test_gamma
216231
assert_send_type "(::BigDecimal, ::Numeric) -> ::BigDecimal",
217232
TestClass.new, :gamma, BigDecimal('0.5'), 32
@@ -222,6 +237,11 @@ def test_hypot
222237
TestClass.new, :hypot, BigDecimal('1'), BigDecimal('2'), 32
223238
end
224239

240+
def test_ldexp
241+
assert_send_type "(::BigDecimal, ::Integer) -> ::BigDecimal",
242+
TestClass.new, :ldexp, BigDecimal("0.123456e0"), 3
243+
end
244+
225245
def test_lgamma
226246
assert_send_type "(::BigDecimal, ::Numeric) -> [::BigDecimal, ::Integer]",
227247
TestClass.new, :lgamma, BigDecimal('0.5'), 32

0 commit comments

Comments
 (0)