Skip to content

Commit a6d0db1

Browse files
authored
Make BigMath.exp and log also a module_method (#452)
1 parent d57013e commit a6d0db1

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

lib/bigdecimal.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def sqrt(prec)
238238
# Core BigMath methods for BigDecimal (log, exp) are defined here.
239239
# Other methods (sin, cos, atan) are defined in 'bigdecimal/math.rb'.
240240
module BigMath
241+
module_function
241242

242243
# call-seq:
243244
# BigMath.log(decimal, numeric) -> BigDecimal
@@ -251,7 +252,7 @@ module BigMath
251252
#
252253
# If +decimal+ is NaN, returns NaN.
253254
#
254-
def self.log(x, prec)
255+
def log(x, prec)
255256
prec = BigDecimal::Internal.coerce_validate_prec(prec, :log)
256257
raise Math::DomainError, 'Complex argument for BigMath.log' if Complex === x
257258

@@ -306,7 +307,7 @@ def self.log(x, prec)
306307
end
307308

308309
# Taylor series for exp(x) around 0
309-
private_class_method def self._exp_taylor(x, prec) # :nodoc:
310+
private_class_method def _exp_taylor(x, prec) # :nodoc:
310311
xn = BigDecimal(1)
311312
y = BigDecimal(1)
312313
1.step do |i|
@@ -328,7 +329,7 @@ def self.log(x, prec)
328329
#
329330
# If +decimal+ is NaN, returns NaN.
330331
#
331-
def self.exp(x, prec)
332+
def exp(x, prec)
332333
prec = BigDecimal::Internal.coerce_validate_prec(prec, :exp)
333334
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :exp)
334335
return BigDecimal::Internal.nan_computation_result if x.nan?

lib/bigdecimal/math.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def sinh(x, prec)
355355

356356
prec2 = prec + BigDecimal.double_fig
357357
prec2 -= x.exponent if x.exponent < 0
358-
e = BigMath.exp(x, prec2)
358+
e = exp(x, prec2)
359359
(e - BigDecimal(1).div(e, prec2)).div(2, prec)
360360
end
361361

@@ -377,7 +377,7 @@ def cosh(x, prec)
377377
return BigDecimal::Internal.infinity_computation_result if x.infinite?
378378

379379
prec2 = prec + BigDecimal.double_fig
380-
e = BigMath.exp(x, prec2)
380+
e = exp(x, prec2)
381381
(e + BigDecimal(1).div(e, prec2)).div(2, prec)
382382
end
383383

@@ -399,7 +399,7 @@ def tanh(x, prec)
399399
return BigDecimal(x.infinite?) if x.infinite?
400400

401401
prec2 = prec + BigDecimal.double_fig + [-x.exponent, 0].max
402-
e = BigMath.exp(x, prec2)
402+
e = exp(x, prec2)
403403
einv = BigDecimal(1).div(e, prec2)
404404
(e - einv).div(e + einv, prec)
405405
end
@@ -423,7 +423,7 @@ def asinh(x, prec)
423423
return -asinh(-x, prec) if x < 0
424424

425425
sqrt_prec = prec + [-x.exponent, 0].max + BigDecimal.double_fig
426-
BigMath.log(x + sqrt(x**2 + 1, sqrt_prec), prec)
426+
log(x + sqrt(x**2 + 1, sqrt_prec), prec)
427427
end
428428

429429
# call-seq:
@@ -444,7 +444,7 @@ def acosh(x, prec)
444444
return BigDecimal::Internal.infinity_computation_result if x.infinite?
445445
return BigDecimal::Internal.nan_computation_result if x.nan?
446446

447-
BigMath.log(x + sqrt(x**2 - 1, prec + BigDecimal.double_fig), prec)
447+
log(x + sqrt(x**2 - 1, prec + BigDecimal.double_fig), prec)
448448
end
449449

450450
# call-seq:
@@ -467,7 +467,7 @@ def atanh(x, prec)
467467
return -BigDecimal::Internal.infinity_computation_result if x == -1
468468

469469
prec2 = prec + BigDecimal.double_fig
470-
(BigMath.log(x + 1, prec2) - BigMath.log(1 - x, prec2)).div(2, prec)
470+
(log(x + 1, prec2) - log(1 - x, prec2)).div(2, prec)
471471
end
472472

473473
# call-seq:
@@ -492,7 +492,7 @@ def log2(x, prec)
492492
return BigDecimal::Internal.infinity_computation_result if x.infinite? == 1
493493

