Skip to content

PROB: the weights must total 1 to 15 digits, as in Excel - #4379

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/prob-weights-total-one
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/prob-weights-total-one

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4284

What was wrong

prob in crates/functions/src/stat/descriptive.rs accepted weights whose total was within 1e-7 of 1, the tolerance LibreOffice uses. Excel's is tighter. With weights 0.5 and 0.50000001, =PROB(A1:A2,B1:B2,1,2) gave 1.00000001, more than any probability can be, and =PROB(A1:A2,B1:B2,1) gave 0.5. Excel gives #NUM! for both, and for 0.49999999 too, as the issue shows.

What changed

The total must equal 1 the way Excel compares numbers: gridcraft_core::compare_numbers, which rounds both sides to 15 significant digits.

  • Binary noise in the sum doesn't count. Ten weights of 0.1 sum to 0.9999999999999999 and still give 1.
  • A total the 15 digits tell apart from 1 is #NUM!.

The per-weight check (each in (0, 1]) is unchanged.

QA report

Main vs branch, end to end (gridcraft-cli run)

Each case has 1 and 2 in column A, and 0.5 and the case's weight in column B. C is =PROB(A,B,1,2), D is =PROB(A,B,1) and E is =SUM(B). The sheet was printed and saved as .xlsx, then the .xlsx was reopened and evaluated. main is a gridcraft-cli built from origin/main (abcfc191); the branch is this PR (0d666c91). The Excel column is the issue's result; I didn't run Excel myself.

Second weight main: PROB 1–2 / PROB 1 this PR Excel (issue)
0.50000001 1.00000001 / 0.5 #NUM! / #NUM! #NUM! / #NUM!
0.49999999 0.99999999 / 0.5 #NUM! / #NUM! #NUM! (deficit side)
0.5 (control) 1 / 0.5 1 / 0.5 1 for the interval
0.500001 (control) #NUM! / #NUM! #NUM! / #NUM! #NUM!
0.4999999999999 1 / 0.5 #NUM! / #NUM! not in the issue
ten weights of 0.1, outcomes 0–9 (control) 0.9999999999999999 0.9999999999999999

Reopened from the saved .xlsx, C1 and D1 are 1.00000001 0.5 on main, and #NUM! on this branch.

Test

stat::tests::frequency_prob now also covers:

  • 0.50000001, 0.49999999, 0.500001 and 0.4999999999999 as the second weight: each is #NUM! for the interval and for one outcome;
  • 0.5: 1 and 0.5;
  • binary noise: ten weights of 0.1, and 0.7 + 0.2 + 0.1. Both sums are 0.9999999999999999, and the test checks that before using them.

The existing PROB cases (limits, weights of 0, a total of 2) still pass.

Negative control

With the 1e-7 tolerance back (on top of 0d666c91, then restored with git checkout), the test fails: left Number(1.00000001), right Error(Num).

Gate (macOS arm64, at 0d666c91)

Step Result
cargo fmt --all -- --check clean
cargo clippy -p gridcraft-functions --all-targets -- -D warnings clean
cargo test -p gridcraft-functions -p gridcraft-calc -p gridcraft-engine 130 (+3 ignored) + 1004 (+1 ignored) + 120 + 7 + 183 passed, 0 failed

Not covered

  • Excel's exact limit: the issue pins it below 1e-8 (1.00000001 and 0.99999999 are refused, 1 is accepted). Between that and binary noise, this PR uses Excel's 15-digit number comparison. The 0.4999999999999 row (a total 1e-13 short of 1) follows that rule, not a result from Excel.
  • Excel: the Excel column is the issue's results.
  • cargo xtask ci: I didn't run the full cargo xtask ci (wasm, layers, the other crates).

Summary by CodeRabbit

  • Bug Fixes
    • PROB now returns #NUM! when probability weights do not total 1, including values slightly above or below 1. Weights that total 1 remain accepted, including decimal values affected by floating-point rounding.

