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 {
69 if numeric { 69 if numeric {
70 switch value { 70 switch value {
71 case .int, .float, .frac: break 71 case .int, .float, .frac: break
72 case .vector where isConstant(value): break
72 default: 73 default:
73 // Calc explains the first argument a quotient, power or function 74 // Calc explains the first argument a quotient, power or function
74 // rejected along the way, which isn't tracked here. 75 // rejected along the way, which isn't tracked here.
@@ -846,12 +847,23 @@ public enum Calc {
846 847
847 // MARK: - Display 848 // MARK: - Display
848 849
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
849 /// `math-format-number`. 859 /// `math-format-number`.
850 static func format(_ value: Value, _ floatFormat: FloatFormat) throws -> String { 860 static func format(_ value: Value, _ floatFormat: FloatFormat) throws -> String {
851 switch value { 861 switch value {
852 case .int(let i): return String(i) 862 case .int(let i): return String(i)
853 case .frac(let n, let d): return "\(n):\(d)" 863 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: ", ") + "]"
855 case .symbolic(let t): return try Symbolic.format(t, floatFormat) 867 case .symbolic(let t): return try Symbolic.format(t, floatFormat)
856 case .nan: return "nan" 868 case .nan: return "nan"
857 case .date(let d): return "<" + (try formatDate(d)) + ">" 869 case .date(let d): return "<" + (try formatDate(d)) + ">"
@@ -868,7 +880,8 @@ public enum Calc {
868 case .sci(let n): (figs, kind) = (n, "sci") 880 case .sci(let n): (figs, kind) = (n, "sci")
869 case .eng(let n): (figs, kind) = (n, "eng") 881 case .eng(let n): (figs, kind) = (n, "eng")
870 } 882 }
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 {
872 if figs < 0 { figs = -figs } 885 if figs < 0 { figs = -figs }
873 mant = try scaleRounding(mant, exp + figs) 886 mant = try scaleRounding(mant, exp + figs)
874 var str = String(mant) 887 var str = String(mant)
Tests/OrgCoreTests/CalcNumericTests.swift +29
@@ -71,4 +71,33 @@ struct CalcNumericTests {
71 } 71 }
72 } 72 }
73 } 73 }
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 }
74} 103}
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
686 holding text; 686 holding text;
687- Calc functions not listed above, intervals such as =[1..5]=, precisions beyond those 687- Calc functions not listed above, intervals such as =[1..5]=, precisions beyond those
688 given under /Mode flags and formats/, parts of the =S= flag, and the =u= flag; 688 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;
690- Lisp functions not listed above. 691- Lisp functions not listed above.
691 692
692The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=. 693The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=.