Skip to content

Accuracy: a subnormal number is stored and computed as 0, as Excel holds it - #3832

Open
MacJedi42 wants to merge 1 commit into
storytold:mainfrom
MacJedi42:fix/excel-subnormal-zero
Open

MacJedi42 wants to merge 1 commit into
storytold:mainfrom
MacJedi42:fix/excel-subnormal-zero

Conversation

@MacJedi42

@MacJedi42 MacJedi42 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Split out of #1462 (part 2 of 5).

Problem

Excel holds any number smaller than 2.2250738585072014E-308 in size as 0, whether typed, read from a file or computed. GridCraft kept the IEEE 754 subnormal.

Why the subnormal case? Isn't Excel objectively worse than an IEEE 754 subnormal (gradual underflow)?

Well... yes, it technically is numerically, 2^-1074 is exactly representable as a double, so 5E-324 is the true answer and 0 is wrong however:

  • The difference spreads into other results, and that's what users notice. No one reads 5E-324 in a cell, but formulas that depend on it come out differently:
Formula Excel GridCraft on main
=2^-1074*2^1074 #NUM! #NUM!
=2^-1074 0 5E-324
=1/2^-1074 #DIV/0! #NUM!
=IF(2^-1074=0,"zero","not zero") zero not zero
=2^-1022 2.2250738585072014E-308 2.2250738585072014E-308
=2^-1023 0 1.1125369292536007E-308
=2^-1023*2^1023 0 1

A workbook built in Excel that guards against zero would branch differently in GridCraft. Its results would then differ from Excel's in ordinary-looking cells.

  • The extra accuracy is mostly illusory. A subnormal carries fewer significant bits the smaller it gets. 2^-1074 has exactly one bit, so it says "roughly 5E-324" and little more. Real spreadsheets don't get there except through underflow, and Excel's display wouldn't show it anyway.
  • GridCraft aims to compute the same results as Excel. Where it aims to do better is speed, openness and agent control, not different numbers.

Measured black-box in Windows Excel (Microsoft 365, 16.0.20430.20146, 64-bit) with synthetic workbooks; Mac Excel 16.113 agrees. Raw output: Spreadsheet Accuracy Suite, request 5.

Fix

  • Value::number (crates/core/src/value.rs) returns 0 for a subnormal result.
  • CellStore::set (crates/model/src/store.rs) stores a subnormal number as 0. This also changes what a file load stores: a subnormal read from a workbook becomes 0, as Excel reads it.

f64::MIN_POSITIVE itself and everything larger are unchanged.

Testing

  • crates/calc/src/tests.rs: a_subnormal_number_is_zero (fails without the fix).
  • crates/functions/src/stat/tests.rs: median_midpoint_keeps_small_values_and_argument_semantics now expects 0 for a subnormal MEDIAN and still expects f64::MIN_POSITIVE to survive.
  • Ran locally on this branch: cargo fmt --all -- --check and cargo test -p gridcraft-calc -p gridcraft-functions (pass). I did not run the full cargo xtask ci on this branch.

Summary by CodeRabbit

  • Bug Fixes
    • Very small numeric values below the minimum normal floating-point value are now treated as zero when evaluated or stored, matching spreadsheet behavior.
    • Comparisons and sums reflect this normalization. Values at the minimum normal magnitude remain unchanged, and invalid numeric inputs continue to be handled as before.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Finite numeric values below f64::MIN_POSITIVE are converted to zero in Value::number and CellStore::set. Tests cover calculations and statistical functions, and verify that minimum normal values remain unchanged.

Changes: Numeric Value Normalization

Layer / File(s) Summary
Subnormal normalization and validation
crates/core/src/value.rs, crates/model/src/store.rs, crates/calc/src/tests.rs, crates/functions/src/stat/tests.rs
Value::number and CellStore::set convert subnormal numbers to zero. Tests check parsed and computed values, comparisons, summation, MEDIAN, and aggregate function 12. They also verify that ±f64::MIN_POSITIVE remain unchanged.

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

Change: Bug fix

Possibly related issues

  • Issue 2947 — Reports that calculations can retain results below the smallest normal number when Excel returns zero. This change addresses subnormal handling in numeric values and cell storage, but does not change formula literal validation.

Suggested reviewers: dylanmtaylor, vangel76, echelon

Merge Risk: Minimal · up to a6294

Subnormal numbers are now treated as zero when computed or stored, matching the Excel behavior the author reports. No concrete merge-blocking problem was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main behavior change: subnormal numbers are stored and computed as zero to match Excel. The raw summary confirms this change in Value::number, CellStore::set, and the added tests. …
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.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main behavior change: subnormal numbers are stored and computed as zero to match Excel. The raw summary confirms this change in Value::number, CellStore::set, and the added tests. The title uses a single sentence and names the affected concept. It is a bit long at 75 characters, but it is specific and related to the changeset.

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 7e0d96d and a629458.

Files selected for processing: 4

  • crates/calc/src/tests.rs
  • crates/core/src/value.rs
  • crates/functions/src/stat/tests.rs
  • crates/model/src/store.rs

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

freemans32 added a commit to freemans32/gridcraft that referenced this pull request Nov 13, 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.

1 participant