Commit c94842da65

c94842da6507556451996a9a65f19655d16252d0

parent: 00080260b4

Verified · cmc

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

Print vectors of numbers, with N too, and zero in fix 0 as Calc does

Layout: unified · split

Sources/OrgCore/Compute/Calc.swift +15 −2
@@ -69,6 +69,7 @@ public enum Calc {
6969 if numeric {
7070 switch value {
7171 case .int, .float, .frac: break
72 case .vector where isConstant(value): break
7273 default:
7374 // Calc explains the first argument a quotient, power or function
7475 // rejected along the way, which isn't tracked here.
@@ -846,12 +847,23 @@ public enum Calc {
846847
847848 // MARK: - Display
848849
850 /// `math-constp` of the values here: a real number, or a vector of them.
851 static func isConstant(_ v: Value) -> Bool {
852 switch v {
853 case .int, .float, .frac: true
854 case .vector(let items): items.allSatisfy(isConstant)
855 default: false
856 }
857 }
858
849859 /// `math-format-number`.
850860 static func format(_ value: Value, _ floatFormat: FloatFormat) throws -> String {
851861 switch value {
852862 case .int(let i): return String(i)
853863 case .frac(let n, let d): return "\(n):\(d)"
854 case .vector: throw Unsupported("vector result")
864 case .vector(let items):
865 guard isConstant(value) else { throw Unsupported("vector result") }
866 return "[" + (try items.map { try format($0, floatFormat) }).joined(separator: ", ") + "]"
855867 case .symbolic(let t): return try Symbolic.format(t, floatFormat)
856868 case .nan: return "nan"
857869 case .date(let d): return "<" + (try formatDate(d)) + ">"
@@ -868,7 +880,8 @@ public enum Calc {
868880 case .sci(let n): (figs, kind) = (n, "sci")
869881 case .eng(let n): (figs, kind) = (n, "eng")
870882 }
871 if kind == "fix", figs < 0 || exp + digits(mant) > -figs {
883 // `math-numdigs` of zero is 0, so zero with no decimal places goes to scientific.
884 if kind == "fix", figs < 0 || exp + (mant == 0 ? 0 : digits(mant)) > -figs {
872885 if figs < 0 { figs = -figs }
873886 mant = try scaleRounding(mant, exp + figs)
874887 var str = String(mant)
Tests/OrgCoreTests/CalcNumericTests.swift +29
@@ -71,4 +71,33 @@ struct CalcNumericTests {
7171 }
7272 }
7373 }
74
75 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
76 func zeroInEveryFormatMatchesCalc() throws {
77 var cases: [(String, String)] = []
78 for kind in ["float", "fix", "sci", "eng"] {
79 for figs in [0, 1, 2, 3, 8, -2] {
80 for e in ["0.", "-0.", "0.0", "1-1.0", "0./0", "0.e5", "0.001", "0.5", "-0.4", "4e-5", "123.456"] {
81 cases.append((e, "(\(kind) \(figs))"))
82 }
83 }
84 }
85 let skipped = try CalcTests.compare(cases)
86 #expect(skipped == [], "\(skipped)")
87 }
88
89 /// Vector results, with and without `N`, and zero with no decimal places.
90 static let tables = [
91 "| 1 | 2 | |\n|---+---+---|\n| 3 | x | |\n| 1.5 | 0. | |\n#+TBLFM: $3=$1..$2;N\n",
92 "| 1 | 2 | |\n|---+---+---|\n| 1.5 | 0. | |\n| 1:3 | -2 | |\n#+TBLFM: $3=$1..$2\n",
93 "| 1.5 | 0 | |\n|---+---+---|\n| 2 | 4 | |\n#+TBLFM: $3=$1..$2;Nf0\n",
94 "| 1 | 2 | |\n|---+---+---|\n| 3 | 4 | |\n#+TBLFM: @1$3=@1$1..@2$2;N::@2$3=vflat(@1$1..@2$2);N\n",
95 "| 1 | 2 | |\n#+TBLFM: $3=[$1, [$2]];N\n",
96 "| 0 | | | |\n|---+---+---+---|\n| 0.5 | | | |\n#+TBLFM: $2=$1*1.0;f0::$3=($1-$1)*1.;s2::$4=($1-$1)*1./0;f0\n",
97 ]
98
99 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
100 func vectorsAndZeroInTablesMatchEmacsNatively() throws {
101 try TableFormulaTests().compare(Self.tables, fallbacksAllowed: false, headerFallbacksAllowed: false)
102 }
74103}
docs/manual/guide/10-tables.org +2 −1
@@ -686,7 +686,8 @@ what it was and the Mac recalculates the table in Emacs instead. This happens fo
686686 holding text;
687687- Calc functions not listed above, intervals such as =[1..5]=, precisions beyond those
688688 given under /Mode flags and formats/, parts of the =S= flag, and the =u= flag;
689- complex results, vector results, and numbers too large for 64-bit integers;
689- complex results, vectors of anything but numbers, and numbers too large for 64-bit
690 integers;
690691- Lisp functions not listed above.
691692
692693The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=.