Skip to content

Float32 loggamma#5

Open
TSGut wants to merge 3 commits intoJuliaMath:mainfrom
TSGut:main
Open

Float32 loggamma#5
TSGut wants to merge 3 commits intoJuliaMath:mainfrom
TSGut:main

Conversation

@TSGut
Copy link
Copy Markdown
Collaborator

@TSGut TSGut commented Apr 8, 2026

Perhaps something more sophisticated can be done but this is just the same methods but without upconverting to Float64, dropping some terms in the series evals and creating the constants as Float32 literals too.

I tried doing this with Float16 too but Float16 has no convenient constructors and was a lot more subtle on what a good quality approximation should return, so I left it for now - though I did change it to upconvert to Float32 instead of Float64.

I also significantly hardened the test cases, now we tests 10k+ cases for real and complex values of Float16, Float32 and Float64, hopefully this will contribute towards preventing accuracy regression in the future.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3c1b159) to head (53725c3).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main        #5   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          281       372   +91     
=========================================
+ Hits           281       372   +91     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TSGut
Copy link
Copy Markdown
Collaborator Author

TSGut commented Apr 8, 2026

Tests & coverage passing, ready for review or merge.

@oscardssmith I have contributor rights on the repo obv but I think it's a good idea to have a second pair of eyes on code before merging.

end
end

function _loggamma(x::Float32)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we unify this (ideally with both the Float64 and BigFloat) nothing that we're doing here should be Float32 specific.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we can, I split it because the implementations may diverge in the future but I'm happy to merge them with type based dispatch if you prefer that

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bigfloat we can't, because the polynomial approximations that are hard coded don't scale to arbitrary precision, that's why different approaches have to be used for bigfloats

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of what I think could be unified for the big float version is the negative integer/nonfinite checks

elseif isinf(x)
return x > 0 ? (x, 1) : (BigFloat(NaN), 1)
elseif x > 0
return real(_loggamma_complex_bigfloat(Complex{BigFloat}(x, zero(BigFloat)))), 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any easy optimizations for real gamma for BigFloat? I'd think that at the very least the polynomials should be nicer...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, squeezing out optimal for bigfloats is hard but better than routing to complex bigfloats is easy. I'll give it a shot

zinv * @evalpoly(t, _STIRLING_COEFFS_32...)
end

function _loggamma(z::Complex{Float32})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly to above, can this be unified with the other Complex{T} methods?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants