Commit 64e174a198
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/Calc.swift +14 −11
| @@ -222,6 +222,9 @@ public enum Calc { | ||
| 222 | 222 | m /= 10 |
| 223 | 223 | e += 1 |
| 224 | 224 | } |
| 225 | // Calc signals `math-overflow` and `math-underflow` here and leaves the formula alone. | |
| 226 | let magnitude = e + digits(m) - 1 | |
| 227 | if magnitude >= 4_000_000 || magnitude <= -4_000_000 { throw Unsupported("number size") } | |
| 225 | 228 | guard let i = Int(exactly: m) else { throw Unsupported("calc precision") } |
| 226 | 229 | return .float(i, e) |
| 227 | 230 | } |
| @@ -1503,12 +1506,10 @@ public enum Calc { | ||
| 1503 | 1506 | /// `math-mod`: the remainder with the divisor's sign. |
| 1504 | 1507 | func mod(_ a: Value, _ b: Value) throws -> Value { |
| 1505 | 1508 | if isZero(a) { return a } |
| 1506 | if case .int(let x) = a, case .int(let y) = b { | |
| 1507 | guard y != 0 else { throw Unsupported("division by zero") } | |
| 1508 | let r = x % y | |
| 1509 | return .int(r != 0 && (r < 0) != (y < 0) ? r + y : r) | |
| 1510 | } | |
| 1511 | let q = try floorOf(try divide(a, b)) | |
| 1509 | if isZero(b) { throw Unsupported("division by zero") } | |
| 1510 | // `math-imod` only for natural numbers; otherwise the quotient is a float without F. | |
| 1511 | if case .int(let x) = a, case .int(let y) = b, x >= 0, y > 0 { return .int(x % y) } | |
| 1512 | let q = try floorOf(try divide(a, b, fractions: modes.preferFractions)) | |
| 1512 | 1513 | return try subtract(a, try multiply(b, q)) |
| 1513 | 1514 | } |
| 1514 | 1515 | |
| @@ -1524,6 +1525,7 @@ public enum Calc { | ||
| 1524 | 1525 | |
| 1525 | 1526 | func floorOf(_ v: Value) throws -> Value { |
| 1526 | 1527 | if case .int = v { return v } |
| 1528 | if case .frac(let n, let d) = v { return .int(n / d - (n % d < 0 ? 1 : 0)) } | |
| 1527 | 1529 | guard let i = Int(exactly: try double(v).rounded(.down)) else { throw Unsupported("integer size") } |
| 1528 | 1530 | return .int(i) |
| 1529 | 1531 | } |
| @@ -1710,12 +1712,13 @@ public enum Calc { | ||
| 1710 | 1712 | let x = try one(args) |
| 1711 | 1713 | if x == .nan { return x } |
| 1712 | 1714 | if case .int(let i) = x, modes.degrees ? i % (name == "tan" ? 180 : 90) == 0 : i == 0 { |
| 1713 | let quarter = ((i / 90) % 4 + 4) % 4 | |
| 1714 | switch name { | |
| 1715 | case "sin": return .int([0, 1, 0, -1][quarter]) | |
| 1716 | case "cos": return .int([1, 0, -1, 0][quarter]) | |
| 1717 | default: return .int(0) | |
| 1715 | if name == "tan" { return .int(0) } | |
| 1716 | // `(aref [0 1 0 -1] (math-mod (/ x 90) 4))`: a float quotient in math-mod can put | |
| 1717 | // the index out of range, and Calc then leaves the call alone. | |
| 1718 | guard case .int(let quarter) = try mod(.int(i / 90), .int(4)), (0...3).contains(quarter) else { | |
| 1719 | throw Unsupported("number size") | |
| 1718 | 1720 | } |
| 1721 | return .int((name == "sin" ? [0, 1, 0, -1] : [1, 0, -1, 0])[quarter]) | |
| 1719 | 1722 | } |
| 1720 | 1723 | return try extra(2) { |
| 1721 | 1724 | let r = try float(x) |
Tests/OrgCoreTests/CalcNumericTests.swift +26 −1
| @@ -39,7 +39,8 @@ struct CalcNumericTests { | ||
| 39 | 39 | for precision in [3, 6, 8, 12, 14, 15, 16, 17, 18] { |
| 40 | 40 | for radians in [false, true] { |
| 41 | 41 | var cases: [(String, String)] = ["sin(nan)", "cos(nan)", "arctan(nan)", "tan(180)", "tan(-360)", "tan(0)", "arcsin(0)", |
| 42 | "arccos(1)", "arccos(0)", "arcsin(-1)", "arctan(1)", "sqrt(2)", "sqrt(1e-9)"].map { ($0, "(float 0)") } | |
| 42 | "arccos(1)", "arccos(0)", "arcsin(-1)", "arctan(1)", "sqrt(2)", "sqrt(1e-9)", "sin(-74250)", "cos(-74250)", | |
| 43 | "sin(-360000000000090)", "cos(-90*4000000000001)", "tan(-360000000000180)"].map { ($0, "(float 0)") } | |
| 43 | 44 | for _ in 0..<500 { |
| 44 | 45 | let f = ["sin", "cos", "tan", "arcsin", "arccos", "arctan", "arctan2", "sinh", "cosh", "tanh", "sqrt"].randomElement(using: &generator)! |
| 45 | 46 | let x = switch f { |
| @@ -115,6 +116,30 @@ struct CalcNumericTests { | ||
| 115 | 116 | for fractions in [true, false] { _ = try CalcTests.compare(cases, fractions: fractions) } |
| 116 | 117 | } |
| 117 | 118 | |
| 119 | /// `math-mod` divides as a float, without F, unless both operands are natural numbers. | |
| 120 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | |
| 121 | func modOfNegativeIntegersMatchesCalc() throws { | |
| 122 | let cases = ["(-74251)%4", "mod(74251,-4)", "mod(-1234567890123457,4)", "(-1234567890123457)%7", "mod(-7,2)", "mod(7,-2)", | |
| 123 | "(-7)%(-2)", "mod(1234567890123457,4)", "(-1234567890123457)\\4", "mod(-7:2,2)", "mod(-1:3,-1:7)"].map { ($0, "(float 0)") } | |
| 124 | for precision in [3, 12] { | |
| 125 | for fractions in [true, false] { | |
| 126 | let skipped = try CalcTests.compare(cases, precision: precision, fractions: fractions) | |
| 127 | #expect(skipped == [], "p\(precision) \(skipped)") | |
| 128 | } | |
| 129 | } | |
| 130 | } | |
| 131 | ||
| 132 | /// Calc's `math-overflow` and `math-underflow` past an exponent of 4,000,000 leave the formula alone. | |
| 133 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | |
| 134 | func floatOverflowIsRefusedAsCalcDoes() throws { | |
| 135 | let refused = ["2.5^123456789012", "pow(-1.5,123456789012)", "pow(1e20,123456789012)", "0.4^123456789012", "2.5^(-123456789012)"] | |
| 136 | let cases = (refused + ["2.5^1000000", "0.4^1000000"]).map { ($0, "(float 0)") } | |
| 137 | for fractions in [true, false] { | |
| 138 | let skipped = try CalcTests.compare(cases, fractions: fractions) | |
| 139 | #expect(skipped == refused) | |
| 140 | } | |
| 141 | } | |
| 142 | ||
| 118 | 143 | /// Vector results, with and without `N`, zero with no decimal places, and fractions. |
| 119 | 144 | static let tables = [ |
| 120 | 145 | "| 1 | 2 | | |\n|---+---+---+---|\n| 2.5 | 0.25 | | |\n#+TBLFM: $3=vmedian($1..$2);F::$4=round($1,1);F\n", |
docs/manual/guide/10-tables.org +2 −2
| @@ -686,8 +686,8 @@ what it was and the Mac recalculates the table in Emacs instead. This happens fo | ||
| 686 | 686 | holding text; |
| 687 | 687 | - Calc functions not listed above, intervals such as =[1..5]=, precisions beyond those |
| 688 | 688 | given under /Mode flags and formats/, parts of the =S= flag, and the =u= flag; |
| 689 | - complex results, vectors of anything but numbers, and numbers too large for 64-bit | |
| 690 | integers; | |
| 689 | - complex results, vectors of anything but numbers, numbers too large for 64-bit | |
| 690 | integers, and decimal numbers past Calc's limit of 10^±4000000; | |
| 691 | 691 | - Lisp functions not listed above. |
| 692 | 692 | |
| 693 | 693 | The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=. |