PROB accepted weights whose total was within 1e-7 of 1, so weights of
0.5 and 0.50000001 gave about 1.00000001 for the whole interval where
Excel gives #NUM!. The total is now compared with 1 the way Excel
compares numbers (compare_numbers, 15 significant digits): binary noise
such as ten weights of 0.1 still passes.

Fixes storytold#4284
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

prob now checks whether the total weight equals 1 using gridcraft_core::compare_numbers. Tests cover totals slightly above or below 1, exact totals, and decimal weights with floating-point summation noise.

Changes: PROB weight-total validation

Layer / File(s) Summary
Weight-total validation and regression tests
crates/functions/src/stat/descriptive.rs, crates/functions/src/stat/tests.rs
prob replaces the absolute tolerance check with a numeric comparison against 1.0. Tests verify that near-one totals return #NUM! and that exact totals and decimal weights return expected probabilities.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: aiongg, harrison-booth

Merge Risk: Minimal · up to 0d666

PROB now rejects weight totals that differ from 1 at Excel's 15-digit precision, as the linked issue asks. No concrete merge-blocking risk remains. Very long distributions whose binary sum drifts by about 1e-13 could be rejected, but no evidence shows Excel accepts them.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: PROB now requires the weights to total 1 to 15 significant digits, matching Excel. The PR replaces the 1e-7 absolute tolerance with a compare_numbers check that rounds…
Linked Issues check Passed Issue #4284 requires the weight-total check for positive PROB weights to reject totals that differ from 1 (for example 1.00000001 and 0.99999999 return #NUM!, 1.000001 returns #NUM!), while a total of…
Out of Scope Changes check Passed The whole-PR diff against merge base abcfc19 touches only two files. descriptive.rs changes three lines of the total-weight check, with a comment that cites #4284. tests.rs adds 16 lines of PROB test…
Full details: Title check

Explanation

The title names the main change: PROB now requires the weights to total 1 to 15 significant digits, matching Excel. The PR replaces the 1e-7 absolute tolerance with a compare_numbers check that rounds to 15 significant digits, and the linked issue #4284 asks for the same Excel-compatible total-weight behavior. The title is specific and concise, and it covers the primary change.

Full details: Linked Issues check

Explanation

Issue #4284 requires the weight-total check for positive PROB weights to reject totals that differ from 1 (for example 1.00000001 and 0.99999999 return #NUM!, 1.000001 returns #NUM!), while a total of exactly 1 is accepted. At head 0d666c9, crates/functions/src/stat/descriptive.rs replaces the check (total - 1.0).abs() > 1e-7 with gridcraft_core::compare_numbers(total, 1.0) != Ordering::Equal, which returns CellError::Num on mismatch. Under 15-significant-digit comparison, 1.00000001, 0.99999999, and 1.000001 are not equal to 1 and return #NUM!. A total of 1 and binary noise such as ten weights of 0.1 compare equal and are accepted. The per-weight check (each in (0, 1]) is unchanged, so #539 (zero weights) is not altered. crates/functions/src/stat/tests.rs adds cases for the issue fixtures (0.50000001, 0.49999999, 0.500001) for both the full interval and a single outcome, plus a 0.5 control and binary-noise sums. The diff reads as implementing the requirement. The compile and test results come from the PR description's reported gate run; I did not rebuild or run the tests. Excel's exact tolerance is not pinned by the issue beyond the stated fixtures, so the 15-digit rule is an approximation for totals between about 1e-15 and 1e-8 from 1.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against merge base abcfc19 touches only two files. descriptive.rs changes three lines of the total-weight check, with a comment that cites #4284. tests.rs adds 16 lines of PROB test cases that exercise the changed check. Both changes connect directly to the linked issue. No unrelated files or behavior changes appear in the diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between abcfc19 and 0d666c9.

Files selected for processing: 2

  • crates/functions/src/stat/descriptive.rs
  • crates/functions/src/stat/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.

emrahoksak pushed a commit to emrahoksak/girdcraft- that referenced this pull request Nov 18, 2026

This branch has not been deployed

No deployments
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.

PROB accepts positive weights that total 1.00000001

1 participant