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 | case call(String, [Node]) |
933 | case call(String, [Node]) |
| 934 | case variable(String) |
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 | var mayExplain: Bool { |
938 | var mayExplain: Bool { |
| 938 | switch self { |
939 | switch self { |
| 939 | case .number, .variable: false |
940 | case .number, .variable: false |
| 940 | case .call: true |
941 | case .call: true |
| 941 | case .vector(let items): items.contains { $0.mayExplain } |
942 | case .vector(let items): items.contains { $0.mayExplain } |
| 942 | case .unary(_, let a): a.mayExplain |
943 | case .unary(_, let a): a.mayExplain |
| |
944 | case .binary("*", let a, let b) where !a.variables.isDisjoint(with: b.variables): true |
| 943 | case .binary(let op, let a, let b): "/%\\^".contains(op) || a.mayExplain || b.mayExplain |
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 | struct Parser { |
960 | struct Parser { |
Tests/OrgCoreTests/CalcTests.swift
+2 −2
| @@ -427,10 +427,10 @@ struct CalcTests { |
| 427 | #expect(throws: Calc.Failure("Number expected")) { try Calc.evaluate("x+1", numeric: true) } |
427 | #expect(throws: Calc.Failure("Number expected")) { try Calc.evaluate("x+1", numeric: true) } |
| 428 | #expect(throws: Calc.Unsupported.self) { try Calc.evaluate("1/0", numeric: true) } |
428 | #expect(throws: Calc.Unsupported.self) { try Calc.evaluate("1/0", numeric: true) } |
| 429 | // Calc explains what a quotient, power or function rejected, even when it cancels. |
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 | #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e, numeric: true) } |
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 | #expect(throws: Calc.Failure("Number expected"), "\(e)") { try Calc.evaluate(e, numeric: true) } |
434 | #expect(throws: Calc.Failure("Number expected"), "\(e)") { try Calc.evaluate(e, numeric: true) } |
| 435 | } |
435 | } |
| 436 | #expect(try Calc.evaluate("x/x", numeric: true) == "1") |
436 | #expect(try Calc.evaluate("x/x", numeric: true) == "1") |
Tests/OrgCoreTests/TableFormulaTests.swift
+3
| @@ -100,6 +100,7 @@ struct TableFormulaTests { |
| 100 | "| a | b |\n|---+---|\n| 21.11.2026 | |\n| 192.168.1.1 | |\n| 1.5 | |\n#+TBLFM: $2=$1+1\n", |
100 | "| a | b |\n|---+---|\n| 21.11.2026 | |\n| 192.168.1.1 | |\n| 1.5 | |\n#+TBLFM: $2=$1+1\n", |
| 101 | "| a | b |\n|---+---|\n| 21.11.2026 | |\n#+TBLFM: $2=$1+1;T\n", |
101 | "| a | b |\n|---+---|\n| 21.11.2026 | |\n#+TBLFM: $2=$1+1;T\n", |
| 102 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x;T\n", |
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 | /// A range of one field with a space is a vector without commas, which Calc splits at the |
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 | "| a | b | c |\n|---+---+---|\n| 1 2 | | |\n#+TBLFM: $3=vsum($1..$2)\n", |
111 | "| a | b | c |\n|---+---+---|\n| 1 2 | | |\n#+TBLFM: $3=vsum($1..$2)\n", |
| 111 | "| a | b |\n|---+---|\n| N A | |\n| | |\n#+TBLFM: @2$2=vmean(@2$1..@3$1)\n", |
112 | "| a | b |\n|---+---|\n| N A | |\n| | |\n#+TBLFM: @2$2=vmean(@2$1..@3$1)\n", |
| 112 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x^2/x;T\n", |
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 | let caret = (text as NSString).range(of: "#+TBLFM").location + 3 |
117 | let caret = (text as NSString).range(of: "#+TBLFM").location + 3 |
| 115 | let failure = Self.recalculate(TableRecalculate(all: true), text, caret: caret).failure ?? "" |
118 | let failure = Self.recalculate(TableRecalculate(all: true), text, caret: caret).failure ?? "" |