Skip to content

Fix Division-by-Zero in Masked Statistical Functions#1122

Open
JAi-SATHVIK wants to merge 5 commits intofortran-lang:masterfrom
JAi-SATHVIK:fix-mean-mask-upstream
Open

Fix Division-by-Zero in Masked Statistical Functions#1122
JAi-SATHVIK wants to merge 5 commits intofortran-lang:masterfrom
JAi-SATHVIK:fix-mean-mask-upstream

Conversation

@JAi-SATHVIK
Copy link
Contributor

Masked statistical functions (mean, variance, correlation, and covariance) currently crash or return NaN when provided with an "all-false" mask. This is caused by a missing guard against zero counts in denominators specifically when count(mask) is 0 leading to critical division-by-zero errors during runtime.

@codecov
Copy link

codecov bot commented Feb 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.53%. Comparing base (8116e5c) to head (88f13a0).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1122      +/-   ##
==========================================
- Coverage   68.55%   68.53%   -0.02%     
==========================================
  Files         396      396              
  Lines       12746    12746              
  Branches     1376     1376              
==========================================
- Hits         8738     8736       -2     
- Misses       4008     4010       +2     

☔ 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.

#:endif
mask_)

if (count(mask_) < 2) then
Copy link
Contributor

@jalvesz jalvesz Feb 16, 2026

Choose a reason for hiding this comment

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

Instead of introducing another internal implicit loop (using count) it might be better to store the denominator in its own local variable and check whether the denominator is equal to zero or not to assign NaN. E.g:

denom = dot_product( centeri_, centeri_)*dot_product( centerj_, centerj_)
if (denom<epsilon(0._${k1}$)) then
     res(j, i) = ieee_value(1._${k1}$, ieee_quiet_nan)
else
     res(j, i) = dot_product( centerj_, centeri_)/sqrt(denom)
end if

same applies elsewhere

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