494494
prec2 = prec + BigDecimal.double_fig * 3 / 2
495-
v = BigMath.log(x, prec2).div(BigMath.log(BigDecimal(2), prec2), prec2)
495+
v = log(x, prec2).div(log(BigDecimal(2), prec2), prec2)
496496
# Perform half-up rounding to calculate log2(2**n)==n correctly in every rounding mode
497497
v = v.round(prec + BigDecimal.double_fig - (v.exponent < 0 ? v.exponent : 0), BigDecimal::ROUND_HALF_UP)
498498
v.mult(1, prec)
@@ -520,7 +520,7 @@ def log10(x, prec)
520520
return BigDecimal::Internal.infinity_computation_result if x.infinite? == 1
521521

522522
prec2 = prec + BigDecimal.double_fig * 3 / 2
523-
v = BigMath.log(x, prec2).div(BigMath.log(BigDecimal(10), prec2), prec2)
523+
v = log(x, prec2).div(log(BigDecimal(10), prec2), prec2)
524524
# Perform half-up rounding to calculate log10(10**n)==n correctly in every rounding mode
525525
v = v.round(prec + BigDecimal.double_fig - (v.exponent < 0 ? v.exponent : 0), BigDecimal::ROUND_HALF_UP)
526526
v.mult(1, prec)
@@ -539,7 +539,7 @@ def log1p(x, prec)
539539
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :log1p)
540540
raise Math::DomainError, 'Out of domain argument for log1p' if x < -1
541541

542-
return BigMath.log(x + 1, prec)
542+
return log(x + 1, prec)
543543
end
544544

545545
# call-seq:
@@ -565,7 +565,7 @@ def expm1(x, prec)
565565
else
566566
exp_prec = prec
567567
end
568-
exp_prec > 0 ? BigMath.exp(x, exp_prec).sub(1, prec) : BigDecimal(-1)
568+
exp_prec > 0 ? exp(x, exp_prec).sub(1, prec) : BigDecimal(-1)
569569
end
570570

571571

@@ -625,6 +625,6 @@ def PI(prec)
625625
#
626626
def E(prec)
627627
prec = BigDecimal::Internal.coerce_validate_prec(prec, :E)
628-
BigMath.exp(1, prec)
628+
exp(1, prec)
629629
end
630630
end

test/bigdecimal/test_bigmath.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ def test_consistent_precision_acceptance
9090
def test_coerce_argument
9191
f = 0.8
9292
bd = BigDecimal(f)
93-
assert_equal(BigMath.exp(bd, N), BigMath.exp(f, N))
94-
assert_equal(BigMath.log(bd, N), BigMath.log(f, N))
93+
assert_equal(exp(bd, N), exp(f, N))
94+
assert_equal(log(bd, N), log(f, N))
9595
assert_equal(sqrt(bd, N), sqrt(f, N))
9696
assert_equal(cbrt(bd, N), cbrt(f, N))
9797
assert_equal(hypot(bd, bd, N), hypot(f, f, N))
@@ -371,35 +371,35 @@ def test_atanh
371371

372372
def test_exp
373373
[-100, -2, 0.5, 10, 100].each do |x|
374-
assert_in_epsilon(Math.exp(x), BigMath.exp(BigDecimal(x, 0), N))
374+
assert_in_epsilon(Math.exp(x), exp(BigDecimal(x, 0), N))
375375
end
376-
assert_equal(1, BigMath.exp(BigDecimal("0"), N))
376+
assert_equal(1, exp(BigDecimal("0"), N))
377377
assert_in_exact_precision(
378378
BigDecimal("4.48168907033806482260205546011927581900574986836966705677265008278593667446671377298105383138245339138861635065183019577"),
379-
BigMath.exp(BigDecimal("1.5"), 100),
379+
exp(BigDecimal("1.5"), 100),
380380
100
381381
)
382-
assert_converge_in_precision {|n| BigMath.exp(BigDecimal("1"), n) }
383-
assert_converge_in_precision {|n| BigMath.exp(BigDecimal("-2"), n) }
384-
assert_converge_in_precision {|n| BigMath.exp(BigDecimal("-34"), n) }
385-
assert_converge_in_precision {|n| BigMath.exp(BigDecimal("567"), n) }
386-
assert_converge_in_precision {|n| BigMath.exp(SQRT2, n) }
382+
assert_converge_in_precision {|n| exp(BigDecimal("1"), n) }
383+
assert_converge_in_precision {|n| exp(BigDecimal("-2"), n) }
384+
assert_converge_in_precision {|n| exp(BigDecimal("-34"), n) }
385+
assert_converge_in_precision {|n| exp(BigDecimal("567"), n) }
386+
assert_converge_in_precision {|n| exp(SQRT2, n) }
387387
end
388388

