(l: &CellValue, r: &CellValue, op: BinaryOp)
| 61 | } |
| 62 | |
| 63 | fn apply(l: &CellValue, r: &CellValue, op: BinaryOp) -> CellValue { |
| 64 | let (lf, rf) = match (to_f64(l), to_f64(r)) { |
| 65 | (Some(a), Some(b)) => (a, b), |
| 66 | _ => return passthrough(l, r), |
| 67 | }; |
| 68 | let v = match op { |
| 69 | BinaryOp::Add => lf + rf, |
| 70 | BinaryOp::Sub => lf - rf, |
| 71 | BinaryOp::Mul => lf * rf, |
| 72 | BinaryOp::Div => { |
| 73 | if rf == 0.0 { |
| 74 | return CellValue::Null; |
| 75 | } |
| 76 | lf / rf |
| 77 | } |
| 78 | }; |
| 79 | // Preserve Int64 when both operands were integral and result is |
| 80 | // exact — keeps integer attrs from drifting to floats unnecessarily. |
| 81 | if let (CellValue::Int64(_), CellValue::Int64(_)) = (l, r) |
| 82 | && v.fract() == 0.0 |
| 83 | && v.is_finite() |
| 84 | { |
| 85 | return CellValue::Int64(v as i64); |
| 86 | } |
| 87 | CellValue::Float64(v) |
| 88 | } |
| 89 | |
| 90 | fn passthrough(l: &CellValue, r: &CellValue) -> CellValue { |
| 91 | if !l.is_null() { |
no test coverage detected