Commit 64e174a198

64e174a198c829aabd43c985ba6acd4a2292fc8e

parent: a77399700c

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 19:39 UTC

Match Calc's mod of negative integers and refuse float overflow

math-mod divides as a float without F unless both operands are natural, which also sets the sin and cos index for degrees. Exponents past 4000000 fall back to Emacs.

Layout: unified · split

Sources/OrgCore/Compute/Calc.swift +14 −11
@@ -222,6 +222,9 @@ public enum Calc {
222222 m /= 10
223223 e += 1
224224 }
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") }
225228 guard let i = Int(exactly: m) else { throw Unsupported("calc precision") }
226229 return .float(i, e)
227230 }
@@ -1503,12 +1506,10 @@ public enum Calc {
15031506 /// `math-mod`: the remainder with the divisor's sign.
15041507 func mod(_ a: Value, _ b: Value) throws -> Value {
15051508 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))
15121513 return try subtract(a, try multiply(b, q))
15131514 }
15141515
@@ -1524,6 +1525,7 @@ public enum Calc {
15241525
15251526 func floorOf(_ v: Value) throws -> Value {
15261527 if case .int = v { return v }
1528 if case .frac(let n, let d) = v { return .int(n / d - (n % d < 0 ? 1 : 0)) }
15271529 guard let i = Int(exactly: try double(v).rounded(.down)) else { throw Unsupported("integer size") }
15281530 return .int(i)
15291531 }
@@ -1710,12 +1712,13 @@ public enum Calc {
17101712 let x = try one(args)
17111713 if x == .nan { return x }
17121714 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")
17181720 }
1721 return .int((name == "sin" ? [0, 1, 0, -1] : [1, 0, -1, 0])[quarter])
17191722 }
17201723 return try extra(2) {
17211724 let r = try float(x)
Tests/OrgCoreTests/CalcNumericTests.swift +26 −1
@@ -39,7 +39,8 @@ struct CalcNumericTests {
3939 for precision in [3, 6, 8, 12, 14, 15, 16, 17, 18] {
4040 for radians in [false, true] {
4141 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)") }
4344 for _ in 0..<500 {
4445 let f = ["sin", "cos", "tan", "arcsin", "arccos", "arctan", "arctan2", "sinh", "cosh", "tanh", "sqrt"].randomElement(using: &generator)!
4546 let x = switch f {
@@ -115,6 +116,30 @@ struct CalcNumericTests {
115116 for fractions in [true, false] { _ = try CalcTests.compare(cases, fractions: fractions) }
116117 }
117118
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
118143 /// Vector results, with and without `N`, zero with no decimal places, and fractions.
119144 static let tables = [
120145 "| 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
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, 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;
691691- Lisp functions not listed above.
692692
693693The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=.