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 {
933933 case call(String, [Node])
934934 case variable(String)
935935
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.
937938 var mayExplain: Bool {
938939 switch self {
939940 case .number, .variable: false
940941 case .call: true
941942 case .vector(let items): items.contains { $0.mayExplain }
942943 case .unary(_, let a): a.mayExplain
944 case .binary("*", let a, let b) where !a.variables.isDisjoint(with: b.variables): true
943945 case .binary(let op, let a, let b): "/%\\^".contains(op) || a.mayExplain || b.mayExplain
944946 }
945947 }
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 }
946958 }
947959
948960 struct Parser {
Tests/OrgCoreTests/CalcTests.swift +2 −2
@@ -427,10 +427,10 @@ struct CalcTests {
427427 #expect(throws: Calc.Failure("Number expected")) { try Calc.evaluate("x+1", numeric: true) }
428428 #expect(throws: Calc.Unsupported.self) { try Calc.evaluate("1/0", numeric: true) }
429429 // 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"] {
431431 #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e, numeric: true) }
432432 }
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"] {
434434 #expect(throws: Calc.Failure("Number expected"), "\(e)") { try Calc.evaluate(e, numeric: true) }
435435 }
436436 #expect(try Calc.evaluate("x/x", numeric: true) == "1")
Tests/OrgCoreTests/TableFormulaTests.swift +3
@@ -100,6 +100,7 @@ struct TableFormulaTests {
100100 "| a | b |\n|---+---|\n| 21.11.2026 | |\n| 192.168.1.1 | |\n| 1.5 | |\n#+TBLFM: $2=$1+1\n",
101101 "| a | b |\n|---+---|\n| 21.11.2026 | |\n#+TBLFM: $2=$1+1;T\n",
102102 "| 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",
103104 ]
104105
105106 /// A range of one field with a space is a vector without commas, which Calc splits at the
@@ -110,6 +111,8 @@ struct TableFormulaTests {
110111 "| a | b | c |\n|---+---+---|\n| 1 2 | | |\n#+TBLFM: $3=vsum($1..$2)\n",
111112 "| a | b |\n|---+---|\n| N A | |\n| | |\n#+TBLFM: @2$2=vmean(@2$1..@3$1)\n",
112113 "| 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",
113116 ] {
114117 let caret = (text as NSString).range(of: "#+TBLFM").location + 3
115118 let failure = Self.recalculate(TableRecalculate(all: true), text, caret: caret).failure ?? ""