Skip to content

fix: enforce incomplete gamma argument domains - #1203

Open
truemagic-coder wants to merge 2 commits into
fortran-lang:masterfrom
truemagic-coder:fix-898-gamma-diagnostics
Open

fix: enforce incomplete gamma argument domains#1203
truemagic-coder wants to merge 2 commits into
fortran-lang:masterfrom
truemagic-coder:fix-898-gamma-diagnostics

Conversation

@truemagic-coder

@truemagic-coder truemagic-coder commented Aug 17, 2026

Copy link
Copy Markdown

Summary

Fix incomplete gamma handling to match the documented stdlib specification.

For x >= 0, p may be a positive integer or positive real.

For x < 0, p must be a positive integer. Real-p overloads reject the call.

Changes

  • Keep negative-x support for integer-p overloads.
  • Reject negative x in real-p overloads.
  • Validate p > 0 before special-case returns.
  • Fix log_lower_incomplete_gamma(p, 0) to return -Inf.
  • Remove the invalid real-p negative-x path.
  • Keep generated gamma code clean under strict uninitialized diagnostics.

Tests

Added coverage for:

  • positive integer and non-integer p with nonnegative x
  • positive integer p with negative x
  • rejection of real p with negative x
  • rejection of zero and negative p
  • lower and upper incomplete gamma
  • logarithmic lower and upper incomplete gamma
  • log_lower_incomplete_gamma(p, 0) == -Inf
  • generated single- and double-precision paths

Negative-x integer-p regressions use a relative tolerance of 128*epsilon(kind).

Verification

GNU Fortran 15.2.0 locally:

  • focused gamma tests: 2/2 passed
  • strict -Werror=maybe-uninitialized check: passed
  • full stdlib suite: 428/428 passed twice

Independent verification by FalseGreen also passed the branch-specific gamma/specification gates.

Assisted-By: Codex

@truemagic-coder
truemagic-coder marked this pull request as ready for review August 17, 2026 06:02

@jvdp1 jvdp1 left a comment

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.

Thank youj for this fix, @truemagic-coder .
Should the specs be also updated?

@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.33%. Comparing base (9a15c77) to head (32979dc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1203      +/-   ##
==========================================
+ Coverage   68.20%   68.33%   +0.12%     
==========================================
  Files          19       19              
  Lines        2378     2378              
==========================================
+ Hits         1622     1625       +3     
+ Misses        756      753       -3     

☔ View full report in Codecov by Harness.
📢 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.

@truemagic-coder

Copy link
Copy Markdown
Author

I missed the documented constraint for negative x. For x < 0, p must be a positive integer.

I’m fixing the implementation and tests against the spec now, then I’ll rerun the full test suite and verification.

@truemagic-coder

Copy link
Copy Markdown
Author

I found two contract issues while checking this against the spec.

Real p must reject negative x. Also, the public wrappers currently return early for x == 0, which lets p <= 0 bypass validation.

I’m fixing both and adding explicit rejection tests before pushing the update.

@truemagic-coder

truemagic-coder commented Aug 18, 2026

Copy link
Copy Markdown
Author

Hi @jvdp1

I missed part of the written spec in the first pass.

After going back through it, the spec does not need to be updated. It is already clear that for x < 0, p must be a positive integer.

I found a few implementation issues while aligning the code to that contract.

Real p now rejects negative x, and p <= 0 is checked before the x == 0 special cases. log_lower_incomplete_gamma(p, 0) also returned 0 instead of -Inf, which is now fixed.

Those cases now have regression coverage.

The focused gamma tests pass, the strict compiler diagnostic passes, and the full suite passes 428/428 twice locally with GNU Fortran 15.2.0.

Updated commit: 32979dc.

Thanks!

@truemagic-coder truemagic-coder changed the title fix: repair negative-argument incomplete gamma fix: enforce incomplete gamma argument domains Aug 18, 2026
@jvdp1
jvdp1 requested a lite review from Copilot August 18, 2026 12:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates stdlib_specialfunctions_gamma to enforce the documented argument domains for incomplete gamma routines, especially around negative x handling and p > 0 validation, and expands the test suite to cover these domain rules and edge cases like log_lower_incomplete_gamma(p, 0).

Changes:

  • Enforce p > 0 checks and reject negative x for real-p incomplete gamma overloads; preserve negative-x behavior for integer-p overloads.
  • Fix log_lower_incomplete_gamma(p, 0) to return -Inf (instead of 0).
  • Add new tests for negative-x integer-p behavior, x=0 log-lower behavior, and invalid-argument rejection (via a separate test program).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/specialfunctions/test_specialfunctions_gamma.fypp Adds new positive/negative-domain and x=0 log-lower tests.
test/specialfunctions/test_specialfunctions_gamma_invalid.fypp New test program that asserts invalid domains are rejected.
test/specialfunctions/CMakeLists.txt Registers the new invalid-argument test in CMake.
src/specialfunctions/stdlib_specialfunctions_gamma.fypp Enforces domain rules, fixes log_lower_incomplete_gamma(p,0), and addresses uninitialized-result warnings.
CHANGELOG.md Notes the incomplete-gamma domain enforcement and diagnostics fix under Unreleased.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/specialfunctions/stdlib_specialfunctions_gamma.fypp
Comment thread src/specialfunctions/stdlib_specialfunctions_gamma.fypp
Comment thread test/specialfunctions/test_specialfunctions_gamma_invalid.fypp
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.

3 participants