| @@ -116,6 +116,8 @@ public enum Calc { |
| 116 | 116 | // Constants to 36 digits, rounded to the precision, as `math-defcache` gives them. |
| 117 | 117 | static func pi() throws -> Value { try makeFloat(314159265358979323846264338327950288, -35) } |
| 118 | 118 | static func piOver2() throws -> Value { try makeFloat(157079632679489661923132169163975144, -35) } |
| 119 | static func piOver4() throws -> Value { try makeFloat(785398163397448309615660845819875721, -36) } |
| 120 | static func twoPi() throws -> Value { try makeFloat(628318530717958647692528676655900577, -35) } |
| 119 | 121 | static func piOver180() throws -> Value { try makeFloat(174532925199432957692369076848861271, -37) } |
| 120 | 122 | |
| 121 | 123 | /// `math-float`. |
| @@ -277,19 +279,6 @@ public enum Calc { |
| 277 | 279 | } |
| 278 | 280 | } |
| 279 | 281 | |
| 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 | 282 | static func isSymbolic(_ vs: Value...) -> Bool { |
| 294 | 283 | vs.contains { if case .symbolic = $0 { true } else { false } } |
| 295 | 284 | } |
| @@ -324,10 +313,22 @@ public enum Calc { |
| 324 | 313 | let ediff = e1 - e2 |
| 325 | 314 | if ediff >= 0 { |
| 326 | 315 | if ediff >= 2 * precision { return try makeFloat(m1, e1) } |
| 327 | | return try makeFloat(try scaleRounding(m1, ediff) + m2, e2) |
| 316 | let (m, shift) = try align(m1, ediff, m2) |
| 317 | return try makeFloat(m, e2 + shift) |
| 328 | 318 | } |
| 329 | 319 | if -ediff >= 2 * precision { return try makeFloat(m2, e2) } |
| 330 | | return try makeFloat(m1 + (try scaleRounding(m2, -ediff)), e1) |
| 320 | let (m, shift) = try align(m2, -ediff, m1) |
| 321 | return try makeFloat(m, e1 + shift) |
| 322 | } |
| 323 | |
| 324 | /// `big` times 10^ediff plus `small`, and the power of ten that scales it. Past 37 digits |
| 325 | /// the digits of `small` below the precision become one sticky digit, which rounds the same. |
| 326 | static func align(_ big: Int128, _ ediff: Int, _ small: Int128) throws -> (Int128, Int) { |
| 327 | if digits(big) + ediff <= 37 { return (try scaleRounding(big, ediff) + small, 0) } |
| 328 | let s = max(precision + 2 - digits(big), 0) |
| 329 | let k = ediff - s |
| 330 | let (q, r) = k > 37 ? (0, small) : small.quotientAndRemainder(dividingBy: try power10(k)) |
| 331 | return (try scaleRounding(big, s + 1) + q * 10 + r.signum(), k - 1) |
| 331 | 332 | } |
| 332 | 333 | |
| 333 | 334 | static func negate(_ a: Value) throws -> Value { |
| @@ -507,7 +508,22 @@ public enum Calc { |
| 507 | 508 | static func squareRootRaw(_ a: Value) throws -> Value { |
| 508 | 509 | if isZero(a) { return a } |
| 509 | 510 | guard try sign(a) > 0 else { throw Unsupported("complex result") } |
| 510 | | return try emacs({ $0.squareRoot() }, a) |
| 511 | if precision <= 16 { return try emacs({ $0.squareRoot() }, a) } |
| 512 | // Newton's method from the root of the first six or seven digits. |
| 513 | guard case .float(let m, let e) = a else { throw Unsupported("arguments") } |
| 514 | var ldiff = digits(Int128(m)) - 6 |
| 515 | if (e + ldiff) % 2 != 0 { ldiff += 1 } |
| 516 | let head = ldiff >= 0 ? Int128(m) / (try power10(ldiff)) : Int128(m) * (try power10(-ldiff)) |
| 517 | var root = Int128(Double(head).squareRoot()) |
| 518 | while root * root > head { root -= 1 } |
| 519 | while (root + 1) * (root + 1) <= head { root += 1 } |
| 520 | var guess = try makeFloat(root, (e + ldiff) / 2) |
| 521 | for _ in 0..<1000 { |
| 522 | let next = try multiply(try add(guess, try divide(a, guess)), .float(5, -1)) |
| 523 | if nearlyEqual(next, guess) { return next } |
| 524 | guess = next |
| 525 | } |
| 526 | throw Unsupported("number") |
| 511 | 527 | } |
| 512 | 528 | |
| 513 | 529 | /// `math-exp-raw` of a float. |
| @@ -525,7 +541,100 @@ public enum Calc { |
| 525 | 541 | /// `math-arctan-raw` of a float. |
| 526 | 542 | static func arctanRaw(_ x: Value) throws -> Value { |
| 527 | 543 | if try sign(x) < 0 { return try negate(try arctanRaw(try negate(x))) } |
| 528 | | return isZero(x) ? x : try emacs({ Foundation.atan($0) }, x) |
| 544 | if isZero(x) { return x } |
| 545 | if precision <= 16 { return try emacs({ Foundation.atan($0) }, x) } |
| 546 | if symbolicMode { throw Unsupported("symbolic mode") } |
| 547 | if x == .float(1, 0) { return try piOver4() } |
| 548 | if try compare(.float(414214, -6), x) < 0 { |
| 549 | if try compare(.float(1, 0), x) < 0 { |
| 550 | return try subtract(try multiply(try pi(), .float(5, -1)), try arctanRaw(try divide(.float(1, 0), x))) |
| 551 | } |
| 552 | let reduced = try divide(try subtract(.float(1, 0), x), try add(.float(1, 0), x)) |
| 553 | return try subtract(try multiply(try pi(), .float(25, -2)), try arctanRaw(reduced)) |
| 554 | } |
| 555 | // `math-arctan-series`. |
| 556 | let xnegsqr = try negate(try multiply(x, x)) |
| 557 | var (sum, power, n) = (x, x, 3) |
| 558 | for _ in 0..<10000 { |
| 559 | power = try multiply(power, xnegsqr) |
| 560 | let next = try add(sum, try divide(power, try float(.int(n)))) |
| 561 | if nearlyEqual(sum, next) { return sum } |
| 562 | (sum, n) = (next, n + 2) |
| 563 | } |
| 564 | throw Unsupported("number") |
| 565 | } |
| 566 | |
| 567 | /// `math-arctan2-raw` of floats. |
| 568 | static func arctan2Raw(_ y: Value, _ x: Value) throws -> Value { |
| 569 | if isZero(y) { return try sign(x) < 0 ? try pi() : .float(0, 0) } |
| 570 | if isZero(x) { return try sign(y) > 0 ? try piOver2() : try negate(try piOver2()) } |
| 571 | if try sign(x) > 0 { return try arctanRaw(try divide(y, x)) } |
| 572 | if try sign(y) > 0 { return try add(try arctanRaw(try divide(y, x)), try pi()) } |
| 573 | // `math-sub-float` gives pi itself, not its negative, when the angle is too small to count. |
| 574 | let angle = try arctanRaw(try divide(y, x)) |
| 575 | if case .float(_, let e) = angle, case .float(_, let ep) = try pi(), ep - e >= 2 * precision { return try pi() } |
| 576 | return try subtract(angle, try pi()) |
| 577 | } |
| 578 | |
| 579 | /// `math-arcsin-raw` of a real float. |
| 580 | static func arcsinRaw(_ x: Value) throws -> Value { |
| 581 | try arctan2Raw(x, try squareRootRaw(try subtract(.float(1, 0), try multiply(x, x)))) |
| 582 | } |
| 583 | |
| 584 | /// `math-sin-raw` of a real float; `orgx` is the argument before reduction. |
| 585 | static func sinRaw(_ x: Value, _ orgx: Value? = nil) throws -> Value { |
| 586 | let orgx = orgx ?? x |
| 587 | if try sign(x) < 0 { return try negate(try sinRaw(try negate(x), orgx)) } |
| 588 | if try compare(.float(7, 0), x) < 0 { |
| 589 | // `math-mod` by 2 pi. |
| 590 | let turns = try floorInt(try divide(x, try twoPi())) |
| 591 | return try sinRaw(try subtract(x, try multiply(.int(turns), try twoPi())), orgx) |
| 592 | } |
| 593 | return try sinRaw2(x, orgx) |
| 594 | } |
| 595 | |
| 596 | /// `math-cos-raw` of a real float. |
| 597 | static func cosRaw(_ x: Value) throws -> Value { |
| 598 | try sinRaw(try subtract(try piOver2(), x), x) |
| 599 | } |
| 600 | |
| 601 | /// `math-nearly-zerop-float`: `a` is zero next to `b`, at one digit less. |
| 602 | static func nearlyZero(_ a: Value, _ b: Value) -> Bool { |
| 603 | guard case .float(let ma, let ea) = a, case .float(let mb, let eb) = b else { return false } |
| 604 | return ma == 0 || digits(Int128(ma)) + ea <= 1 + (mb == 0 ? 0 : digits(Int128(mb))) + eb - (precision - 1) |
| 605 | } |
| 606 | |
| 607 | /// `math-sin-raw-2`. |
| 608 | static func sinRaw2(_ x: Value, _ orgx: Value) throws -> Value { |
| 609 | let xmpo2 = try subtract(try piOver2(), x) |
| 610 | if try sign(xmpo2) < 0 { return try negate(try sinRaw2(try subtract(x, try pi()), orgx)) } |
| 611 | if try compare(try piOver4(), x) < 0 { return try cosRaw2(xmpo2, orgx) } |
| 612 | if try compare(x, try negate(try piOver4())) < 0 { return try negate(try cosRaw2(try add(try piOver2(), x), orgx)) } |
| 613 | if nearlyZero(x, orgx) { return .float(0, 0) } |
| 614 | if precision <= 16 { return try emacs({ Foundation.sin($0) }, x) } |
| 615 | if symbolicMode { throw Unsupported("symbolic mode") } |
| 616 | return try sinSeries(x, 6, 4, x, try negate(try multiply(x, x))) |
| 617 | } |
| 618 | |
| 619 | /// `math-cos-raw-2`. |
| 620 | static func cosRaw2(_ x: Value, _ orgx: Value) throws -> Value { |
| 621 | if nearlyZero(x, orgx) { return .float(1, 0) } |
| 622 | if precision <= 16 { return try emacs({ Foundation.cos($0) }, x) } |
| 623 | if symbolicMode { throw Unsupported("symbolic mode") } |
| 624 | let xnegsqr = try negate(try multiply(x, x)) |
| 625 | return try sinSeries(try add(.float(1, 0), try multiply(xnegsqr, .float(5, -1))), 24, 5, xnegsqr, xnegsqr) |
| 626 | } |
| 627 | |
| 628 | /// `math-sin-series`. |
| 629 | static func sinSeries(_ sum: Value, _ nfac: Int, _ n: Int, _ x: Value, _ xnegsqr: Value) throws -> Value { |
| 630 | var (sum, nfac, n, x) = (sum, Value.int(nfac), n, x) |
| 631 | for _ in 0..<10000 { |
| 632 | x = try multiply(x, xnegsqr) |
| 633 | let next = try add(sum, try divide(x, try float(nfac))) |
| 634 | if nearlyEqual(sum, next) { return sum } |
| 635 | (sum, nfac, n) = (next, try multiply(nfac, .int(n * (n + 1))), n + 2) |
| 636 | } |
| 637 | throw Unsupported("number") |
| 529 | 638 | } |
| 530 | 639 | |
| 531 | 640 | static func ln2() throws -> Value { try makeFloat(693147180559945309417232121458176568, -36) } |
| @@ -1456,8 +1565,6 @@ public enum Calc { |
| 1456 | 1565 | return calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: modes.now ?? Date()) |
| 1457 | 1566 | } |
| 1458 | 1567 | |
| 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 | 1568 | |
| 1462 | 1569 | func call(_ name: String, _ args: [Value]) throws -> Value { |
| 1463 | 1570 | switch name { |
| @@ -1583,33 +1690,42 @@ public enum Calc { |
| 1583 | 1690 | } |
| 1584 | 1691 | case "sin", "cos", "tan": |
| 1585 | 1692 | let x = try one(args) |
| 1586 | | if modes.degrees, case .int(let i) = x, i % 90 == 0 { |
| 1693 | if x == .nan { return x } |
| 1694 | if case .int(let i) = x, modes.degrees ? i % (name == "tan" ? 180 : 90) == 0 : i == 0 { |
| 1587 | 1695 | let quarter = ((i / 90) % 4 + 4) % 4 |
| 1588 | 1696 | switch name { |
| 1589 | 1697 | case "sin": return .int([0, 1, 0, -1][quarter]) |
| 1590 | 1698 | case "cos": return .int([1, 0, -1, 0][quarter]) |
| 1591 | | default: |
| 1592 | | guard quarter % 2 == 0 else { throw Unsupported("infinite result") } |
| 1593 | | return .int(0) |
| 1699 | default: return .int(0) |
| 1594 | 1700 | } |
| 1595 | 1701 | } |
| 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)) |
| 1702 | return try extra(2) { |
| 1703 | let r = try float(x) |
| 1704 | let radians = modes.degrees ? try multiply(r, try piOver180()) : r |
| 1705 | if name == "sin" { return try sinRaw(radians) } |
| 1706 | if name == "cos" { return try cosRaw(radians) } |
| 1707 | let cosine = try cosRaw(radians) |
| 1708 | // Calc leaves the quotient by zero alone. |
| 1709 | if isZero(cosine) { throw Unsupported("division by zero") } |
| 1710 | return try divide(try sinRaw(radians), cosine) |
| 1711 | } |
| 1599 | 1712 | case "arcsin", "arccos", "arctan": |
| 1600 | 1713 | let x = try one(args) |
| 1601 | 1714 | if modes.degrees, case .int(let i) = x { |
| 1602 | 1715 | switch (name, i) { |
| 1603 | | case ("arcsin", -1), ("arcsin", 0), ("arcsin", 1): return .int(90 * i) |
| 1604 | | case ("arccos", -1), ("arccos", 0), ("arccos", 1): return .int(90 - 90 * i) |
| 1605 | | case ("arctan", -1), ("arctan", 0), ("arctan", 1): return .int(45 * i) |
| 1716 | case ("arcsin", -1), ("arcsin", 1): return .int(90 * i) |
| 1717 | case ("arccos", -1), ("arccos", 0): return .int(90 - 90 * i) |
| 1718 | case ("arctan", -1), ("arctan", 1): return .int(45 * i) |
| 1606 | 1719 | default: break |
| 1607 | 1720 | } |
| 1608 | 1721 | } |
| 1609 | | let d = try double(x) |
| 1610 | | if name != "arctan", abs(d) > 1 { throw Unsupported("complex result") } |
| 1611 | | let r = name == "arcsin" ? Foundation.asin(d) : name == "arccos" ? Foundation.acos(d) : Foundation.atan(d) |
| 1612 | | return try fromDouble(angle(r)) |
| 1722 | if x == .int(name == "arccos" ? 1 : 0) { return .int(0) } |
| 1723 | if x == .nan { return x } |
| 1724 | return try extra(2) { |
| 1725 | let r = try float(x) |
| 1726 | let angle = name == "arcsin" ? try arcsinRaw(r) : name == "arccos" ? try subtract(try piOver2(), try arcsinRaw(r)) : try arctanRaw(r) |
| 1727 | return modes.degrees ? try divide(angle, try piOver180()) : angle |
| 1728 | } |
| 1613 | 1729 | case "eq", "neq", "lt", "gt", "leq", "geq", "land", "lor": |
| 1614 | 1730 | guard args.count == 2 else { throw Unsupported("arguments") } |
| 1615 | 1731 | let ops: [String: Character] = ["eq": "=", "neq": "≠", "lt": "<", "gt": ">", "leq": "≤", "geq": "≥", "land": "&", "lor": "|"] |
| @@ -1722,7 +1838,8 @@ public enum Calc { |
| 1722 | 1838 | if x == .int(0) { return x } |
| 1723 | 1839 | _ = try sign(x) |
| 1724 | 1840 | return try extra(2) { |
| 1725 | | let e = try extra(2) { try expRaw(try float(x)) } |
| 1841 | let f = try float(x) |
| 1842 | let e = try extra(2) { try expRaw(f) } |
| 1726 | 1843 | let inverse = try divide(.int(1), e) |
| 1727 | 1844 | return try divide(try subtract(e, inverse), try add(e, inverse)) |
| 1728 | 1845 | } |
| @@ -1750,19 +1867,7 @@ public enum Calc { |
| 1750 | 1867 | guard args.count == 2 else { throw Unsupported("arguments") } |
| 1751 | 1868 | _ = (try sign(args[0]), try sign(args[1])) |
| 1752 | 1869 | return try extra(2) { |
| 1753 | | let (y, x) = (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 | | } |
| 1870 | let r = try arctan2Raw(try float(args[0]), try float(args[1])) |
| 1766 | 1871 | return modes.degrees ? try divide(r, try piOver180()) : r |
| 1767 | 1872 | } |
| 1768 | 1873 | case "deg", "rad": |