Commit 56097b2321
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/Calc.swift +14 −14
| @@ -1045,22 +1045,22 @@ public enum Calc { | |||
| 1045 | return left | 1045 | return left |
| 1046 | } | 1046 | } |
| 1047 | 1047 | ||
| 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)`. | ||
| 1049 | mutating func product() throws -> Node { | 1051 | mutating func product() throws -> Node { |
| 1050 | var left = try unary() | 1052 | let left = try unary() |
| 1051 | while let c = peek() { | 1053 | guard let c = peek() else { return left } |
| 1052 | if c == "*" { | 1054 | if c == "*" { |
| 1053 | i += 1 | 1055 | i += 1 |
| 1054 | } else if !(c.isASCII && c.isNumber) && !c.isLetter && c != "(" && c != "[" | 1056 | } else if !(c.isASCII && c.isNumber) && !c.isLetter && c != "(" && c != "[" |
| 1055 | && !(c == "." && i + 1 < chars.count && chars[i + 1].isASCII && chars[i + 1].isNumber) { | 1057 | && !(c == "." && i + 1 < chars.count && chars[i + 1].isASCII && chars[i + 1].isNumber) { |
| 1056 | break | 1058 | return left |
| 1057 | } else if spaceSeparated, chars[i - 1] == " " || chars[i - 1] == "\t" { | 1059 | } 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. | 1060 | // Calc reads `[1 2]` as two elements and `[1 2 * 3]` as one; not reproduced. |
| 1059 | throw Unsupported("formula syntax") | 1061 | throw Unsupported("formula syntax") |
| 1060 | } | ||
| 1061 | left = .binary("*", left, try unary()) | ||
| 1062 | } | 1062 | } |
| 1063 | return left | 1063 | return .binary("*", left, try product()) |
| 1064 | } | 1064 | } |
| 1065 | 1065 | ||
| 1066 | /// `math-factor-after`: whether what follows can start an operand, which makes `%` | 1066 | /// `math-factor-after`: whether what follows can start an operand, which makes `%` |
Tests/OrgCoreTests/CalcTests.swift +18
| @@ -361,8 +361,26 @@ struct CalcTests { | |||
| 361 | "vsum([1/0, 2])", "vsum([5 kg,3])", "vsum([2 (3),1])", | 361 | "vsum([1/0, 2])", "vsum([5 kg,3])", "vsum([2 (3),1])", |
| 362 | // No comma: Calc separates elements by spaces, except inside parentheses and arguments. | 362 | // No comma: Calc separates elements by spaces, except inside parentheses and arguments. |
| 363 | "vsum([1 - 2])", "vsum([x - 1])", "vsum([2x])", "vsum([(1 2)])", "vsum([max(1 2,3)])", "vcount([x])", | 363 | "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))", | ||
| 364 | ] | 366 | ] |
| 365 | 367 | ||
| 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 | |||
| 366 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | 384 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) |
| 367 | func symbolicMatchesCalc() throws { | 385 | func symbolicMatchesCalc() throws { |
| 368 | var cases = Self.symbolic.map { ($0, "(float 8)") } | 386 | 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): | |||
| 325 | | ~==~, ~!=~, =<=, =>=, ~<=~, ~>=~ | Comparisons, giving 1 or 0. They can't be chained. | | 325 | | ~==~, ~!=~, =<=, =>=, ~<=~, ~>=~ | Comparisons, giving 1 or 0. They can't be chained. | |
| 326 | | =+=, =-= | Addition and subtraction. | | 326 | | =+=, =-= | Addition and subtraction. | |
| 327 | | =/=, =%=, =\= | Division, modulo (sign of the divisor), integer division rounding down. | | 327 | | =/=, =%=, =\= | 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)=. | |
| 329 | | unary =-= | Negation. | | 329 | | unary =-= | Negation. | |
| 330 | | =^= | Power. | | 330 | | =^= | Power. | |
| 331 | | =!=, =!!= after a value | Factorial, double factorial. | | 331 | | =!=, =!!= after a value | Factorial, double factorial. | |