Match Calc's trig, vectors, F flag fractions and zero printing !424

merged merged by cmc on 2026-11-21 21:13 UTC · krz/orgstar:gaps-calc into main

4 files changed, +391 −75

Layout: unified · split

Sources/OrgCore/Compute/Calc.swift +206 −67
@@ -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.
@@ -116,6 +117,8 @@ public enum Calc {
116 // Constants to 36 digits, rounded to the precision, as `math-defcache` gives them. 117 // Constants to 36 digits, rounded to the precision, as `math-defcache` gives them.
117 static func pi() throws -> Value { try makeFloat(314159265358979323846264338327950288, -35) } 118 static func pi() throws -> Value { try makeFloat(314159265358979323846264338327950288, -35) }
118 static func piOver2() throws -> Value { try makeFloat(157079632679489661923132169163975144, -35) } 119 static func piOver2() throws -> Value { try makeFloat(157079632679489661923132169163975144, -35) }
120 static func piOver4() throws -> Value { try makeFloat(785398163397448309615660845819875721, -36) }
121 static func twoPi() throws -> Value { try makeFloat(628318530717958647692528676655900577, -35) }
119 static func piOver180() throws -> Value { try makeFloat(174532925199432957692369076848861271, -37) } 122 static func piOver180() throws -> Value { try makeFloat(174532925199432957692369076848861271, -37) }
120 123
121 /// `math-float`. 124 /// `math-float`.
@@ -219,6 +222,9 @@ public enum Calc {
219 m /= 10 222 m /= 10
220 e += 1 223 e += 1
221 } 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") }
222 guard let i = Int(exactly: m) else { throw Unsupported("calc precision") } 228 guard let i = Int(exactly: m) else { throw Unsupported("calc precision") }
223 return .float(i, e) 229 return .float(i, e)
224 } 230 }
@@ -277,19 +283,6 @@ public enum Calc {
277 } 283 }
278 } 284 }
279 285
280 static func fromDouble(_ d: Double) throws -> Value {
281 // Calc works these out at two digits more; past 16 it uses its own series.
282 guard precision <= 14 else { throw Unsupported("calc precision") }
283 guard d.isFinite else { throw Unsupported("infinite result") }
284 if d == 0 { return .float(0, 0) }
285 // Seventeen significant digits are exact for a Double; rounding to 12 follows.
286 let text = String(format: "%.16e", d)
287 let pieces = text.split(separator: "e")
288 let digitsText = pieces[0].replacingOccurrences(of: ".", with: "")
289 guard let mantissa = Int128(digitsText), let exponent = Int(pieces[1]) else { throw Unsupported("number") }
290 return try makeFloat(mantissa, exponent - 16)
291 }
292
293 static func isSymbolic(_ vs: Value...) -> Bool { 286 static func isSymbolic(_ vs: Value...) -> Bool {
294 vs.contains { if case .symbolic = $0 { true } else { false } } 287 vs.contains { if case .symbolic = $0 { true } else { false } }
295 } 288 }
@@ -324,10 +317,22 @@ public enum Calc {
324 let ediff = e1 - e2 317 let ediff = e1 - e2
325 if ediff >= 0 { 318 if ediff >= 0 {
326 if ediff >= 2 * precision { return try makeFloat(m1, e1) } 319 if ediff >= 2 * precision { return try makeFloat(m1, e1) }
327 return try makeFloat(try scaleRounding(m1, ediff) + m2, e2) 320 let (m, shift) = try align(m1, ediff, m2)
321 return try makeFloat(m, e2 + shift)
328 } 322 }
329 if -ediff >= 2 * precision { return try makeFloat(m2, e2) } 323 if -ediff >= 2 * precision { return try makeFloat(m2, e2) }
330 return try makeFloat(m1 + (try scaleRounding(m2, -ediff)), e1) 324 let (m, shift) = try align(m2, -ediff, m1)
325 return try makeFloat(m, e1 + shift)
326 }
327
328 /// `big` times 10^ediff plus `small`, and the power of ten that scales it. Past 37 digits
329 /// the digits of `small` below the precision become one sticky digit, which rounds the same.
330 static func align(_ big: Int128, _ ediff: Int, _ small: Int128) throws -> (Int128, Int) {
331 if digits(big) + ediff <= 37 { return (try scaleRounding(big, ediff) + small, 0) }
332 let s = max(precision + 2 - digits(big), 0)
333 let k = ediff - s
334 let (q, r) = k > 37 ? (0, small) : small.quotientAndRemainder(dividingBy: try power10(k))
335 return (try scaleRounding(big, s + 1) + q * 10 + r.signum(), k - 1)
331 } 336 }
332 337
333 static func negate(_ a: Value) throws -> Value { 338 static func negate(_ a: Value) throws -> Value {
@@ -507,7 +512,22 @@ public enum Calc {
507 static func squareRootRaw(_ a: Value) throws -> Value { 512 static func squareRootRaw(_ a: Value) throws -> Value {
508 if isZero(a) { return a } 513 if isZero(a) { return a }
509 guard try sign(a) > 0 else { throw Unsupported("complex result") } 514 guard try sign(a) > 0 else { throw Unsupported("complex result") }
510 return try emacs({ $0.squareRoot() }, a) 515 if precision <= 16 { return try emacs({ $0.squareRoot() }, a) }
516 // Newton's method from the root of the first six or seven digits.
517 guard case .float(let m, let e) = a else { throw Unsupported("arguments") }
518 var ldiff = digits(Int128(m)) - 6
519 if (e + ldiff) % 2 != 0 { ldiff += 1 }
520 let head = ldiff >= 0 ? Int128(m) / (try power10(ldiff)) : Int128(m) * (try power10(-ldiff))
521 var root = Int128(Double(head).squareRoot())
522 while root * root > head { root -= 1 }
523 while (root + 1) * (root + 1) <= head { root += 1 }
524 var guess = try makeFloat(root, (e + ldiff) / 2)
525 for _ in 0..<1000 {
526 let next = try multiply(try add(guess, try divide(a, guess)), .float(5, -1))
527 if nearlyEqual(next, guess) { return next }
528 guess = next
529 }
530 throw Unsupported("number")
511 } 531 }
512 532
513 /// `math-exp-raw` of a float. 533 /// `math-exp-raw` of a float.
@@ -525,7 +545,100 @@ public enum Calc {
525 /// `math-arctan-raw` of a float. 545 /// `math-arctan-raw` of a float.
526 static func arctanRaw(_ x: Value) throws -> Value { 546 static func arctanRaw(_ x: Value) throws -> Value {
527 if try sign(x) < 0 { return try negate(try arctanRaw(try negate(x))) } 547 if try sign(x) < 0 { return try negate(try arctanRaw(try negate(x))) }
528 return isZero(x) ? x : try emacs({ Foundation.atan($0) }, x) 548 if isZero(x) { return x }
549 if precision <= 16 { return try emacs({ Foundation.atan($0) }, x) }
550 if symbolicMode { throw Unsupported("symbolic mode") }
551 if x == .float(1, 0) { return try piOver4() }
552 if try compare(.float(414214, -6), x) < 0 {
553 if try compare(.float(1, 0), x) < 0 {
554 return try subtract(try multiply(try pi(), .float(5, -1)), try arctanRaw(try divide(.float(1, 0), x)))
555 }
556 let reduced = try divide(try subtract(.float(1, 0), x), try add(.float(1, 0), x))
557 return try subtract(try multiply(try pi(), .float(25, -2)), try arctanRaw(reduced))
558 }
559 // `math-arctan-series`.
560 let xnegsqr = try negate(try multiply(x, x))
561 var (sum, power, n) = (x, x, 3)
562 for _ in 0..<10000 {
563 power = try multiply(power, xnegsqr)
564 let next = try add(sum, try divide(power, try float(.int(n))))
565 if nearlyEqual(sum, next) { return sum }
566 (sum, n) = (next, n + 2)
567 }
568 throw Unsupported("number")
569 }
570
571 /// `math-arctan2-raw` of floats.
572 static func arctan2Raw(_ y: Value, _ x: Value) throws -> Value {
573 if isZero(y) { return try sign(x) < 0 ? try pi() : .float(0, 0) }
574 if isZero(x) { return try sign(y) > 0 ? try piOver2() : try negate(try piOver2()) }
575 if try sign(x) > 0 { return try arctanRaw(try divide(y, x)) }
576 if try sign(y) > 0 { return try add(try arctanRaw(try divide(y, x)), try pi()) }
577 // `math-sub-float` gives pi itself, not its negative, when the angle is too small to count.
578 let angle = try arctanRaw(try divide(y, x))
579 if case .float(_, let e) = angle, case .float(_, let ep) = try pi(), ep - e >= 2 * precision { return try pi() }
580 return try subtract(angle, try pi())
581 }
582
583 /// `math-arcsin-raw` of a real float.
584 static func arcsinRaw(_ x: Value) throws -> Value {
585 try arctan2Raw(x, try squareRootRaw(try subtract(.float(1, 0), try multiply(x, x))))
586 }
587
588 /// `math-sin-raw` of a real float; `orgx` is the argument before reduction.
589 static func sinRaw(_ x: Value, _ orgx: Value? = nil) throws -> Value {
590 let orgx = orgx ?? x
591 if try sign(x) < 0 { return try negate(try sinRaw(try negate(x), orgx)) }
592 if try compare(.float(7, 0), x) < 0 {
593 // `math-mod` by 2 pi.
594 let turns = try floorInt(try divide(x, try twoPi()))
595 return try sinRaw(try subtract(x, try multiply(.int(turns), try twoPi())), orgx)
596 }
597 return try sinRaw2(x, orgx)
598 }
599
600 /// `math-cos-raw` of a real float.
601 static func cosRaw(_ x: Value) throws -> Value {
602 try sinRaw(try subtract(try piOver2(), x), x)
603 }
604
605 /// `math-nearly-zerop-float`: `a` is zero next to `b`, at one digit less.
606 static func nearlyZero(_ a: Value, _ b: Value) -> Bool {
607 guard case .float(let ma, let ea) = a, case .float(let mb, let eb) = b else { return false }
608 return ma == 0 || digits(Int128(ma)) + ea <= 1 + (mb == 0 ? 0 : digits(Int128(mb))) + eb - (precision - 1)
609 }
610
611 /// `math-sin-raw-2`.
612 static func sinRaw2(_ x: Value, _ orgx: Value) throws -> Value {
613 let xmpo2 = try subtract(try piOver2(), x)
614 if try sign(xmpo2) < 0 { return try negate(try sinRaw2(try subtract(x, try pi()), orgx)) }
615 if try compare(try piOver4(), x) < 0 { return try cosRaw2(xmpo2, orgx) }
616 if try compare(x, try negate(try piOver4())) < 0 { return try negate(try cosRaw2(try add(try piOver2(), x), orgx)) }
617 if nearlyZero(x, orgx) { return .float(0, 0) }
618 if precision <= 16 { return try emacs({ Foundation.sin($0) }, x) }
619 if symbolicMode { throw Unsupported("symbolic mode") }
620 return try sinSeries(x, 6, 4, x, try negate(try multiply(x, x)))
621 }
622
623 /// `math-cos-raw-2`.
624 static func cosRaw2(_ x: Value, _ orgx: Value) throws -> Value {
625 if nearlyZero(x, orgx) { return .float(1, 0) }
626 if precision <= 16 { return try emacs({ Foundation.cos($0) }, x) }
627 if symbolicMode { throw Unsupported("symbolic mode") }
628 let xnegsqr = try negate(try multiply(x, x))
629 return try sinSeries(try add(.float(1, 0), try multiply(xnegsqr, .float(5, -1))), 24, 5, xnegsqr, xnegsqr)
630 }
631
632 /// `math-sin-series`.
633 static func sinSeries(_ sum: Value, _ nfac: Int, _ n: Int, _ x: Value, _ xnegsqr: Value) throws -> Value {
634 var (sum, nfac, n, x) = (sum, Value.int(nfac), n, x)
635 for _ in 0..<10000 {
636 x = try multiply(x, xnegsqr)
637 let next = try add(sum, try divide(x, try float(nfac)))
638 if nearlyEqual(sum, next) { return sum }
639 (sum, nfac, n) = (next, try multiply(nfac, .int(n * (n + 1))), n + 2)
640 }
641 throw Unsupported("number")
529 } 642 }
530 643
531 static func ln2() throws -> Value { try makeFloat(693147180559945309417232121458176568, -36) } 644 static func ln2() throws -> Value { try makeFloat(693147180559945309417232121458176568, -36) }
@@ -636,10 +749,11 @@ public enum Calc {
636 return (pow == x, sum) 749 return (pow == x, sum)
637 } 750 }
638 751
639 /// `math-round` in `math-rounding-mode` `even` or `up`. 752 /// `math-round` in `math-rounding-mode` `even` or `up`, or with `away` the default mode,
640 static func round(_ a: Value, even: Bool) throws -> Value { 753 /// which rounds halves away from zero.
754 static func round(_ a: Value, even: Bool, away: Bool = false) throws -> Value {
641 if isWhole(a) { return .int(try trunc(a)) } 755 if isWhole(a) { return .int(try trunc(a)) }
642 if try sign(a) < 0, even { return try negate(try round(try negate(a), even: even)) } 756 if try sign(a) < 0, even || away { return try negate(try round(try negate(a), even: even)) }
643 let b = try add(a, rational(a) != nil ? .frac(1, 2) : .float(5, -1)) 757 let b = try add(a, rational(a) != nil ? .frac(1, 2) : .float(5, -1))
644 var f = try floorInt(b) 758 var f = try floorInt(b)
645 if even, isWhole(b), f % 2 != 0 { f -= 1 } 759 if even, isWhole(b), f % 2 != 0 { f -= 1 }
@@ -737,12 +851,23 @@ public enum Calc {
737 851
738 // MARK: - Display 852 // MARK: - Display
739 853
854 /// `math-constp` of the values here: a real number, or a vector of them.
855 static func isConstant(_ v: Value) -> Bool {
856 switch v {
857 case .int, .float, .frac: true
858 case .vector(let items): items.allSatisfy(isConstant)
859 default: false
860 }
861 }
862
740 /// `math-format-number`. 863 /// `math-format-number`.
741 static func format(_ value: Value, _ floatFormat: FloatFormat) throws -> String { 864 static func format(_ value: Value, _ floatFormat: FloatFormat) throws -> String {
742 switch value { 865 switch value {
743 case .int(let i): return String(i) 866 case .int(let i): return String(i)
744 case .frac(let n, let d): return "\(n):\(d)" 867 case .frac(let n, let d): return "\(n):\(d)"
745 case .vector: throw Unsupported("vector result") 868 case .vector(let items):
869 guard isConstant(value) else { throw Unsupported("vector result") }
870 return "[" + (try items.map { try format($0, floatFormat) }).joined(separator: ", ") + "]"
746 case .symbolic(let t): return try Symbolic.format(t, floatFormat) 871 case .symbolic(let t): return try Symbolic.format(t, floatFormat)
747 case .nan: return "nan" 872 case .nan: return "nan"
748 case .date(let d): return "<" + (try formatDate(d)) + ">" 873 case .date(let d): return "<" + (try formatDate(d)) + ">"
@@ -759,7 +884,8 @@ public enum Calc {
759 case .sci(let n): (figs, kind) = (n, "sci") 884 case .sci(let n): (figs, kind) = (n, "sci")
760 case .eng(let n): (figs, kind) = (n, "eng") 885 case .eng(let n): (figs, kind) = (n, "eng")
761 } 886 }
762 if kind == "fix", figs < 0 || exp + digits(mant) > -figs { 887 // `math-numdigs` of zero is 0, so zero with no decimal places goes to scientific.
888 if kind == "fix", figs < 0 || exp + (mant == 0 ? 0 : digits(mant)) > -figs {
763 if figs < 0 { figs = -figs } 889 if figs < 0 { figs = -figs }
764 mant = try scaleRounding(mant, exp + figs) 890 mant = try scaleRounding(mant, exp + figs)
765 var str = String(mant) 891 var str = String(mant)
@@ -1379,12 +1505,11 @@ public enum Calc {
1379 1505
1380 /// `math-mod`: the remainder with the divisor's sign. 1506 /// `math-mod`: the remainder with the divisor's sign.
1381 func mod(_ a: Value, _ b: Value) throws -> Value { 1507 func mod(_ a: Value, _ b: Value) throws -> Value {
1382 if case .int(let x) = a, case .int(let y) = b { 1508 if isZero(a) { return a }
1383 guard y != 0 else { throw Unsupported("division by zero") } 1509 if isZero(b) { throw Unsupported("division by zero") }
1384 let r = x % y 1510 // `math-imod` only for natural numbers; otherwise the quotient is a float without F.
1385 return .int(r != 0 && (r < 0) != (y < 0) ? r + y : r) 1511 if case .int(let x) = a, case .int(let y) = b, x >= 0, y > 0 { return .int(x % y) }
1386 } 1512 let q = try floorOf(try divide(a, b, fractions: modes.preferFractions))
1387 let q = try floorOf(try divide(a, b))
1388 return try subtract(a, try multiply(b, q)) 1513 return try subtract(a, try multiply(b, q))
1389 } 1514 }
1390 1515
@@ -1400,6 +1525,18 @@ public enum Calc {
1400 1525
1401 func floorOf(_ v: Value) throws -> Value { 1526 func floorOf(_ v: Value) throws -> Value {
1402 if case .int = v { return v } 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)) }
1529 if case .float(let m, let e) = v {
1530 if e >= 0 {
1531 guard e <= 18 else { throw Unsupported("integer size") }
1532 let (i, overflow) = m.multipliedReportingOverflow(by: Int(try power10(e)))
1533 if overflow { throw Unsupported("integer size") }
1534 return .int(i)
1535 }
1536 guard -e <= 18 else { return .int(m < 0 ? -1 : 0) }
1537 let d = Int(try power10(-e))
1538 return .int(m / d - (m % d < 0 ? 1 : 0))
1539 }
1403 guard let i = Int(exactly: try double(v).rounded(.down)) else { throw Unsupported("integer size") } 1540 guard let i = Int(exactly: try double(v).rounded(.down)) else { throw Unsupported("integer size") }
1404 return .int(i) 1541 return .int(i)
1405 } 1542 }
@@ -1456,8 +1593,6 @@ public enum Calc {
1456 return calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: modes.now ?? Date()) 1593 return calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: modes.now ?? Date())
1457 } 1594 }
1458 1595
1459 func radians(_ x: Double) -> Double { modes.degrees ? x * .pi / 180 : x }
1460 func angle(_ x: Double) -> Double { modes.degrees ? x * 180 / .pi : x }
1461 1596
1462 func call(_ name: String, _ args: [Value]) throws -> Value { 1597 func call(_ name: String, _ args: [Value]) throws -> Value {
1463 switch name { 1598 switch name {
@@ -1490,7 +1625,7 @@ public enum Calc {
1490 if items.contains(where: { if case .date = $0 { true } else { false } }) { throw Unsupported("arguments") } 1625 if items.contains(where: { if case .date = $0 { true } else { false } }) { throw Unsupported("arguments") }
1491 try items.sort { try compare($0, $1) < 0 } 1626 try items.sort { try compare($0, $1) < 0 }
1492 if items.count % 2 == 1 { return items[items.count / 2] } 1627 if items.count % 2 == 1 { return items[items.count / 2] }
1493 return try divide(try add(items[items.count / 2 - 1], items[items.count / 2]), .int(2)) 1628 return try divide(try add(items[items.count / 2 - 1], items[items.count / 2]), .int(2), fractions: modes.preferFractions)
1494 case "if": 1629 case "if":
1495 guard args.count == 3 else { throw Unsupported("arguments") } 1630 guard args.count == 3 else { throw Unsupported("arguments") }
1496 return try truth(args[0]) ? args[1] : args[2] 1631 return try truth(args[0]) ? args[1] : args[2]
@@ -1542,12 +1677,15 @@ public enum Calc {
1542 return .date(try call(name, [d])) 1677 return .date(try call(name, [d]))
1543 } 1678 }
1544 var x = args[0] 1679 var x = args[0]
1545 if case .frac(let n, let d) = x { x = try divide(.int(n), .int(d)) }
1546 if args.count == 2 { 1680 if args.count == 2 {
1547 guard case .int(let places) = args[1] else { throw Unsupported("arguments") } 1681 // As `rounde`: scaled to an integer with fractions, then scaled back.
1548 guard case .float(let m, let e) = x else { return x } 1682 guard isWhole(args[1]), case let places = try trunc(args[1]) else { throw Unsupported("arguments") }
1549 return try makeFloat(try scaleRounding(Int128(m), e + places), -places) 1683 _ = try sign(x)
1684 if places <= 0, case .int = x { return x }
1685 let rounded = try round(try scale(x, places, fractions: true), even: false, away: true)
1686 return try scale(rounded, -places, fractions: modes.preferFractions)
1550 } 1687 }
1688 if case .frac(let n, let d) = x { x = try divide(.int(n), .int(d)) }
1551 guard case .float(let m, let e) = x else { return x } 1689 guard case .float(let m, let e) = x else { return x }
1552 guard let i = Int(exactly: try scaleRounding(Int128(m), e)) else { throw Unsupported("integer size") } 1690 guard let i = Int(exactly: try scaleRounding(Int128(m), e)) else { throw Unsupported("integer size") }
1553 return .int(i) 1691 return .int(i)
@@ -1583,33 +1721,43 @@ public enum Calc {
1583 } 1721 }
1584 case "sin", "cos", "tan": 1722 case "sin", "cos", "tan":
1585 let x = try one(args) 1723 let x = try one(args)
1586 if modes.degrees, case .int(let i) = x, i % 90 == 0 { 1724 if x == .nan { return x }
1587 let quarter = ((i / 90) % 4 + 4) % 4 1725 if case .int(let i) = x, modes.degrees ? i % (name == "tan" ? 180 : 90) == 0 : i == 0 {
1588 switch name { 1726 if name == "tan" { return .int(0) }
1589 case "sin": return .int([0, 1, 0, -1][quarter]) 1727 // `(aref [0 1 0 -1] (math-mod (/ x 90) 4))`: a float quotient in math-mod can put
1590 case "cos": return .int([1, 0, -1, 0][quarter]) 1728 // the index out of range, and Calc then leaves the call alone.
1591 default: 1729 guard case .int(let quarter) = try mod(.int(i / 90), .int(4)), (0...3).contains(quarter) else {
1592 guard quarter % 2 == 0 else { throw Unsupported("infinite result") } 1730 throw Unsupported("number size")
1593 return .int(0)
1594 } 1731 }
1732 return .int((name == "sin" ? [0, 1, 0, -1] : [1, 0, -1, 0])[quarter])
1733 }
1734 return try extra(2) {
1735 let r = try float(x)
1736 let radians = modes.degrees ? try multiply(r, try piOver180()) : r
1737 if name == "sin" { return try sinRaw(radians) }
1738 if name == "cos" { return try cosRaw(radians) }
1739 let cosine = try cosRaw(radians)
1740 // Calc leaves the quotient by zero alone.
1741 if isZero(cosine) { throw Unsupported("division by zero") }
1742 return try divide(try sinRaw(radians), cosine)
1595 } 1743 }
1596 if !modes.degrees, x == .int(0) { return .int(name == "cos" ? 1 : 0) }
1597 let r = radians(try double(x))
1598 return try fromDouble(name == "sin" ? Foundation.sin(r) : name == "cos" ? Foundation.cos(r) : Foundation.tan(r))
1599 case "arcsin", "arccos", "arctan": 1744 case "arcsin", "arccos", "arctan":
1600 let x = try one(args) 1745 let x = try one(args)
1601 if modes.degrees, case .int(let i) = x { 1746 if modes.degrees, case .int(let i) = x {
1602 switch (name, i) { 1747 switch (name, i) {
1603 case ("arcsin", -1), ("arcsin", 0), ("arcsin", 1): return .int(90 * i) 1748 case ("arcsin", -1), ("arcsin", 1): return .int(90 * i)
1604 case ("arccos", -1), ("arccos", 0), ("arccos", 1): return .int(90 - 90 * i) 1749 case ("arccos", -1), ("arccos", 0): return .int(90 - 90 * i)
1605 case ("arctan", -1), ("arctan", 0), ("arctan", 1): return .int(45 * i) 1750 case ("arctan", -1), ("arctan", 1): return .int(45 * i)
1606 default: break 1751 default: break
1607 } 1752 }
1608 } 1753 }
1609 let d = try double(x) 1754 if x == .int(name == "arccos" ? 1 : 0) { return .int(0) }
1610 if name != "arctan", abs(d) > 1 { throw Unsupported("complex result") } 1755 if x == .nan { return x }
1611 let r = name == "arcsin" ? Foundation.asin(d) : name == "arccos" ? Foundation.acos(d) : Foundation.atan(d) 1756 return try extra(2) {
1612 return try fromDouble(angle(r)) 1757 let r = try float(x)
1758 let angle = name == "arcsin" ? try arcsinRaw(r) : name == "arccos" ? try subtract(try piOver2(), try arcsinRaw(r)) : try arctanRaw(r)
1759 return modes.degrees ? try divide(angle, try piOver180()) : angle
1760 }
1613 case "eq", "neq", "lt", "gt", "leq", "geq", "land", "lor": 1761 case "eq", "neq", "lt", "gt", "leq", "geq", "land", "lor":
1614 guard args.count == 2 else { throw Unsupported("arguments") } 1762 guard args.count == 2 else { throw Unsupported("arguments") }
1615 let ops: [String: Character] = ["eq": "=", "neq": "≠", "lt": "<", "gt": ">", "leq": "≤", "geq": "≥", "land": "&", "lor": "|"] 1763 let ops: [String: Character] = ["eq": "=", "neq": "≠", "lt": "<", "gt": ">", "leq": "≤", "geq": "≥", "land": "&", "lor": "|"]
@@ -1722,7 +1870,8 @@ public enum Calc {
1722 if x == .int(0) { return x } 1870 if x == .int(0) { return x }
1723 _ = try sign(x) 1871 _ = try sign(x)
1724 return try extra(2) { 1872 return try extra(2) {
1725 let e = try extra(2) { try expRaw(try float(x)) } 1873 let f = try float(x)
1874 let e = try extra(2) { try expRaw(f) }
1726 let inverse = try divide(.int(1), e) 1875 let inverse = try divide(.int(1), e)
1727 return try divide(try subtract(e, inverse), try add(e, inverse)) 1876 return try divide(try subtract(e, inverse), try add(e, inverse))
1728 } 1877 }
@@ -1750,19 +1899,7 @@ public enum Calc {
1750 guard args.count == 2 else { throw Unsupported("arguments") } 1899 guard args.count == 2 else { throw Unsupported("arguments") }
1751 _ = (try sign(args[0]), try sign(args[1])) 1900 _ = (try sign(args[0]), try sign(args[1]))
1752 return try extra(2) { 1901 return try extra(2) {
1753 let (y, x) = (try float(args[0]), try float(args[1])) 1902 let r = try arctan2Raw(try float(args[0]), try float(args[1]))
1754 let r: Value
1755 if isZero(y) {
1756 r = try sign(x) < 0 ? try pi() : .float(0, 0)
1757 } else if isZero(x) {
1758 r = try sign(y) > 0 ? try piOver2() : try negate(try piOver2())
1759 } else if try sign(x) > 0 {
1760 r = try arctanRaw(try divide(y, x))
1761 } else if try sign(y) > 0 {
1762 r = try add(try arctanRaw(try divide(y, x)), try pi())
1763 } else {
1764 r = try subtract(try arctanRaw(try divide(y, x)), try pi())
1765 }
1766 return modes.degrees ? try divide(r, try piOver180()) : r 1903 return modes.degrees ? try divide(r, try piOver180()) : r
1767 } 1904 }
1768 case "deg", "rad": 1905 case "deg", "rad":
@@ -1774,6 +1911,8 @@ public enum Calc {
1774 case "vgmean", "vhmean": 1911 case "vgmean", "vhmean":
1775 let items = try elements(args) 1912 let items = try elements(args)
1776 guard !items.isEmpty else { throw Unsupported("empty vector") } 1913 guard !items.isEmpty else { throw Unsupported("empty vector") }
1914 // Calc's root of one float, a power of 1:1, stays unevaluated.
1915 if name == "vgmean", items.count == 1, case .float = items[0] { throw Unsupported("arguments") }
1777 return try extra(2) { 1916 return try extra(2) {
1778 if name == "vhmean" { 1917 if name == "vhmean" {
1779 let inverses = try items.map { try divide(.int(1), $0, fractions: modes.preferFractions) } 1918 let inverses = try items.map { try divide(.int(1), $0, fractions: modes.preferFractions) }
Tests/OrgCoreTests/CalcNumericTests.swift added +166
@@ -0,0 +1,166 @@
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct CalcNumericTests {
6 /// A random real number up to `scale` digits before the point: an integer, a fraction or
7 /// a float, tiny to large.
8 static func argument(_ generator: inout SystemRandomNumberGenerator, scale: Int = 6) -> String {
9 let sign = Bool.random(using: &generator) ? "-" : ""
10 let digits = Int.random(in: 1...9, using: &generator)
11 let mantissa = String(Int.random(in: 1...999_999_999, using: &generator)).prefix(digits)
12 switch Int.random(in: 0..<5, using: &generator) {
13 case 0: return sign + String(Int.random(in: 0...(scale > 2 ? 720 : 70), using: &generator))
14 case 1: return sign + "\(Int.random(in: 1...99, using: &generator)):\(Int.random(in: 2...99, using: &generator))"
15 case 2: return sign + "\(mantissa)e\(Int.random(in: -30...(scale - mantissa.count), using: &generator))"
16 case 3: return sign + "\(mantissa)e\(Int.random(in: -mantissa.count - 1...(scale - mantissa.count), using: &generator))"
17 default: return sign + ["90.", "180.", "45", "30", "1.5707963267949", "3.14159265358979", "0.785398163397448", "1", "1.", "0.5",
18 "1e-10", "6.28318530717959", "7.", "7.00000000000001"].randomElement(using: &generator)!
19 }
20 }
21
22 /// A random number from -1 to 1.
23 static func unit(_ generator: inout SystemRandomNumberGenerator) -> String {
24 let sign = Bool.random(using: &generator) ? "-" : ""
25 let mantissa = String(Int.random(in: 1...999_999_999, using: &generator)).prefix(Int.random(in: 1...9, using: &generator))
26 switch Int.random(in: 0..<4, using: &generator) {
27 case 0: return sign + ["0", "1", "1.", "0.5", "0.", "0.999999999999", "1e-10"].randomElement(using: &generator)!
28 case 1:
29 let d = Int.random(in: 2...99, using: &generator)
30 return sign + "\(Int.random(in: 1..<d, using: &generator)):\(d)"
31 case 2: return sign + "\(mantissa)e\(-mantissa.count - Int.random(in: 0...20, using: &generator))"
32 default: return sign + "0.\(mantissa)"
33 }
34 }
35
36 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
37 func trigonometryMatchesCalc() throws {
38 var generator = SystemRandomNumberGenerator()
39 for precision in [3, 6, 8, 12, 14, 15, 16, 17, 18] {
40 for radians in [false, true] {
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)", "sin(-74250)", "cos(-74250)",
43 "sin(-360000000000090)", "cos(-90*4000000000001)", "tan(-360000000000180)"].map { ($0, "(float 0)") }
44 for _ in 0..<500 {
45 let f = ["sin", "cos", "tan", "arcsin", "arccos", "arctan", "arctan2", "sinh", "cosh", "tanh", "sqrt"].randomElement(using: &generator)!
46 let x = switch f {
47 case "arcsin", "arccos": Self.unit(&generator)
48 case "arctan2": Self.argument(&generator) + "," + Self.argument(&generator)
49 case "sinh", "cosh", "tanh": Self.argument(&generator, scale: 1)
50 case "sqrt": Self.argument(&generator).replacingOccurrences(of: "-", with: "")
51 default: Self.argument(&generator)
52 }
53 cases.append(("\(f)(\(x))", "(float 0)"))
54 }
55 var modes = Calc.Modes()
56 (modes.precision, modes.degrees, modes.format) = (precision, !radians, .float(0))
57 var refused: [String] = []
58 for (c, expected) in zip(cases, try CalcOracle.run(cases, precision: precision, radians: radians)) {
59 guard let ours = try? Calc.evaluate(c.0, modes: modes) else {
60 // Calc keeps a tangent of a quotient by zero unevaluated, and Calc's exp
61 // past 16 digits, worked out at two (four for tanh) digits more, isn't reproduced. Past
62 // 16 digits, two more don't always fit in a mantissa.
63 let name = String(c.0.prefix { $0 != "(" })
64 if precision <= 16, !expected.hasPrefix(name + "("), !(precision >= 13 && name == "tanh"), !(precision >= 15 && ["sinh", "cosh"].contains(name)) {
65 refused.append(c.0)
66 }
67 continue
68 }
69 #expect(ours == expected, "\(c.0) p\(precision) \(radians ? "rad" : "deg")")
70 }
71 #expect(refused == [], "p\(precision) \(radians ? "rad" : "deg") \(refused)")
72 }
73 }
74 }
75
76 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
77 func zeroInEveryFormatMatchesCalc() throws {
78 var cases: [(String, String)] = []
79 for kind in ["float", "fix", "sci", "eng"] {
80 for figs in [0, 1, 2, 3, 8, -2] {
81 for e in ["0.", "-0.", "0.0", "1-1.0", "0./0", "0.e5", "0.001", "0.5", "-0.4", "4e-5", "123.456"] {
82 cases.append((e, "(\(kind) \(figs))"))
83 }
84 }
85 }
86 let skipped = try CalcTests.compare(cases)
87 #expect(skipped == [], "\(skipped)")
88 }
89
90 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
91 func fractionModeMatchesCalc() throws {
92 var generator = SystemRandomNumberGenerator()
93 let numbers = ["0", "1", "2", "3", "-2", "7", "10", "12", "1:2", "1:3", "-5:4", "7:2", "-3:7", "0.5", "2.5", "-1.5", "0.25", "3.", "1.2e1",
94 "100", "0.123456789", "0."]
95 func number() -> String { numbers.randomElement(using: &generator)! }
96 func vector() -> String { "[" + (0..<Int.random(in: 1...5, using: &generator)).map { _ in number() }.joined(separator: ",") + "]" }
97 let unary = ["fact", "dfact", "abs", "floor", "ceil", "trunc", "round", "sqrt", "exp", "ln", "log10", "sin", "cos", "tan", "arcsin",
98 "arccos", "arctan", "lnot", "percent", "float", "pfloat", "sign", "rounde", "roundu", "frac", "exp10", "sinh", "cosh",
99 "tanh", "arcsinh", "arccosh", "arctanh", "deg", "rad"]
100 let binary = ["mod", "idiv", "max", "min", "round", "rounde", "roundu", "fdiv", "gcd", "lcm", "choose", "perm", "pow", "nroot",
101 "hypot", "log", "arctan2", "eq", "lt", "land", "lor", "agmean"]
102 let statistics = ["vsum", "vprod", "vcount", "vmean", "vmax", "vmin", "vmedian", "vvar", "vpvar", "vsdev", "vpsdev", "vflat", "vgmean", "vhmean"]
103 var cases = ["vmedian([1,2])", "round(2.5,1)", "round(1:3,2)", "rounde(2.45,1)", "vmean([1,2])", "vmedian([1:3,1])", "7/2", "1/3+1/6",
104 "2^-1", "0.5^-1", "(1:2)^0.5", "4^(1:2)", "8^(-1:3)", "7%2", "7\\2", "<2026-10-05 Mon>+1/2", "vsdev([1,2])",
105 "round(3.,-2)", "round(-2.25,1)", "round(2.5,1.)", "mod(0,0.5)", "0%0.5", "vgmean([0.5])", "vgmean([1:4])"].map { ($0, "(float 12)") }
106 for _ in 0..<1500 {
107 switch Int.random(in: 0..<4, using: &generator) {
108 case 0: cases.append(("\(unary.randomElement(using: &generator)!)(\(number()))", "(float 12)"))
109 case 1: cases.append(("\(binary.randomElement(using: &generator)!)(\(number()),\(number()))", "(float 12)"))
110 case 2: cases.append(("\(statistics.randomElement(using: &generator)!)(\(vector()))", "(float 12)"))
111 default:
112 let op = ["+", "-", "*", "/", "^", "%", "\\"].randomElement(using: &generator)!
113 cases.append(("(\(number()))\(op)(\(number()))", "(float 12)"))
114 }
115 }
116 for fractions in [true, false] { _ = try CalcTests.compare(cases, fractions: fractions) }
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 let wide = ["mod(-1234567890123456789,7)", "(-1234567890123456789)%7", "mod(1234567890123456789,-7)", "mod(-29999999999999999,-10)",
131 "(-29999999999999999)%(-10)", "mod(-9999999999999999,-10)"].map { ($0, "(float 0)") }
132 for precision in [16, 17, 18] {
133 for fractions in [true, false] {
134 let skipped = try CalcTests.compare(wide, precision: precision, fractions: fractions)
135 #expect(skipped == [], "p\(precision) \(skipped)")
136 }
137 }
138 }
139
140 /// Calc's `math-overflow` and `math-underflow` past an exponent of 4,000,000 leave the formula alone.
141 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
142 func floatOverflowIsRefusedAsCalcDoes() throws {
143 let refused = ["2.5^123456789012", "pow(-1.5,123456789012)", "pow(1e20,123456789012)", "0.4^123456789012", "2.5^(-123456789012)"]
144 let cases = (refused + ["2.5^1000000", "0.4^1000000"]).map { ($0, "(float 0)") }
145 for fractions in [true, false] {
146 let skipped = try CalcTests.compare(cases, fractions: fractions)
147 #expect(skipped == refused)
148 }
149 }
150
151 /// Vector results, with and without `N`, zero with no decimal places, and fractions.
152 static let tables = [
153 "| 1 | 2 | | |\n|---+---+---+---|\n| 2.5 | 0.25 | | |\n#+TBLFM: $3=vmedian($1..$2);F::$4=round($1,1);F\n",
154 "| 1 | 2 | |\n|---+---+---|\n| 3 | x | |\n| 1.5 | 0. | |\n#+TBLFM: $3=$1..$2;N\n",
155 "| 1 | 2 | |\n|---+---+---|\n| 1.5 | 0. | |\n| 1:3 | -2 | |\n#+TBLFM: $3=$1..$2\n",
156 "| 1.5 | 0 | |\n|---+---+---|\n| 2 | 4 | |\n#+TBLFM: $3=$1..$2;Nf0\n",
157 "| 1 | 2 | |\n|---+---+---|\n| 3 | 4 | |\n#+TBLFM: @1$3=@1$1..@2$2;N::@2$3=vflat(@1$1..@2$2);N\n",
158 "| 1 | 2 | |\n#+TBLFM: $3=[$1, [$2]];N\n",
159 "| 0 | | | |\n|---+---+---+---|\n| 0.5 | | | |\n#+TBLFM: $2=$1*1.0;f0::$3=($1-$1)*1.;s2::$4=($1-$1)*1./0;f0\n",
160 ]
161
162 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
163 func vectorsAndZeroInTablesMatchEmacsNatively() throws {
164 try TableFormulaTests().compare(Self.tables, fallbacksAllowed: false, headerFallbacksAllowed: false)
165 }
166}
Tests/OrgCoreTests/CalcTests.swift +13 −4
@@ -4,7 +4,8 @@ import Testing
4 4
5/// Runs `calc-eval` with org's table modes over a list of expressions. 5/// Runs `calc-eval` with org's table modes over a list of expressions.
6enum CalcOracle { 6enum CalcOracle {
7 static func run(_ cases: [(expression: String, format: String)], precision: Int = 12, zone: String = "UTC", symbolic: Bool = false) throws -> [String] { 7 static func run(_ cases: [(expression: String, format: String)], precision: Int = 12, zone: String = "UTC", symbolic: Bool = false,
8 radians: Bool = false, fractions: Bool = false) throws -> [String] {
8 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-calc-\(UUID().uuidString)") 9 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-calc-\(UUID().uuidString)")
9 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) 10 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
10 defer { try? FileManager.default.removeItem(at: folder) } 11 defer { try? FileManager.default.removeItem(at: folder) }
@@ -27,6 +28,8 @@ enum CalcOracle {
27 (setq modes (plist-put modes 'calc-float-format format)) 28 (setq modes (plist-put modes 'calc-float-format format))
28 (setq modes (plist-put modes 'calc-internal-prec (string-to-number (getenv "ORACLE_PREC")))) 29 (setq modes (plist-put modes 'calc-internal-prec (string-to-number (getenv "ORACLE_PREC"))))
29 (when (getenv "ORACLE_SYMBOLIC") (setq modes (plist-put modes 'calc-symbolic-mode t))) 30 (when (getenv "ORACLE_SYMBOLIC") (setq modes (plist-put modes 'calc-symbolic-mode t)))
31 (when (getenv "ORACLE_RADIANS") (setq modes (plist-put modes 'calc-angle-mode 'rad)))
32 (when (getenv "ORACLE_FRACTIONS") (setq modes (plist-put modes 'calc-prefer-frac t)))
30 (let ((r (condition-case e (calc-eval (cons (car c) modes) nil) 33 (let ((r (condition-case e (calc-eval (cons (car c) modes) nil)
31 (error (format "LISP ERROR %S" e))))) 34 (error (format "LISP ERROR %S" e)))))
32 (if (stringp r) r (format "ERROR %S" r))))) 35 (if (stringp r) r (format "ERROR %S" r)))))
@@ -34,7 +37,10 @@ enum CalcOracle {
34 (with-temp-file (getenv "ORACLE_OUTPUT") 37 (with-temp-file (getenv "ORACLE_OUTPUT")
35 (insert (json-encode (vconcat results))))) 38 (insert (json-encode (vconcat results)))))
36 """#.write(to: script, atomically: true, encoding: .utf8) 39 """#.write(to: script, atomically: true, encoding: .utf8)
37 _ = try EmacsOracle.run(["-Q", "--batch", "-l", script.path], environment: ["ORACLE_INPUT": input.path, "ORACLE_OUTPUT": output.path, "ORACLE_PREC": String(precision), "TZ": zone].merging(symbolic ? ["ORACLE_SYMBOLIC": "1"] : [:]) { $1 }) 40 _ = try EmacsOracle.run(["-Q", "--batch", "-l", script.path], environment: ["ORACLE_INPUT": input.path, "ORACLE_OUTPUT": output.path, "ORACLE_PREC": String(precision), "TZ": zone]
41 .merging(symbolic ? ["ORACLE_SYMBOLIC": "1"] : [:]) { $1 }
42 .merging(radians ? ["ORACLE_RADIANS": "1"] : [:]) { $1 }
43 .merging(fractions ? ["ORACLE_FRACTIONS": "1"] : [:]) { $1 })
38 return try JSONDecoder().decode([String].self, from: Data(contentsOf: output)) 44 return try JSONDecoder().decode([String].self, from: Data(contentsOf: output))
39 } 45 }
40} 46}
@@ -224,14 +230,17 @@ struct CalcTests {
224 230
225 /// Each case through Calc and through `Calc.evaluate`, expecting the same text; returns the 231 /// Each case through Calc and through `Calc.evaluate`, expecting the same text; returns the
226 /// cases `Calc.evaluate` doesn't support. 232 /// cases `Calc.evaluate` doesn't support.
227 static func compare(_ cases: [(String, String)], precision: Int = 12, zone: (name: String, offset: Int) = ("UTC", 0), symbolic: Bool = false) throws -> [String] { 233 static func compare(_ cases: [(String, String)], precision: Int = 12, zone: (name: String, offset: Int) = ("UTC", 0), symbolic: Bool = false,
228 let emacs = try CalcOracle.run(cases, precision: precision, zone: zone.name, symbolic: symbolic) 234 radians: Bool = false, fractions: Bool = false) throws -> [String] {
235 let emacs = try CalcOracle.run(cases, precision: precision, zone: zone.name, symbolic: symbolic, radians: radians, fractions: fractions)
229 #expect(emacs.count == cases.count) 236 #expect(emacs.count == cases.count)
230 var skipped: [String] = [] 237 var skipped: [String] = []
231 for (c, expected) in zip(cases, emacs) { 238 for (c, expected) in zip(cases, emacs) {
232 var modes = Calc.Modes() 239 var modes = Calc.Modes()
233 modes.precision = precision 240 modes.precision = precision
234 modes.symbolic = symbolic 241 modes.symbolic = symbolic
242 modes.degrees = !radians
243 modes.preferFractions = fractions
235 modes.timeZone = TimeZone(secondsFromGMT: zone.offset)! 244 modes.timeZone = TimeZone(secondsFromGMT: zone.offset)!
236 let f = c.1.dropFirst().dropLast().split(separator: " ") 245 let f = c.1.dropFirst().dropLast().split(separator: " ")
237 let n = Int(f[1])! 246 let n = Int(f[1])!
docs/manual/guide/10-tables.org +6 −4
@@ -413,9 +413,10 @@ After the formula, a =;= starts its flags, as in ~$3=$1/$2;%.2f~ or ~$4=$1*2;f2~
413| =U= | As =T=, showing =HH:MM=. | 413| =U= | As =T=, showing =HH:MM=. |
414| =t= | As =T=, showing decimal hours with two places, such as =1.50=. | 414| =t= | As =T=, showing decimal hours with two places, such as =1.50=. |
415 415
416Above =p14=, functions worked out from decimal approximations (=sqrt= of a non-square, 416Above =p14=, functions worked out from decimal approximations (=exp=, =ln=, =log=,
417=exp=, =ln=, =log=, non-integer powers, trigonometric and hyperbolic functions) may need 417non-integer powers, =sinh= and =cosh=) may need Emacs, and so may =tanh= above =p12=.
418Emacs, and above =p16= so may the statistics functions and powers of decimal numbers. 418Above =p16= so may the statistics functions, powers of decimal numbers, =sqrt= of a
419non-square and the trigonometric functions.
419 420
420With =S=, the functions that may give an inexact result (=sqrt=, =exp=, =ln=, =log=, 421With =S=, the functions that may give an inexact result (=sqrt=, =exp=, =ln=, =log=,
421=log10=, =pow=, =nroot=, =hypot=, the inverse trigonometric and hyperbolic functions, 422=log10=, =pow=, =nroot=, =hypot=, the inverse trigonometric and hyperbolic functions,
@@ -685,7 +686,8 @@ what it was and the Mac recalculates the table in Emacs instead. This happens fo
685 holding text; 686 holding text;
686- 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
687 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;
688- complex results, vector results, and numbers too large for 64-bit 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;
689- Lisp functions not listed above. 691- Lisp functions not listed above.
690 692
691The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=. 693The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=.