GeezerVison is showing github.com — Fix inaccurate calculation (last digit) and add a workaround for add/… · ruby/bigdecimal@5c6854b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/bigdecimal/math.rb +23-8Lines changed: 23 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -563,13 +563,23 @@ def expm1(x, prec) | 563 | 563 | if x < -1 | 564 | 564 | # log10(exp(x)) = x * log10(e) | 565 | 565 | lg_e = 0.4342944819032518 | 566 | | - exp_prec = prec + (lg_e * x).ceil + 2 | | 566 | + exp_prec = prec + (lg_e * x).ceil + BigDecimal.double_fig | 567 | 567 | elsif x < 1 | 568 | | - exp_prec = prec - x.exponent + 2 | | 568 | + exp_prec = prec - x.exponent + BigDecimal.double_fig | 569 | 569 | else | 570 | 570 | exp_prec = prec | 571 | 571 | end | 572 | | - exp_prec > 0 ? exp(x, exp_prec).sub(1, prec) : BigDecimal(-1) | | 572 | + | | 573 | + return BigDecimal(-1) if exp_prec <= 0 | | 574 | + | | 575 | + exp = exp(x, exp_prec) | | 576 | + | | 577 | + if exp.exponent > prec + BigDecimal.double_fig | | 578 | + # Workaroudn for https://github.com/ruby/bigdecimal/issues/464 | | 579 | + exp | | 580 | + else | | 581 | + exp.sub(1, prec) | | 582 | + end | 573 | 583 | end | 574 | 584 |
| 575 | 585 | # erf(decimal, numeric) -> BigDecimal | @@ -596,7 +606,12 @@ def erf(x, prec) | 596 | 606 | log10_erfc = -xf ** 2 / Math.log(10) - Math.log10(xf * Math::PI ** 0.5) | 597 | 607 | erfc_prec = [prec + log10_erfc.ceil, 1].max | 598 | 608 | erfc = _erfc_asymptotic(x, erfc_prec) | 599 | | - return BigDecimal(1).sub(erfc, prec) if erfc | | 609 | + if erfc | | 610 | + # Workaround for https://github.com/ruby/bigdecimal/issues/464 | | 611 | + return BigDecimal(1) if erfc.exponent < -prec - BigDecimal.double_fig | | 612 | + | | 613 | + return BigDecimal(1).sub(erfc, prec) | | 614 | + end | 600 | 615 | end | 601 | 616 |
| 602 | 617 | prec2 = prec + BigDecimal.double_fig | @@ -623,7 +638,7 @@ def erfc(x, prec) | 623 | 638 | x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :erfc) | 624 | 639 | return BigDecimal::Internal.nan_computation_result if x.nan? | 625 | 640 | return BigDecimal(1 - x.infinite?) if x.infinite? | 626 | | - return BigDecimal(1).sub(erf(x, prec), prec) if x < 0 | | 641 | + return BigDecimal(1).sub(erf(x, prec + BigDecimal.double_fig), prec) if x < 0 | 627 | 642 | return BigDecimal(0) if x > 5000000000 # erfc(5000000000) < 1e-10000000000000000000 (underflow) | 628 | 643 |
| 629 | 644 | if x >= 8 | @@ -714,12 +729,12 @@ def gamma(x, prec) | 714 | 729 | x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :gamma) | 715 | 730 | prec2 = prec + BigDecimal.double_fig | 716 | 731 | if x < 0.5 | 717 | | - raise Math::DomainError 'Numerical argument is out of domain - gamma' if x.frac.zero? | | 732 | + raise Math::DomainError, 'Numerical argument is out of domain - gamma' if x.frac.zero? | 718 | 733 |
| 719 | 734 | # Euler's reflection formula: gamma(z) * gamma(1-z) = pi/sin(pi*z) | 720 | 735 | pi = PI(prec2) | 721 | 736 | sin = _sinpix(x, pi, prec2) | 722 | | - return pi.div(gamma(1 - x, prec).mult(sin, prec2), prec) | | 737 | + return pi.div(gamma(1 - x, prec2).mult(sin, prec2), prec) | 723 | 738 | elsif x.frac.zero? && x < 1000 * prec | 724 | 739 | return _gamma_positive_integer(x, prec2).mult(1, prec) | 725 | 740 | end | @@ -747,7 +762,7 @@ def lgamma(x, prec) | 747 | 762 | # Euler's reflection formula: gamma(z) * gamma(1-z) = pi/sin(pi*z) | 748 | 763 | pi = PI(prec2) | 749 | 764 | sin = _sinpix(x, pi, prec2) | 750 | | - log_gamma = BigMath.log(pi, prec2).sub(lgamma(1 - x, prec).first + BigMath.log(sin.abs, prec2), prec) | | 765 | + log_gamma = BigMath.log(pi, prec2).sub(lgamma(1 - x, prec2).first + BigMath.log(sin.abs, prec2), prec) | 751 | 766 | [log_gamma, sin > 0 ? 1 : -1] | 752 | 767 | elsif x.frac.zero? && x < 1000 * prec | 753 | 768 | log_gamma = BigMath.log(_gamma_positive_integer(x, prec2), prec) | |
Collapse file Copy file name to clipboardExpand all lines: test/bigdecimal/helper.rb +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,7 +53,7 @@ def assert_converge_in_precision(&block) | 53 | 53 | [50, 100, 150].each do |n| | 54 | 54 | value = yield(n) | 55 | 55 | assert(value != expected, "Unable to estimate precision for exact value") | 56 | | - assert_in_exact_precision(expected, value, n) | | 56 | + assert_equal(expected.mult(1, n), value) | 57 | 57 | end | 58 | 58 | end | 59 | 59 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/bigdecimal/test_bigmath.rb +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -476,6 +476,7 @@ def test_expm1 | 476 | 476 | assert_in_exact_precision(exp(-3, 100) - 1, expm1(BigDecimal("-3"), 100), 100) | 477 | 477 | assert_in_exact_precision(exp(BigDecimal("1.23e-10"), 120) - 1, expm1(BigDecimal("1.23e-10"), 100), 100) | 478 | 478 | assert_in_exact_precision(exp(123, 120) - 1, expm1(BigDecimal("123"), 100), 100) | | 479 | + assert_equal(exp(BigDecimal("1e+12"), N), expm1(BigDecimal("1e+12"), N)) | 479 | 480 | end | 480 | 481 |
| 481 | 482 | def test_erf | @@ -486,6 +487,7 @@ def test_erf | 486 | 487 | assert_equal(-1, BigMath.erf(MINF, N)) | 487 | 488 | assert_equal(1, BigMath.erf(BigDecimal(1000), 100)) | 488 | 489 | assert_equal(-1, BigMath.erf(BigDecimal(-1000), 100)) | | 490 | + assert_equal(1, BigMath.erf(BigDecimal(10000000), 100)) | 489 | 491 | assert_not_equal(1, BigMath.erf(BigDecimal(10), 45)) | 490 | 492 | assert_not_equal(1, BigMath.erf(BigDecimal(15), 100)) | 491 | 493 | assert_equal(1, BigMath.erf(BigDecimal('1e400'), 10)) | @@ -524,6 +526,7 @@ def test_erfc | 524 | 526 | BigMath.erfc(BigDecimal("40"), 100) | 525 | 527 | ) | 526 | 528 | assert_converge_in_precision {|n| BigMath.erfc(BigDecimal(30), n) } | | 529 | + assert_converge_in_precision {|n| BigMath.erfc(BigDecimal(-2), n) } | 527 | 530 | assert_converge_in_precision {|n| BigMath.erfc(30 * SQRT2, n) } | 528 | 531 | assert_converge_in_precision {|n| BigMath.erfc(BigDecimal(50), n) } | 529 | 532 | assert_converge_in_precision {|n| BigMath.erfc(BigDecimal(60000), n) } | |
|