Commit 56097b2321

56097b23215d95a120e9dbea153d2603d3734bd2

parent: 3fb1e1409c

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 11:31 UTC

Group Calc products from the right

Layout: unified · split

Sources/OrgCore/Compute/Calc.swift +14 −14
@@ -1045,22 +1045,22 @@ public enum Calc {
10451045 return left
10461046 }
10471047
1048 /// `*`, and juxtaposition `2 3` or `2 (x)`, which Calc reads as `*`.
1048 /// `*`, and juxtaposition `2 3` or `2 (x)`, which Calc reads as `*`. With
1049 /// `calc-multiplication-has-precedence` both are right-associative: `a * b c` and
1050 /// `a * b * c` are `a * (b * c)`.
10491051 mutating func product() throws -> Node {
1050 var left = try unary()
1051 while let c = peek() {
1052 if c == "*" {
1053 i += 1
1054 } else if !(c.isASCII && c.isNumber) && !c.isLetter && c != "(" && c != "["
1055 && !(c == "." && i + 1 < chars.count && chars[i + 1].isASCII && chars[i + 1].isNumber) {
1056 break
1057 } else if spaceSeparated, chars[i - 1] == " " || chars[i - 1] == "\t" {
1058 // Calc reads `[1 2]` as two elements and `[1 2 * 3]` as one; not reproduced.
1059 throw Unsupported("formula syntax")
1060 }
1061 left = .binary("*", left, try unary())
1052 let left = try unary()
1053 guard let c = peek() else { return left }
1054 if c == "*" {
1055 i += 1
1056 } else if !(c.isASCII && c.isNumber) && !c.isLetter && c != "(" && c != "["
1057 && !(c == "." && i + 1 < chars.count && chars[i + 1].isASCII && chars[i + 1].isNumber) {
1058 return left
1059 } else if spaceSeparated, chars[i - 1] == " " || chars[i - 1] == "\t" {
1060 // Calc reads `[1 2]` as two elements and `[1 2 * 3]` as one; not reproduced.
1061 throw Unsupported("formula syntax")
10621062 }
1063 return left
1063 return .binary("*", left, try product())
10641064 }
10651065
10661066 /// `math-factor-after`: whether what follows can start an operand, which makes `%`
Tests/OrgCoreTests/CalcTests.swift +18
@@ -361,8 +361,26 @@ struct CalcTests {
361361 "vsum([1/0, 2])", "vsum([5 kg,3])", "vsum([2 (3),1])",
362362 // No comma: Calc separates elements by spaces, except inside parentheses and arguments.
363363 "vsum([1 - 2])", "vsum([x - 1])", "vsum([2x])", "vsum([(1 2)])", "vsum([max(1 2,3)])", "vcount([x])",
364 "(1/x) * 2 x", "(1/x) * 2 * x", "2 x * (1/x)", "(1/(2 x)) * 2 x", "x^-1 * 2 x", "((x)^2)^((0.0)^(0.0))",
365 "(x*y)^(0.0^0.0)", "(y^2)^(2*(0.0^0.0))", "(x^2)^(0.0^0.0 + 1)", "(x^2)^(-(0.0^0.0))", "(x^2)^(2^(0.0^0.0))",
364366 ]
365367
368 /// Products are right-associative, and a formula of integer-valued numbers is known to
369 /// be integer-valued.
370 @Test func symbolicOrder() throws {
371 var modes = Calc.Modes()
372 modes.format = .float(8)
373 for (e, expected) in [("(1/x) * 2 x", "2 x / x"), ("(1/x) * 2 * x", "2 x / x"), ("2 x * (1/x)", "2"),
374 ("(1/(2 x)) * 2 x", "1"), ("x^-1 * 2 x", "2 x / x"),
375 ("((x)^2)^((0.0)^(0.0))", "x^(2 0.^0.)"), ("(x*y)^(0.0^0.0)", "x^0.^0. y^0.^0."),
376 ("(y^2)^(2*(0.0^0.0))", "y^(4 0.^0.)"), ("(x^2)^(0.0^0.0 + 1)", "x^(2 0.^0. + 2)"),
377 ("(x^2)^(-(0.0^0.0))", "x^(-2 0.^0.)"), ("(x^2)^(2^(0.0^0.0))", "(x^2)^2^0.^0.")] {
378 #expect(try Calc.evaluate(e, modes: modes) == expected, "\(e)")
379 }
380 modes.format = .sci(11)
381 #expect(try Calc.evaluate("9.48205255399 * 7.65908717266 9.30092496999", modes: modes) == "6.7546913880e2")
382 }
383
366384 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
367385 func symbolicMatchesCalc() throws {
368386 var cases = Self.symbolic.map { ($0, "(float 8)") }
docs/manual/guide/10-tables.org +1 −1
@@ -325,7 +325,7 @@ side if it is nonzero, else the right side):
325325| ~==~, ~!=~, =<=, =>=, ~<=~, ~>=~ | Comparisons, giving 1 or 0. They can't be chained. |
326326| =+=, =-= | Addition and subtraction. |
327327| =/=, =%=, =\= | Division, modulo (sign of the divisor), integer division rounding down. |
328| =*= | Multiplication. Values side by side also multiply: =2 3=, =2 (x)=. |
328| =*= | Multiplication. Values side by side also multiply: =2 3=, =2 (x)=. It groups from the right: =a * b c= is =a * (b c)=. |
329329| unary =-= | Negation. |
330330| =^= | Power. |
331331| =!=, =!!= after a value | Factorial, double factorial. |