Skip to content

fix(functions): reject negative fractions in GCD and LCM - #2072

Merged
FredApps merged 4 commits into
storytold:mainfrom
MMexicann:fix/gcd-lcm-negative-fractions
Nov 14, 2026
Merged

FredApps merged 4 commits into
storytold:mainfrom
MMexicann:fix/gcd-lcm-negative-fractions

Conversation

@MMexicann

@MMexicann MMexicann commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

GCD(-0.5, 2) returns 2 and LCM(-0.5, 2) returns 0 on main: the shared integer helper truncates negative fractions to negative zero before validating their sign. Check the original argument first so both return #NUM!.

The regression covers scalar and range arguments, a zero before an invalid negative value, positive fractional truncation, and signed zero. Function parity notes record this behavior; counts and estimates are unchanged.

Behavior reference: Microsoft’s public GCD and LCM documentation specifies #NUM! for negative arguments and truncation for nonintegers. No desktop Excel oracle check is claimed.

Refreshed on upstream main 632e4a1bc51ee86b3f0a55e95ee78def391aab30 on 2026-11-04. The merge preserves the extracted mod tests; layout and moves the original regression verbatim into crates/functions/src/math/tests.rs.

Validation on Windows with Rust 1.99 (GNULLVM):

  • Repository cargo fmt --all -- --check passes.
  • The exact current core and functions sources were copied into an isolated workspace with the same dependency constraints and lints, using cached dependencies. The regression fails with the original predicate (GCD(-0.1, 2) returns 2), and all 19 math tests pass with the fix.
  • Isolated cargo clippy -p gridcraft-functions --all-targets -- -D warnings and the wasm32-unknown-unknown check pass.
  • The isolated full functions run has 161 passing tests and one unchanged failure in engineering::tests::complex_numbers: an expected final digit .547i is .548i on this toolchain. The failure also occurs with the original predicate.
  • cargo --offline xtask ci cannot resolve the uncached pinned egui revision 6d518fc7; local workspace gates could not run.

Hosted integration validation at 4e4bd75e31cd11db5c1773b34c80d253d97bb295: CI / gate / gate (ubuntu-24.04) passed, running cargo xtask ci. SonarCloud also passed. This supplies the full workspace integration evidence beyond the local isolated checks.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • GCD and LCM now return #NUM! for negative fractional inputs, including values in ranges and inputs paired with zero.
    • Positive fractions are still truncated before calculation, and signed zero remains valid.
  • Documentation

    • Updated the function parity documentation to describe the GCD and LCM behavior.

Validate the original input before truncation. Add scalar and range regressions, including signed zero, positive fractions and zero-first LCM validation.
Record tested edge-case behavior and update the parity revision history. Function counts and readiness estimates remain unchanged.
@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

GCD and LCM now validate each original numeric argument before truncation. Negative fractional arguments return #NUM!. Regression tests and the function-parity document cover this behavior and the handling of signed zero and positive fractions.

Changes: GCD and LCM validation

Layer / File(s) Summary
Argument validation and behavior coverage
crates/functions/src/math.rs, crates/functions/src/math/tests.rs, docs/function-parity.md
integer_args checks each original value against the allowed range before converting its truncated value to u64. Tests cover negative fractions passed directly or through ranges, zero-containing inputs, signed zero, and positive-fraction truncation. The parity document records the behavior and update date.

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

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: aiongg

Merge Risk: Minimal · up to 4e4bd

The validation change and regression coverage align with the stated behavior, so the PR appears ready to merge.

Architecture risk: Low · up to 570d0

The change affects 2 systems.

Changed systems: crates, docs

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — crates (service) was modified; 1 changed file maps to changed impact.
  • observed — docs (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in crates/functions/src/math.rs: integer_args now compares each original value, rather than its truncated value, against the valid nonnegative integer range before converting it to u64; negative fractions therefore produce #NUM!.
  • observed — Modified behavior in crates/functions/src/math.rs: Added tests confirming GCD and LCM reject negative fractional arguments in scalar and array forms, including when another argument is zero, while accepting negative zero and truncating positive fractional inputs.
  • observed — Modified behavior in docs/function-parity.md: Updated the review metadata from the MEDIAN/AGGREGATE overflow correction to the GCD/LCM negative-fraction validation correction, including the new update date.
  • observed — Modified behavior in docs/function-parity.md: Added documentation that GCD and LCM validate original argument signs before truncation: negative fractions return #NUM!, including in ranges and alongside zero; positive fractions truncate and signed zero remains valid.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(functions): reject negative fractions in GCD and LCM" matches the main change. The raw summary shows changes to math.rs integer_args, which now checks the original value for range befor…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(functions): reject negative fractions in GCD and LCM" matches the main change. The raw summary shows changes to math.rs integer_args, which now checks the original value for range before truncation, plus docs and regression tests for GCD and LCM with negative fractional arguments. The title is specific, short, and in conventional-commit form.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

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 570d020 and 4e4bd75.

Files selected for processing: 3
  • crates/functions/src/math.rs
  • crates/functions/src/math/tests.rs
  • docs/function-parity.md
Files skipped from review as they are similar to previous changes: 1
  • docs/function-parity.md

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

@FredApps FredApps 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.

Read the whole change at 570d020.

integer_args in crates/functions/src/math.rs now checks the original value x against 0.0..9.007_199_254_740_992e15 instead of the truncated t, so -0.1, -0.5 and -0.999 give #NUM! rather than truncating to 0 and passing. -0.0 is still inside the range, NaN is outside it, and the upper bound is unchanged. The conversion t as u64 still uses the truncated value, so 12.9 and 18.9 still become 12 and 18.

The new test covers the scalar form, the range form (rf(row(&[2.0, x]))), a zero beside the negative fraction, signed zero, and the positive truncation case (GCD 6, LCM 36). Those are the cases the change could break. The parity doc and revision history are updated to match.

One point I have not checked against Excel itself: that Excel returns #NUM! for a negative fraction in GCD and LCM. The PR states it and the sign check matches Excel's documented rule for negative arguments.

CI at this head: cargo xtask ci is green.

@Harrison-Booth

Copy link
Copy Markdown
Contributor

I checked these formulas in a new, unsaved Excel workbook through Windows PowerShell COM (each formula entered into a cell and calculated):

Formula Excel result
=GCD(-0.1,2) #NUM!
=LCM(-0.1,2) #NUM!
=GCD(-0.9,2) #NUM!
=LCM(-0.9,2) #NUM!
=GCD(-1.1,2) #NUM!
=LCM(-1.1,2) #NUM!

These Excel results agree with rejecting the negative input before truncating it to an integer.

Move the original negative-fraction regression into the existing extracted math tests. Scalar and range cases, zero-first validation, signed zero and positive truncation coverage are unchanged.
@MMexicann
MMexicann marked this pull request as ready for review November 4, 2026 19:57

@FredApps FredApps 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.

Re-reviewed current head. Checking the original argument before truncation correctly rejects negative fractions for GCD and LCM, including range inputs and a zero companion, while preserving positive truncation and signed zero. Regression coverage matches those boundaries. Hosted Ubuntu gate is green; no local tests run. No blocking finding.

@FredApps
FredApps merged commit 10849d5 into storytold:main Nov 14, 2026
6 checks passed
@FredApps

Copy link
Copy Markdown
Contributor

Merged after re-review of 4e4bd75 and verification of its successful hosted CI gate. No local tests were run for this review. Thank you for the fix.

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