Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crates/calc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2896,3 +2896,20 @@ fn computed_criterion_that_reenters_itself_is_not_met() {
t.set("B60", "=DCOUNT($F$2:$G$7,\"age\",N46:N47)");
assert_eq!(t.num("B60"), 0.0);
}

#[test]
fn a_subnormal_number_is_zero() {
// out-of-range.00001 to 00009, power.00021: Excel holds no number below 2.2250738585072014e-308.
let mut t = T::new();
t.set("A1", "1e-310");
t.set("A2", "2.2250738585072e-308");
t.set("B1", "=IF(A1>0,1,2)");
t.set("B2", "=IF(A2>0,1,2)");
t.set("B3", "=SUM(A1:A2)*2");
t.set("B4", "=2^-1074");
assert_eq!(t.get("A1"), Value::Number(0.0));
assert_eq!(t.num("B1"), 2.0);
assert_eq!(t.num("B2"), 2.0);
assert_eq!(t.get("B3"), Value::Number(0.0));
assert_eq!(t.get("B4"), Value::Number(0.0));
}
5 changes: 3 additions & 2 deletions crates/core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ impl From<Array> for Value {
}

impl Value {
/// A number, or `#NUM!` when it is NaN or infinite (Excel never stores those).
/// A number, or `#NUM!` when it is NaN or infinite (Excel never stores those). A subnormal
/// (below 2.2250738585072014e-308 in size) is 0, as Excel holds it (observed on Windows Excel 16.0.20430: `2^-1074` is 0).
pub fn number(v: f64) -> Value {
if v.is_finite() { Value::Number(if v == 0.0 { 0.0 } else { v }) } else { Value::Error(CellError::Num) }
if v.is_finite() { Value::Number(if v.abs() < f64::MIN_POSITIVE { 0.0 } else { v }) } else { Value::Error(CellError::Num) }
}
pub fn text(s: impl Into<Arc<str>>) -> Value {
Value::Text(s.into())
Expand Down
7 changes: 6 additions & 1 deletion crates/functions/src/stat/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ fn median_finite_midpoint_does_not_overflow() {
fn median_midpoint_keeps_small_values_and_argument_semantics() {
// Halving each operand unconditionally can lose the smallest numbers. Use exact
// equality here: the usual aggregate-test tolerance would accept zero.
for value in [f64::MIN_POSITIVE, -f64::MIN_POSITIVE, f64::from_bits(1), -f64::from_bits(1)] {
for value in [f64::MIN_POSITIVE, -f64::MIN_POSITIVE] {
assert_eq!(ev("MEDIAN", vec![n(value), n(value)]), nv(value));
assert_eq!(aggregate_values(12, &[nv(value), nv(value)]), nv(value));
}
// A subnormal is 0, as Excel holds it.
for value in [f64::from_bits(1), -f64::from_bits(1)] {
assert_eq!(ev("MEDIAN", vec![n(value), n(value)]), nv(0.0));
assert_eq!(aggregate_values(12, &[nv(value), nv(value)]), nv(0.0));
}
close(ev("MEDIAN", vec![c(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0])]), 3.5);
close(ev("MEDIAN", vec![c(&[1e308, -1e308, 3.0])]), 3.0);
close(ev("MEDIAN", vec![n(1e308)]), 1e308);
Expand Down
11 changes: 9 additions & 2 deletions crates/model/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ impl CellStore {
}
Arc::make_mut(band).rows.get_mut(&c.row)?.get_mut(&c.col)
}
/// Stores a cell; a default (empty, unstyled) cell is removed instead.
pub fn set(&mut self, c: CellRef, cell: Cell) {
/// Stores a cell; a default (empty, unstyled) cell is removed instead. A subnormal number is
/// stored as 0: Excel reads one from a file, or takes one typed, as 0 (observed on Windows Excel 16.0.20430).
pub fn set(&mut self, c: CellRef, mut cell: Cell) {
if let gridcraft_core::Value::Number(n) = cell.value
&& n != 0.0
&& n.abs() < f64::MIN_POSITIVE
{
cell.value = gridcraft_core::Value::Number(0.0);
}
if cell.is_blank() {
self.remove(c);
return;
Expand Down
Loading