Commit 8ea52e8b2c
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/TablePlot.swift +1 −3
| @@ -236,9 +236,7 @@ public enum TablePlot { | |||
| 236 | 236 | ||
| 237 | /// `string-to-number`: the leading number, else 0. | 237 | /// `string-to-number`: the leading number, else 0. |
| 238 | static func stringToNumber(_ s: String) -> Double { | 238 | static func stringToNumber(_ s: String) -> Double { |
| 239 | let trimmed = s.drop { $0 == " " || $0 == "\t" } | 239 | lispNumber(s).value |
| 240 | guard let m = trimmed.firstMatch(of: /^[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?/) else { return 0 } | ||
| 241 | return Double(m.0) ?? 0 | ||
| 242 | } | 240 | } |
| 243 | 241 | ||
| 244 | /// `org-plot/gnuplot-script`, with `org-plot/gnuplot-term-extra` and the preamble empty. | 242 | /// `org-plot/gnuplot-script`, with `org-plot/gnuplot-term-extra` and the preamble empty. |
Sources/OrgCore/Compute/TablePlotRadar.swift +8 −3
| @@ -26,8 +26,11 @@ extension TablePlot { | |||
| 26 | /// A number as `string-to-number` reads it, and whether it's an integer. | 26 | /// A number as `string-to-number` reads it, and whether it's an integer. |
| 27 | static func lispNumber(_ s: String) -> (value: Double, integer: Bool) { | 27 | static func lispNumber(_ s: String) -> (value: Double, integer: Bool) { |
| 28 | let trimmed = s.drop { $0 == " " || $0 == "\t" } | 28 | let trimmed = s.drop { $0 == " " || $0 == "\t" } |
| 29 | guard let m = trimmed.firstMatch(of: /^[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?/) else { return (0, true) } | 29 | guard let m = trimmed.firstMatch(of: /^([-+]?)([0-9]+\.?[0-9]*|\.[0-9]+)([eE]([-+]?[0-9]+|\+INF|\+NaN))?/) else { return (0, true) } |
| 30 | let text = String(m.0) | 30 | let text = String(m.0), sign: Double = m.1 == "-" ? -1 : 1 |
| 31 | // The reader's `1.0e+INF` and `1.0e+NaN`. | ||
| 32 | if text.hasSuffix("INF") { return (sign * .infinity, false) } | ||
| 33 | if text.hasSuffix("NaN") { return (sign * .nan, false) } | ||
| 31 | return (Double(text) ?? 0, text.firstMatch(of: /\.[0-9]|[eE]/) == nil) | 34 | return (Double(text) ?? 0, text.firstMatch(of: /\.[0-9]|[eE]/) == nil) |
| 32 | } | 35 | } |
| 33 | 36 | ||
| @@ -46,7 +49,9 @@ extension TablePlot { | |||
| 46 | /// `org--plot/values-stats`. | 49 | /// `org--plot/values-stats`. |
| 47 | static func stats(_ nums: [(value: Double, integer: Bool)], hardMin: Double? = nil, hardMax: Double? = nil) | 50 | static func stats(_ nums: [(value: Double, integer: Bool)], hardMin: Double? = nil, hardMax: Double? = nil) |
| 48 | -> (factor: Double, niceMin: (value: Double, integer: Bool), niceMax: (value: Double, integer: Bool)) { | 51 | -> (factor: Double, niceMin: (value: Double, integer: Bool), niceMax: (value: Double, integer: Bool)) { |
| 49 | let low = hardMin ?? nums.map(\.value).min()!, high = hardMax ?? nums.map(\.value).max()! | 52 | // Emacs's `min` and `max` return a NaN argument. |
| 53 | let values = nums.map(\.value), nan = values.contains { $0.isNaN } | ||
| 54 | let low = hardMin ?? (nan ? .nan : values.min()!), high = hardMax ?? (nan ? .nan : values.max()!) | ||
| 50 | let range = high - low | 55 | let range = high - low |
| 51 | guard range != 0 else { return (1, nums[0], nums[0]) } | 56 | guard range != 0 else { return (1, nums[0], nums[0]) } |
| 52 | let order = (1 - log10(range)).rounded(.up) | 57 | let order = (1 - log10(range)).rounded(.up) |
Tests/OrgCoreTests/TablePlotTests.swift +6
| @@ -213,6 +213,12 @@ struct TablePlotTests { | |||
| 213 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymin:100\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } | 213 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymin:100\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } |
| 214 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymax:0\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } | 214 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymax:0\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } |
| 215 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:-1\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } | 215 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:-1\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } |
| 216 | // string-to-number reads the reader's infinities and NaNs; Emacs then signals overflow-error. | ||
| 217 | #expect(TablePlot.lispNumber("-2.0e+INF").value == -.infinity && TablePlot.lispNumber("1e+NaN").value.isNaN) | ||
| 218 | #expect(TablePlot.lispNumber("1.0e-INF").value == 1 && TablePlot.lispNumber("1.0e+inf").value == 1 && TablePlot.stringToNumber(".5e+INFx") == .infinity) | ||
| 219 | for field in ["1.0e+INF", "1.0e+NaN"] { | ||
| 220 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:4\n| a | 1 | \(field) |\n| b | 2 | 5 |\n", at: 0) } | ||
| 221 | } | ||
| 216 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:99999999999999999999\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } | 222 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:99999999999999999999\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } |
| 217 | // gnuplot draws as many rings as it's given. | 223 | // gnuplot draws as many rings as it's given. |
| 218 | _ = try TablePlot.plot("#+PLOT: type:radar ticks:2000\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) | 224 | _ = try TablePlot.plot("#+PLOT: type:radar ticks:2000\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) |