Commit 428d644fb1
428d644fb164b52f5378c54b9a2bc9e763be158e
parent: 2e5b193b0e
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-21 10:29 UTC
Refuse num errors from products of a variable with itself
Layout: unified · split
Sources/OrgCore/Compute/Calc.swift
+13 −1
| @@ -933,16 +933,28 @@ public enum Calc { |
| 933 | 933 | case call(String, [Node]) |
| 934 | 934 | case variable(String) |
| 935 | 935 | |
| 936 | | /// Whether the formula has a quotient, a power or a function call. |
| 936 | /// Whether the formula has a quotient, a power, a function call, or a product |
| 937 | /// whose factors share a variable, which Calc simplifies to a power. |
| 937 | 938 | var mayExplain: Bool { |
| 938 | 939 | switch self { |
| 939 | 940 | case .number, .variable: false |
| 940 | 941 | case .call: true |
| 941 | 942 | case .vector(let items): items.contains { $0.mayExplain } |
| 942 | 943 | case .unary(_, let a): a.mayExplain |
| 944 | case .binary("*", let a, let b) where !a.variables.isDisjoint(with: b.variables): true |
| 943 | 945 | case .binary(let op, let a, let b): "/%\\^".contains(op) || a.mayExplain || b.mayExplain |
| 944 | 946 | } |
| 945 | 947 | } |
| 948 | |
| 949 | var variables: Set<String> { |
| 950 | switch self { |
| 951 | case .number: [] |
| 952 | case .variable(let name): [name] |
| 953 | case .call(_, let items), .vector(let items): items.reduce(into: []) { $0.formUnion($1.variables) } |
| 954 | case .unary(_, let a): a.variables |
| 955 | case .binary(_, let a, let b): a.variables.union(b.variables) |
| 956 | } |
| 957 | } |
| 946 | 958 | } |
| 947 | 959 | |
| 948 | 960 | struct Parser { |
Tests/OrgCoreTests/CalcTests.swift
+2 −2
| @@ -427,10 +427,10 @@ struct CalcTests { |
| 427 | 427 | #expect(throws: Calc.Failure("Number expected")) { try Calc.evaluate("x+1", numeric: true) } |
| 428 | 428 | #expect(throws: Calc.Unsupported.self) { try Calc.evaluate("1/0", numeric: true) } |
| 429 | 429 | // Calc explains what a quotient, power or function rejected, even when it cancels. |
| 430 | | for e in ["x^2/x", "x*x/x", "inf+(1/0)", "x/2", "abs(x)", "x%2"] { |
| 430 | for e in ["x^2/x", "x*x/x", "inf+(1/0)", "x/2", "abs(x)", "x%2", "x*x", "x x", "x*y*x"] { |
| 431 | 431 | #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e, numeric: true) } |
| 432 | 432 | } |
| 433 | | for e in ["-x", "x*2", "2 x", "x+2-2", "nan"] { |
| 433 | for e in ["-x", "x*2", "2 x", "x*y", "x+x", "x+2-2", "nan"] { |
| 434 | 434 | #expect(throws: Calc.Failure("Number expected"), "\(e)") { try Calc.evaluate(e, numeric: true) } |
| 435 | 435 | } |
| 436 | 436 | #expect(try Calc.evaluate("x/x", numeric: true) == "1") |
Tests/OrgCoreTests/TableFormulaTests.swift
+3
| @@ -100,6 +100,7 @@ struct TableFormulaTests { |
| 100 | 100 | "| a | b |\n|---+---|\n| 21.11.2026 | |\n| 192.168.1.1 | |\n| 1.5 | |\n#+TBLFM: $2=$1+1\n", |
| 101 | 101 | "| a | b |\n|---+---|\n| 21.11.2026 | |\n#+TBLFM: $2=$1+1;T\n", |
| 102 | 102 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x;T\n", |
| 103 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x*y;T\n", |
| 103 | 104 | ] |
| 104 | 105 | |
| 105 | 106 | /// A range of one field with a space is a vector without commas, which Calc splits at the |
| @@ -110,6 +111,8 @@ struct TableFormulaTests { |
| 110 | 111 | "| a | b | c |\n|---+---+---|\n| 1 2 | | |\n#+TBLFM: $3=vsum($1..$2)\n", |
| 111 | 112 | "| a | b |\n|---+---|\n| N A | |\n| | |\n#+TBLFM: @2$2=vmean(@2$1..@3$1)\n", |
| 112 | 113 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x^2/x;T\n", |
| 114 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x*x;T\n", |
| 115 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x x;T\n", |
| 113 | 116 | ] { |
| 114 | 117 | let caret = (text as NSString).range(of: "#+TBLFM").location + 3 |
| 115 | 118 | let failure = Self.recalculate(TableRecalculate(all: true), text, caret: caret).failure ?? "" |