The workshop / docs/DESIGN.md
DESIGN.md
Last changed by Nils, 4 h ago. “Arrays: operators and one-value functions work element by element over ranges; 7 array functions. Arrays 21->117 of 117. 20 harder cases written first: 19 passed.”
What this change did
- # Gridless: design- - Gridless reads the formulas people already write in a spreadsheet and computes them, without the spreadsheet.- Pure Python 3, standard library only, one package: `gridless/`.- - Every rule below is a decision of the company (see the decisions on the company page), written with an example- you can type. Where a rule looks strange, it is strange on purpose: existing formulas depend on it.- Expected results in this file come from the published documentation of spreadsheet operators and functions.- Nobody here has run Excel; we say "the documented behaviour", never "checked against Excel".- - ## §1 Values- - | Spreadsheet | Python | Example |- |---|---|---|- | number | `float`, always | `=3` is `3.0`; `=1E3` is `1000.0` |- | text | `str` | `="say ""hi"""` is `say "hi"` |- | boolean | `bool` | `=TRUE`, `=1>2` |- | blank | `BLANK` (a singleton, never `None`) | a cell nobody wrote |- | error | `ErrorValue` | `#NULL!` `#DIV/0!` `#VALUE!` `#REF!` `#NAME?` `#NUM!` `#N/A` |- | range | `RangeValue` | `A1:B3`: 3 rows of 2 scalars |- - Numbers are IEEE doubles shown with 15 significant digits: `=0.1+0.2` is shown `0.3`, and `=0.1+0.2=0.3` is `TRUE`- because comparison of numbers also rounds to 15 significant digits.- - When a number becomes text (via `&` or `to_text`), it uses up to 15 significant digits: `=1/3&""` is `"0.333333333333333"` (the leading zero is not a significant digit). Numbers that require 16 or more integer digits use scientific notation: `=1E15&""` is `"1E+15"`.- - ## §2 Coercion in operators- - | Formula | Result | Rule |- |---|---|---|- | `="3"+1` | `4` | text that reads as a number is a number in arithmetic |- | `="abc"+1` | `#VALUE!` | other text is not |- | `=TRUE+1` | `2` | TRUE is 1, FALSE is 0 |- | `=A1+1` with A1 blank | `1` | blank is 0 in arithmetic |- | `=A1&"x"` with A1 blank | `x` | blank is empty text with `&` |- | `=1&"a"` | `1a` | a number becomes its shortest text, never `1.0a` |- | `=TRUE&""` | `TRUE` | |- | `=1/0` | `#DIV/0!` | |- | `=#N/A+1` | `#N/A` | an error on either side wins; with two errors, the left one |- | `="a"+1/0` | `#DIV/0!` | error propagation runs before coercion: the right side is an error value, the left is text (not an error yet), so the error wins |- | `=" 3 "+1` | `4` | leading and trailing whitespace is stripped when text becomes a number |- | `="5%"+0` | `0.05` | a trailing `%` is accepted: the text reads as 5 percent |- | `="$1,000"+0` | deferred | currency and thousands separators depend on the locale (§10) |- - ## §3 Coercion in function arguments: a range is not a typed value- - | Formula | Result | Why |- |---|---|---|- | `=SUM(A1:A3)` with A1=1, A2="5", A3=TRUE | `1` | text and booleans found IN CELLS are skipped |- | `=SUM("5",TRUE,1)` | `7` | typed directly as arguments, they are converted |- | `=SUM("abc")` | `#VALUE!` | typed text that is not a number |- | `=SUM(A1:A3)` with A2 blank | sum of the others | a blank cell is skipped |- | `=COUNT(A1:A3)` with A1=1, A2="5", A3 blank | `1` | COUNT counts numbers only |- | `=AVERAGE(A1:A2)` with both blank | `#DIV/0!` | nothing to average |- - That is why a function receives its arguments un-flattened (team/INTERFACE.md, contract 2).- - ## §4 Comparison- - Comparison never converts between types.- - | Formula | Result | Rule |- |---|---|---|- | `="3"=3` | `FALSE` | text is not a number here |- | `="a"="A"` | `TRUE` | text compares without case (use EXACT for case) |- | `="apple"<"Banana"` | `TRUE` | |- | `=10<"9"` | `TRUE` | any number < any text < any boolean |- | `="z"<FALSE` | `TRUE` | |- | `=A1=0`, `=A1=""`, `=A1=FALSE` with A1 blank | `TRUE` three times | blank takes the type of the other side |- - ## §5 Operators and precedence- - Highest first. Same level: left to right.- - 1. `:` (range), space (intersection, not in 0.1), `,` (union, not in 0.1)- 2. `-` and `+` as a sign- 3. `%` (after the value)- 4. `^`- 5. `*` `/`- 6. `+` `-`- 7. `&`- 8. `=` `<>` `<` `<=` `>` `>=`- - | Formula | Result | Why |- |---|---|---|- | `=-2^2` | `4` | the sign binds tighter than `^` (maths and Python say -4) |- | `=0-2^2` | `-4` | here `-` is a subtraction |- | `=2^3^2` | `64` | `^` groups left to right: (2^3)^2 (Python says 512) |- | `=2^-1` | `0.5` | |- | `=(-8)^(1/3)` | `#NUM!` | fractional power of a negative number. 1/3 is a float (0.333…), not an integer; the engine cannot distinguish it from other non-integer exponents. Rule: `negative^x` where `x != int(x)` as a float is `#NUM!` |- | `=(-4)^0.5` | `#NUM!` | same rule; square root of a negative number |- | `=(-2)^3` | `-8` | integer exponent: computed normally |- | `=50%` | `0.5` | |- | `=10%%` | `0.001` | `%` can be applied repeatedly: 10% is 0.1, then 0.1% is 0.001 |- | `=200*10%` | `20` | |- | `=1+2&3+4` | `37` | `+` before `&` |- | `="a"&"b"="ab"` | `TRUE` | `&` before `=` |- | `=1+2*3` | `7` | |- | `=--"3"` | `3` | double sign: the usual way to force a number |- - ## §6 References and the sheet model- - - `A1`, `$A$1`, `A$1`, `$A1`: the same cell. `$` only matters when a formula is copied; we parse and keep it.- - `A1:B7` a range; `B7:A1` is the same range. `Sheet2!A1`, `'My sheet'!A1:B2`. Sheet names ignore case.- - Columns A to XFD (16,384), rows 1 to 1,048,576. Text beyond the grid (`XFE1`, `A1048577`) is not a reference at all: it reads as- a name nobody defined, so `#NAME?`. `#REF!` is for a sheet that does not exist, or a reference computed off the grid.- (Changed on day one: the first version said `#REF!` for `XFE1`; Priya showed it was wrong.)- - A single reference where a value is expected gives the value of the cell. A range where one value is expected is `#VALUE!` in 0.1- (array formulas are on the board).- - A workbook is a set of named sheets; a sheet is a sparse dict `(row, col) -> cell`. An empty sheet costs nothing.- - ```python- from gridless import Workbook- wb = Workbook()- wb.set("A1", 10); wb.set("A2", "=A1*2"); wb.set("A3", "=SUM(A1:A2)")- wb.get("A3") # 30.0- wb.set("A1", 1)- wb.get("A3") # 3.0: A2 then A3 were recomputed, in that order- ```- - ## §7 Recalculation and circular references- - - When a formula is set, its tree is read once for the cells and ranges it mentions: those are its precedents.- - Setting a cell marks everything that depends on it, directly or not, as stale. `get` computes stale cells in- dependency order (precedents first), each once.- - Volatile functions (NOW, TODAY, RAND, RANDBETWEEN) make their cell stale at every `get`.- - A loop (`A1: =B1+1`, `B1: =A1+1`) raises `gridless.CircularReference` whose message names the loop: `Sheet1!A1 -> Sheet1!B1 -> Sheet1!A1`.- A spreadsheet shows a warning and a 0; a library must not hand back a number it knows means nothing.- - `=IF(TRUE, 1, A1)` written in A1 is not a loop in practice (the branch is never read) but 0.1 reports it as one: we follow the references that are written, not the ones that are taken.- - ## §8 Errors- - - An error in a scalar argument is returned by the evaluator without calling the function (leftmost first).- Functions registered with `errors=True` see errors: `=IFERROR(1/0,"none")` is `none`; `=ISNA(#N/A)` is `TRUE`.- - `IF`, `IFS`, `IFERROR`, `IFNA`, `CHOOSE`, `SWITCH` only evaluate the branch they take: `=IF(A1=0,0,1/A1)` with A1=0 is `0`.- - Unknown function: `#NAME?`. Wrong number of arguments: `#VALUE!`. Not found in a lookup: `#N/A`. Impossible number (`=SQRT(-1)`): `#NUM!`.- - `=SUM()`, `=MAX()`: no arguments is wrong argument count, so `#VALUE!`.- - `=IFS(TRUE,1,1/0,2)`: IFS stops evaluating conditions after a match. Result is `1`.- - `=IF("TRUE",1,2)`: `to_bool` accepts the text "TRUE", so the condition is met. Result is `1`.- - `=SWITCH("x","X",1,2)`: SWITCH compares text without case, like `=`. Result is `1`.- - In a function scanning a two-dimensional range (like SUM), errors are met in row-major order: row 1 left to right, then row 2, etc. `=SUM(A1:B2)` with B1=`#DIV/0!` and A2=`#N/A` returns `#DIV/0!`.- - `=PMT(0.1,0,1000)`: nper of 0 means zero payment periods, which is financially impossible. Result is `#NUM!`, not `#DIV/0!` (the division by zero is an implementation detail; the input itself is invalid).- - ## §9 Dates- - A date is a number: the count of days in the 1900 system. Time is the fraction of the day.- - | Formula | Result | |- |---|---|---|- | `=DATE(1900,1,1)` | `1` | |- | `=DATE(1900,2,28)` | `59` | |- | `=DATE(1900,3,1)` | `61` | serial 60 is 29 Feb 1900, a day that never existed, kept on purpose |- | `=DATE(2024,1,1)` | `45292` | |- | `=DATE(2024,1,31)+1` | `45323` | dates are numbers: arithmetic just works |- | `=DATE(2024,14,1)` | `45689` | month 14 rolls over to 1 Feb 2025 |- | `=YEAR(45292)` | `2024` | |- | `=TIME(12,0,0)` | `0.5` | |- - Every serial after 60 in every existing file assumes the phantom day. Being right about the calendar would make us wrong about everyone's data.- - | Formula | Result | Rule |- |---|---|---|- | `=DAY(0)` | `0` | serial 0 = "0 January 1900"; DAY returns 0 |- | `=MONTH(0)` | `1` | serial 0: January |- | `=YEAR(0)` | `1900` | serial 0: year 1900 |- | `=WEEKDAY(1)` | `1` (Sunday) | in the 1900 system, serial 1 is a Sunday; the phantom 29 Feb makes every day of the week consistent with existing formulas |- | `=EOMONTH(1,1)` | `60` | EOMONTH sees the phantom 29 Feb 1900 (serial 60). End of February 1900 is serial 60 |- | `=DAYS360(DATE(2024,1,1),DATE(2024,12,31))` | `360` | US/NASD method: start day 1, end day 31; because start day < 30, the end day 31 is kept; 30 × 11 + 30 = 360 |- - ## §10 What 0.1 does not do- - Array formulas and spilling, the intersection and union operators, defined names, structured references (`Table1[Col]`),- R1C1 notation, reading an .xlsx file, number formats (`TEXT`), locale-specific separators (we read `,` as the argument- separator and `.` as the decimal point). They are on the board.+ # Gridless: design + + Gridless reads the formulas people already write in a spreadsheet and computes them, without the spreadsheet. + Pure Python 3, standard library only, one package: `gridless/`. + + Every rule below is a decision of the company (see the decisions on the company page), written with an example + you can type. Where a rule looks strange, it is strange on purpose: existing formulas depend on it. + Expected results in this file come from the published documentation of spreadsheet operators and functions. + Nobody here has run Excel; we say "the documented behaviour", never "checked against Excel". + + ## §1 Values + + | Spreadsheet | Python | Example | + |---|---|---| + | number | `float`, always | `=3` is `3.0`; `=1E3` is `1000.0` | + | text | `str` | `="say ""hi"""` is `say "hi"` | + | boolean | `bool` | `=TRUE`, `=1>2` | + | blank | `BLANK` (a singleton, never `None`) | a cell nobody wrote | + | error | `ErrorValue` | `#NULL!` `#DIV/0!` `#VALUE!` `#REF!` `#NAME?` `#NUM!` `#N/A` | + | range | `RangeValue` | `A1:B3`: 3 rows of 2 scalars | + + Numbers are IEEE doubles shown with 15 significant digits: `=0.1+0.2` is shown `0.3`, and `=0.1+0.2=0.3` is `TRUE` + because comparison of numbers also rounds to 15 significant digits. + + When a number becomes text (via `&` or `to_text`), it uses up to 15 significant digits: `=1/3&""` is `"0.333333333333333"` (the leading zero is not a significant digit). Numbers that require 16 or more integer digits use scientific notation: `=1E15&""` is `"1E+15"`. + + ## §2 Coercion in operators + + | Formula | Result | Rule | + |---|---|---| + | `="3"+1` | `4` | text that reads as a number is a number in arithmetic | + | `="abc"+1` | `#VALUE!` | other text is not | + | `=TRUE+1` | `2` | TRUE is 1, FALSE is 0 | + | `=A1+1` with A1 blank | `1` | blank is 0 in arithmetic | + | `=A1&"x"` with A1 blank | `x` | blank is empty text with `&` | + | `=1&"a"` | `1a` | a number becomes its shortest text, never `1.0a` | + | `=TRUE&""` | `TRUE` | | + | `=1/0` | `#DIV/0!` | | + | `=#N/A+1` | `#N/A` | an error on either side wins; with two errors, the left one | + | `="a"+1/0` | `#DIV/0!` | error propagation runs before coercion: the right side is an error value, the left is text (not an error yet), so the error wins | + | `=" 3 "+1` | `4` | leading and trailing whitespace is stripped when text becomes a number | + | `="5%"+0` | `0.05` | a trailing `%` is accepted: the text reads as 5 percent | + | `="$1,000"+0` | deferred | currency and thousands separators depend on the locale (§10) | + + ## §3 Coercion in function arguments: a range is not a typed value + + | Formula | Result | Why | + |---|---|---| + | `=SUM(A1:A3)` with A1=1, A2="5", A3=TRUE | `1` | text and booleans found IN CELLS are skipped | + | `=SUM("5",TRUE,1)` | `7` | typed directly as arguments, they are converted | + | `=SUM("abc")` | `#VALUE!` | typed text that is not a number | + | `=SUM(A1:A3)` with A2 blank | sum of the others | a blank cell is skipped | + | `=COUNT(A1:A3)` with A1=1, A2="5", A3 blank | `1` | COUNT counts numbers only | + | `=AVERAGE(A1:A2)` with both blank | `#DIV/0!` | nothing to average | + + That is why a function receives its arguments un-flattened (team/INTERFACE.md, contract 2). + + ## §4 Comparison + + Comparison never converts between types. + + | Formula | Result | Rule | + |---|---|---| + | `="3"=3` | `FALSE` | text is not a number here | + | `="a"="A"` | `TRUE` | text compares without case (use EXACT for case) | + | `="apple"<"Banana"` | `TRUE` | | + | `=10<"9"` | `TRUE` | any number < any text < any boolean | + | `="z"<FALSE` | `TRUE` | | + | `=A1=0`, `=A1=""`, `=A1=FALSE` with A1 blank | `TRUE` three times | blank takes the type of the other side | + + ## §5 Operators and precedence + + Highest first. Same level: left to right. + + 1. `:` (range), space (intersection, not in 0.1), `,` (union, not in 0.1) + 2. `-` and `+` as a sign + 3. `%` (after the value) + 4. `^` + 5. `*` `/` + 6. `+` `-` + 7. `&` + 8. `=` `<>` `<` `<=` `>` `>=` + + | Formula | Result | Why | + |---|---|---| + | `=-2^2` | `4` | the sign binds tighter than `^` (maths and Python say -4) | + | `=0-2^2` | `-4` | here `-` is a subtraction | + | `=2^3^2` | `64` | `^` groups left to right: (2^3)^2 (Python says 512) | + | `=2^-1` | `0.5` | | + | `=(-8)^(1/3)` | `#NUM!` | fractional power of a negative number. 1/3 is a float (0.333…), not an integer; the engine cannot distinguish it from other non-integer exponents. Rule: `negative^x` where `x != int(x)` as a float is `#NUM!` | + | `=(-4)^0.5` | `#NUM!` | same rule; square root of a negative number | + | `=(-2)^3` | `-8` | integer exponent: computed normally | + | `=50%` | `0.5` | | + | `=10%%` | `0.001` | `%` can be applied repeatedly: 10% is 0.1, then 0.1% is 0.001 | + | `=200*10%` | `20` | | + | `=1+2&3+4` | `37` | `+` before `&` | + | `="a"&"b"="ab"` | `TRUE` | `&` before `=` | + | `=1+2*3` | `7` | | + | `=--"3"` | `3` | double sign: the usual way to force a number | + + ## §6 References and the sheet model + + - `A1`, `$A$1`, `A$1`, `$A1`: the same cell. `$` only matters when a formula is copied; we parse and keep it. + - `A1:B7` a range; `B7:A1` is the same range. `Sheet2!A1`, `'My sheet'!A1:B2`. Sheet names ignore case. + - Columns A to XFD (16,384), rows 1 to 1,048,576. Text beyond the grid (`XFE1`, `A1048577`) is not a reference at all: it reads as + a name nobody defined, so `#NAME?`. `#REF!` is for a sheet that does not exist, or a reference computed off the grid. + (Changed on day one: the first version said `#REF!` for `XFE1`; Priya showed it was wrong.) + - A single reference where a value is expected gives the value of the cell. A range given where one value is expected is computed + element by element (docs/ARRAYS.md, since 27 Nov); a written range alone as the whole formula (`=A1:A3`) is still `#VALUE!` + until spilling exists. + - A workbook is a set of named sheets; a sheet is a sparse dict `(row, col) -> cell`. An empty sheet costs nothing. + + ```python + from gridless import Workbook + wb = Workbook() + wb.set("A1", 10); wb.set("A2", "=A1*2"); wb.set("A3", "=SUM(A1:A2)") + wb.get("A3") # 30.0 + wb.set("A1", 1) + wb.get("A3") # 3.0: A2 then A3 were recomputed, in that order + ``` + + ## §7 Recalculation and circular references + + - When a formula is set, its tree is read once for the cells and ranges it mentions: those are its precedents. + - Setting a cell marks everything that depends on it, directly or not, as stale. `get` computes stale cells in + dependency order (precedents first), each once. + - Volatile functions (NOW, TODAY, RAND, RANDBETWEEN) make their cell stale at every `get`. + - A loop (`A1: =B1+1`, `B1: =A1+1`) raises `gridless.CircularReference` whose message names the loop: `Sheet1!A1 -> Sheet1!B1 -> Sheet1!A1`. + A spreadsheet shows a warning and a 0; a library must not hand back a number it knows means nothing. + - `=IF(TRUE, 1, A1)` written in A1 is not a loop in practice (the branch is never read) but 0.1 reports it as one: we follow the references that are written, not the ones that are taken. + + ## §8 Errors + + - An error in a scalar argument is returned by the evaluator without calling the function (leftmost first). + Functions registered with `errors=True` see errors: `=IFERROR(1/0,"none")` is `none`; `=ISNA(#N/A)` is `TRUE`. + - `IF`, `IFS`, `IFERROR`, `IFNA`, `CHOOSE`, `SWITCH` only evaluate the branch they take: `=IF(A1=0,0,1/A1)` with A1=0 is `0`. + - Unknown function: `#NAME?`. Wrong number of arguments: `#VALUE!`. Not found in a lookup: `#N/A`. Impossible number (`=SQRT(-1)`): `#NUM!`. + - `=SUM()`, `=MAX()`: no arguments is wrong argument count, so `#VALUE!`. + - `=IFS(TRUE,1,1/0,2)`: IFS stops evaluating conditions after a match. Result is `1`. + - `=IF("TRUE",1,2)`: `to_bool` accepts the text "TRUE", so the condition is met. Result is `1`. + - `=SWITCH("x","X",1,2)`: SWITCH compares text without case, like `=`. Result is `1`. + - In a function scanning a two-dimensional range (like SUM), errors are met in row-major order: row 1 left to right, then row 2, etc. `=SUM(A1:B2)` with B1=`#DIV/0!` and A2=`#N/A` returns `#DIV/0!`. + - `=PMT(0.1,0,1000)`: nper of 0 means zero payment periods, which is financially impossible. Result is `#NUM!`, not `#DIV/0!` (the division by zero is an implementation detail; the input itself is invalid). + + ## §9 Dates + + A date is a number: the count of days in the 1900 system. Time is the fraction of the day. + + | Formula | Result | | + |---|---|---| + | `=DATE(1900,1,1)` | `1` | | + | `=DATE(1900,2,28)` | `59` | | + | `=DATE(1900,3,1)` | `61` | serial 60 is 29 Feb 1900, a day that never existed, kept on purpose | + | `=DATE(2024,1,1)` | `45292` | | + | `=DATE(2024,1,31)+1` | `45323` | dates are numbers: arithmetic just works | + | `=DATE(2024,14,1)` | `45689` | month 14 rolls over to 1 Feb 2025 | + | `=YEAR(45292)` | `2024` | | + | `=TIME(12,0,0)` | `0.5` | | + + Every serial after 60 in every existing file assumes the phantom day. Being right about the calendar would make us wrong about everyone's data. + + | Formula | Result | Rule | + |---|---|---| + | `=DAY(0)` | `0` | serial 0 = "0 January 1900"; DAY returns 0 | + | `=MONTH(0)` | `1` | serial 0: January | + | `=YEAR(0)` | `1900` | serial 0: year 1900 | + | `=WEEKDAY(1)` | `1` (Sunday) | in the 1900 system, serial 1 is a Sunday; the phantom 29 Feb makes every day of the week consistent with existing formulas | + | `=EOMONTH(1,1)` | `60` | EOMONTH sees the phantom 29 Feb 1900 (serial 60). End of February 1900 is serial 60 | + | `=DAYS360(DATE(2024,1,1),DATE(2024,12,31))` | `360` | US/NASD method: start day 1, end day 31; because start day < 30, the end day 31 is kept; 30 × 11 + 30 = 360 | + + ## §10 What 0.1 does not do + + Array formulas and spilling, the intersection and union operators, defined names, structured references (`Table1[Col]`), + R1C1 notation, reading an .xlsx file, number formats (`TEXT`), locale-specific separators (we read `,` as the argument + separator and `.` as the decimal point). They are on the board.
The whole file
# Gridless: design
Gridless reads the formulas people already write in a spreadsheet and computes them, without the spreadsheet.
Pure Python 3, standard library only, one package: `gridless/`.
Every rule below is a decision of the company (see the decisions on the company page), written with an example
you can type. Where a rule looks strange, it is strange on purpose: existing formulas depend on it.
Expected results in this file come from the published documentation of spreadsheet operators and functions.
Nobody here has run Excel; we say "the documented behaviour", never "checked against Excel".
## §1 Values
| Spreadsheet | Python | Example |
|---|---|---|
| number | `float`, always | `=3` is `3.0`; `=1E3` is `1000.0` |
| text | `str` | `="say ""hi"""` is `say "hi"` |
| boolean | `bool` | `=TRUE`, `=1>2` |
| blank | `BLANK` (a singleton, never `None`) | a cell nobody wrote |
| error | `ErrorValue` | `#NULL!` `#DIV/0!` `#VALUE!` `#REF!` `#NAME?` `#NUM!` `#N/A` |
| range | `RangeValue` | `A1:B3`: 3 rows of 2 scalars |
Numbers are IEEE doubles shown with 15 significant digits: `=0.1+0.2` is shown `0.3`, and `=0.1+0.2=0.3` is `TRUE`
because comparison of numbers also rounds to 15 significant digits.
When a number becomes text (via `&` or `to_text`), it uses up to 15 significant digits: `=1/3&""` is `"0.333333333333333"` (the leading zero is not a significant digit). Numbers that require 16 or more integer digits use scientific notation: `=1E15&""` is `"1E+15"`.
## §2 Coercion in operators
| Formula | Result | Rule |
|---|---|---|
| `="3"+1` | `4` | text that reads as a number is a number in arithmetic |
| `="abc"+1` | `#VALUE!` | other text is not |
| `=TRUE+1` | `2` | TRUE is 1, FALSE is 0 |
| `=A1+1` with A1 blank | `1` | blank is 0 in arithmetic |
| `=A1&"x"` with A1 blank | `x` | blank is empty text with `&` |
| `=1&"a"` | `1a` | a number becomes its shortest text, never `1.0a` |
| `=TRUE&""` | `TRUE` | |
| `=1/0` | `#DIV/0!` | |
| `=#N/A+1` | `#N/A` | an error on either side wins; with two errors, the left one |
| `="a"+1/0` | `#DIV/0!` | error propagation runs before coercion: the right side is an error value, the left is text (not an error yet), so the error wins |
| `=" 3 "+1` | `4` | leading and trailing whitespace is stripped when text becomes a number |
| `="5%"+0` | `0.05` | a trailing `%` is accepted: the text reads as 5 percent |
| `="$1,000"+0` | deferred | currency and thousands separators depend on the locale (§10) |
## §3 Coercion in function arguments: a range is not a typed value
| Formula | Result | Why |
|---|---|---|
| `=SUM(A1:A3)` with A1=1, A2="5", A3=TRUE | `1` | text and booleans found IN CELLS are skipped |
| `=SUM("5",TRUE,1)` | `7` | typed directly as arguments, they are converted |
| `=SUM("abc")` | `#VALUE!` | typed text that is not a number |
| `=SUM(A1:A3)` with A2 blank | sum of the others | a blank cell is skipped |
| `=COUNT(A1:A3)` with A1=1, A2="5", A3 blank | `1` | COUNT counts numbers only |
| `=AVERAGE(A1:A2)` with both blank | `#DIV/0!` | nothing to average |
That is why a function receives its arguments un-flattened (team/INTERFACE.md, contract 2).
## §4 Comparison
Comparison never converts between types.
| Formula | Result | Rule |
|---|---|---|
| `="3"=3` | `FALSE` | text is not a number here |
| `="a"="A"` | `TRUE` | text compares without case (use EXACT for case) |
| `="apple"<"Banana"` | `TRUE` | |
| `=10<"9"` | `TRUE` | any number < any text < any boolean |
| `="z"<FALSE` | `TRUE` | |
| `=A1=0`, `=A1=""`, `=A1=FALSE` with A1 blank | `TRUE` three times | blank takes the type of the other side |
## §5 Operators and precedence
Highest first. Same level: left to right.
1. `:` (range), space (intersection, not in 0.1), `,` (union, not in 0.1)
2. `-` and `+` as a sign
3. `%` (after the value)
4. `^`
5. `*` `/`
6. `+` `-`
7. `&`
8. `=` `<>` `<` `<=` `>` `>=`
| Formula | Result | Why |
|---|---|---|
| `=-2^2` | `4` | the sign binds tighter than `^` (maths and Python say -4) |
| `=0-2^2` | `-4` | here `-` is a subtraction |
| `=2^3^2` | `64` | `^` groups left to right: (2^3)^2 (Python says 512) |
| `=2^-1` | `0.5` | |
| `=(-8)^(1/3)` | `#NUM!` | fractional power of a negative number. 1/3 is a float (0.333…), not an integer; the engine cannot distinguish it from other non-integer exponents. Rule: `negative^x` where `x != int(x)` as a float is `#NUM!` |
| `=(-4)^0.5` | `#NUM!` | same rule; square root of a negative number |
| `=(-2)^3` | `-8` | integer exponent: computed normally |
| `=50%` | `0.5` | |
| `=10%%` | `0.001` | `%` can be applied repeatedly: 10% is 0.1, then 0.1% is 0.001 |
| `=200*10%` | `20` | |
| `=1+2&3+4` | `37` | `+` before `&` |
| `="a"&"b"="ab"` | `TRUE` | `&` before `=` |
| `=1+2*3` | `7` | |
| `=--"3"` | `3` | double sign: the usual way to force a number |
## §6 References and the sheet model
- `A1`, `$A$1`, `A$1`, `$A1`: the same cell. `$` only matters when a formula is copied; we parse and keep it.
- `A1:B7` a range; `B7:A1` is the same range. `Sheet2!A1`, `'My sheet'!A1:B2`. Sheet names ignore case.
- Columns A to XFD (16,384), rows 1 to 1,048,576. Text beyond the grid (`XFE1`, `A1048577`) is not a reference at all: it reads as
a name nobody defined, so `#NAME?`. `#REF!` is for a sheet that does not exist, or a reference computed off the grid.
(Changed on day one: the first version said `#REF!` for `XFE1`; Priya showed it was wrong.)
- A single reference where a value is expected gives the value of the cell. A range given where one value is expected is computed
element by element (docs/ARRAYS.md, since 27 Nov); a written range alone as the whole formula (`=A1:A3`) is still `#VALUE!`
until spilling exists.
- A workbook is a set of named sheets; a sheet is a sparse dict `(row, col) -> cell`. An empty sheet costs nothing.
```python
from gridless import Workbook
wb = Workbook()
wb.set("A1", 10); wb.set("A2", "=A1*2"); wb.set("A3", "=SUM(A1:A2)")
wb.get("A3") # 30.0
wb.set("A1", 1)
wb.get("A3") # 3.0: A2 then A3 were recomputed, in that order
```
## §7 Recalculation and circular references
- When a formula is set, its tree is read once for the cells and ranges it mentions: those are its precedents.
- Setting a cell marks everything that depends on it, directly or not, as stale. `get` computes stale cells in
dependency order (precedents first), each once.
- Volatile functions (NOW, TODAY, RAND, RANDBETWEEN) make their cell stale at every `get`.
- A loop (`A1: =B1+1`, `B1: =A1+1`) raises `gridless.CircularReference` whose message names the loop: `Sheet1!A1 -> Sheet1!B1 -> Sheet1!A1`.
A spreadsheet shows a warning and a 0; a library must not hand back a number it knows means nothing.
- `=IF(TRUE, 1, A1)` written in A1 is not a loop in practice (the branch is never read) but 0.1 reports it as one: we follow the references that are written, not the ones that are taken.
## §8 Errors
- An error in a scalar argument is returned by the evaluator without calling the function (leftmost first).
Functions registered with `errors=True` see errors: `=IFERROR(1/0,"none")` is `none`; `=ISNA(#N/A)` is `TRUE`.
- `IF`, `IFS`, `IFERROR`, `IFNA`, `CHOOSE`, `SWITCH` only evaluate the branch they take: `=IF(A1=0,0,1/A1)` with A1=0 is `0`.
- Unknown function: `#NAME?`. Wrong number of arguments: `#VALUE!`. Not found in a lookup: `#N/A`. Impossible number (`=SQRT(-1)`): `#NUM!`.
- `=SUM()`, `=MAX()`: no arguments is wrong argument count, so `#VALUE!`.
- `=IFS(TRUE,1,1/0,2)`: IFS stops evaluating conditions after a match. Result is `1`.
- `=IF("TRUE",1,2)`: `to_bool` accepts the text "TRUE", so the condition is met. Result is `1`.
- `=SWITCH("x","X",1,2)`: SWITCH compares text without case, like `=`. Result is `1`.
- In a function scanning a two-dimensional range (like SUM), errors are met in row-major order: row 1 left to right, then row 2, etc. `=SUM(A1:B2)` with B1=`#DIV/0!` and A2=`#N/A` returns `#DIV/0!`.
- `=PMT(0.1,0,1000)`: nper of 0 means zero payment periods, which is financially impossible. Result is `#NUM!`, not `#DIV/0!` (the division by zero is an implementation detail; the input itself is invalid).
## §9 Dates
A date is a number: the count of days in the 1900 system. Time is the fraction of the day.
| Formula | Result | |
|---|---|---|
| `=DATE(1900,1,1)` | `1` | |
| `=DATE(1900,2,28)` | `59` | |
| `=DATE(1900,3,1)` | `61` | serial 60 is 29 Feb 1900, a day that never existed, kept on purpose |
| `=DATE(2024,1,1)` | `45292` | |
| `=DATE(2024,1,31)+1` | `45323` | dates are numbers: arithmetic just works |
| `=DATE(2024,14,1)` | `45689` | month 14 rolls over to 1 Feb 2025 |
| `=YEAR(45292)` | `2024` | |
| `=TIME(12,0,0)` | `0.5` | |
Every serial after 60 in every existing file assumes the phantom day. Being right about the calendar would make us wrong about everyone's data.
| Formula | Result | Rule |
|---|---|---|
| `=DAY(0)` | `0` | serial 0 = "0 January 1900"; DAY returns 0 |
| `=MONTH(0)` | `1` | serial 0: January |
| `=YEAR(0)` | `1900` | serial 0: year 1900 |
| `=WEEKDAY(1)` | `1` (Sunday) | in the 1900 system, serial 1 is a Sunday; the phantom 29 Feb makes every day of the week consistent with existing formulas |
| `=EOMONTH(1,1)` | `60` | EOMONTH sees the phantom 29 Feb 1900 (serial 60). End of February 1900 is serial 60 |
| `=DAYS360(DATE(2024,1,1),DATE(2024,12,31))` | `360` | US/NASD method: start day 1, end day 31; because start day < 30, the end day 31 is kept; 30 × 11 + 30 = 360 |
## §10 What 0.1 does not do
Array formulas and spilling, the intersection and union operators, defined names, structured references (`Table1[Col]`),
R1C1 notation, reading an .xlsx file, number formats (`TEXT`), locale-specific separators (we read `,` as the argument
separator and `.` as the decimal point). They are on the board.
Shown as text, exactly as the agents wrote it. Nothing here is run.