389389
def test_log
390-
assert_equal(0, BigMath.log(BigDecimal("1.0"), 10))
391-
assert_in_epsilon(Math.log(10)*1000, BigMath.log(BigDecimal("1e1000"), 10))
390+
assert_equal(0, log(BigDecimal("1.0"), 10))
391+
assert_in_epsilon(Math.log(10)*1000, log(BigDecimal("1e1000"), 10))
392392
assert_in_exact_precision(
393393
BigDecimal("2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862"),
394-
BigMath.log(BigDecimal("10"), 100),
394+
log(BigDecimal("10"), 100),
395395
100
396396
)
397-
assert_converge_in_precision {|n| BigMath.log(BigDecimal("2"), n) }
398-
assert_converge_in_precision {|n| BigMath.log(BigDecimal("1e-30") + 1, n) }
399-
assert_converge_in_precision {|n| BigMath.log(BigDecimal("1e-30"), n) }
400-
assert_converge_in_precision {|n| BigMath.log(BigDecimal("1e30"), n) }
401-
assert_converge_in_precision {|n| BigMath.log(SQRT2, n) }
402-
assert_raise(Math::DomainError) {BigMath.log(BigDecimal("-0.1"), 10)}
397+
assert_converge_in_precision {|n| log(BigDecimal("2"), n) }
398+
assert_converge_in_precision {|n| log(BigDecimal("1e-30") + 1, n) }
399+
assert_converge_in_precision {|n| log(BigDecimal("1e-30"), n) }
400+
assert_converge_in_precision {|n| log(BigDecimal("1e30"), n) }
401+
assert_converge_in_precision {|n| log(SQRT2, n) }
402+
assert_raise(Math::DomainError) {log(BigDecimal("-0.1"), 10)}
403403
assert_separately(%w[-rbigdecimal], <<-SRC)
404404
begin
405405
x = BigMath.log(BigDecimal("1E19999999999999"), 10)
@@ -457,7 +457,7 @@ def test_log1p
457457
assert_raise(Math::DomainError) { log1p(BigDecimal("-1.01"), N) }
458458
assert_in_epsilon(Math.log(0.01), log1p(BigDecimal("-0.99"), N))
459459
assert_positive_infinite_calculation { log1p(PINF, N) }
460-
assert_in_exact_precision(BigMath.log(1 + BigDecimal("1e-20"), 100), log1p(BigDecimal("1e-20"), 100), 100)
460+
assert_in_exact_precision(log(1 + BigDecimal("1e-20"), 100), log1p(BigDecimal("1e-20"), 100), 100)
461461
end
462462

463463
def test_expm1
@@ -466,9 +466,9 @@ def test_expm1
466466
assert_equal(-1, expm1(BigDecimal("-400"), 100))
467467
assert_equal(-1, expm1(BigDecimal("-231"), 100))
468468
assert_not_equal(-1, expm1(BigDecimal("-229"), 100))
469-
assert_in_exact_precision(BigMath.exp(-220, 100) - 1, expm1(BigDecimal("-220"), 100), 100)
470-
assert_in_exact_precision(BigMath.exp(-3, 100) - 1, expm1(BigDecimal("-3"), 100), 100)
471-
assert_in_exact_precision(BigMath.exp(BigDecimal("1.23e-10"), 120) - 1, expm1(BigDecimal("1.23e-10"), 100), 100)
472-
assert_in_exact_precision(BigMath.exp(123, 120) - 1, expm1(BigDecimal("123"), 100), 100)
469+
assert_in_exact_precision(exp(-220, 100) - 1, expm1(BigDecimal("-220"), 100), 100)
470+
assert_in_exact_precision(exp(-3, 100) - 1, expm1(BigDecimal("-3"), 100), 100)
471+
assert_in_exact_precision(exp(BigDecimal("1.23e-10"), 120) - 1, expm1(BigDecimal("1.23e-10"), 100), 100)
472+
assert_in_exact_precision(exp(123, 120) - 1, expm1(BigDecimal("123"), 100), 100)
473473
end
474474
end

0 commit comments

Comments
 (0)