GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix lgamma precision around 1 and 2 by tompng · Pull Request #466 · ruby/bigdecimal

Conversation

@tompng

@tompng tompng commented Dec 11, 2025

Copy link
Copy Markdown
Member

Followup of #451

When calculating lgamma(2 + 1e-50, 100), internal calculation needs 100+50 digit precision.
When calculating lgamma(2 + 1e-10000, 100), internal calculation needs 100+10000 digits precision but this is too slow.
Fortunately, in this case, linear interpolation between x=2 and x=2+1e-116 is enough to calculate lgamma(2+1e-10000).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tompng
Fix lgamma precision around 1 and 2
cb2f967
Increase internal calculation digits for lgamma when x is near 1 or 2.
Perform linear interpolation if x is extreamely close to 1 or 2.
@tompng

tompng commented Dec 11, 2025

Copy link
Copy Markdown
Member Author

This bug is found by this script:

d1 = BigDecimal('1e-100')
sqrt2 = BigDecimal(2).sqrt(200)
d2 = d1 * sqrt2
def BigMath.lgamma_0(x, prec) = BigMath.lgamma(x, prec)[0]

%i[
  sin cos tan asin acos atan sinh cosh tanh asinh acosh atanh
  sqrt cbrt log2 log10 exp log erf erfc gamma lgamma_0
].each do |m|
  p m
  [-2, -1, 0, 0.5, 1, 2, 10, 100, sqrt2, sqrt2 / 2].each do |x0|
    [d1, d2, 0, -d1, -d2].each do |d|
      x = BigDecimal(x0, 0) + d
      precs = (1..10).map { |i| 10 * i**2 }
      precs = precs.take(6) if m in :gamma | :lgamma
      true_value = BigMath.send(m, x, precs.last + 100)
      precs.each do |prec|
        v = BigMath.send(m, x, prec)
        unless v == true_value.mult(1, prec)
          p BUG: [m, prec]
          binding.irb
        end
      end
    rescue Math::DomainError, FloatDomainError
    end
  end
end

Other found bugs are fixed in #465

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tompng
tompng merged commit bfb1cb3 into ruby:master Dec 11, 2025
@tompng
tompng deleted the lgamma_around_1_2 branch December 11, 2025 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@tompng
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.