| @@ -69,6 +69,7 @@ public enum Calc { |
| 69 | 69 | if numeric { |
| 70 | 70 | switch value { |
| 71 | 71 | case .int, .float, .frac: break |
| 72 | case .vector where isConstant(value): break |
| 72 | 73 | default: |
| 73 | 74 | // Calc explains the first argument a quotient, power or function |
| 74 | 75 | // rejected along the way, which isn't tracked here. |
| @@ -116,6 +117,8 @@ public enum Calc { |
| 116 | 117 | // Constants to 36 digits, rounded to the precision, as `math-defcache` gives them. |
| 117 | 118 | static func pi() throws -> Value { try makeFloat(314159265358979323846264338327950288, -35) } |
| 118 | 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 | 122 | static func piOver180() throws -> Value { try makeFloat(174532925199432957692369076848861271, -37) } |
| 120 | 123 | |
| 121 | 124 | /// `math-float`. |
| @@ -219,6 +222,9 @@ public enum Calc { |
| 219 | 222 | m /= 10 |
| 220 | 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 | 228 | guard let i = Int(exactly: m) else { throw Unsupported("calc precision") } |
| 223 | 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 | 286 | static func isSymbolic(_ vs: Value...) -> Bool { |
| 294 | 287 | vs.contains { if case .symbolic = $0 { true } else { false } } |
| 295 | 288 | } |
| @@ -324,10 +317,22 @@ public enum Calc { |
| 324 | 317 | let ediff = e1 - e2 |
| 325 | 318 | if ediff >= 0 { |
| 326 | 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 | 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 | 338 | static func negate(_ a: Value) throws -> Value { |
| @@ -507,7 +512,22 @@ public enum Calc { |
| 507 | 512 | static func squareRootRaw(_ a: Value) throws -> Value { |
| 508 | 513 | if isZero(a) { return a } |
| 509 | 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 | 533 | /// `math-exp-raw` of a float. |
| @@ -525,7 +545,100 @@ public enum Calc { |
| 525 | 545 | /// `math-arctan-raw` of a float. |
| 526 | 546 | static func arctanRaw(_ x: Value) throws -> Value { |
| 527 | 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 | 644 | static func ln2() throws -> Value { try makeFloat(693147180559945309417232121458176568, -36) } |
| @@ -636,10 +749,11 @@ public enum Calc { |
| 636 | 749 | return (pow == x, sum) |
| 637 | 750 | } |
| 638 | 751 | |
| 639 | | /// `math-round` in `math-rounding-mode` `even` or `up`. |
| 640 | | static func round(_ a: Value, even: Bool) throws -> Value { |
| 752 | /// `math-round` in `math-rounding-mode` `even` or `up`, or with `away` the default mode, |
| 753 | /// which rounds halves away from zero. |
| 754 | static func round(_ a: Value, even: Bool, away: Bool = false) throws -> Value { |
| 641 | 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 | 757 | let b = try add(a, rational(a) != nil ? .frac(1, 2) : .float(5, -1)) |
| 644 | 758 | var f = try floorInt(b) |
| 645 | 759 | if even, isWhole(b), f % 2 != 0 { f -= 1 } |
| @@ -737,12 +851,23 @@ public enum Calc { |
| 737 | 851 | |
| 738 | 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 | 863 | /// `math-format-number`. |
| 741 | 864 | static func format(_ value: Value, _ floatFormat: FloatFormat) throws -> String { |
| 742 | 865 | switch value { |
| 743 | 866 | case .int(let i): return String(i) |
| 744 | 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 | 871 | case .symbolic(let t): return try Symbolic.format(t, floatFormat) |
| 747 | 872 | case .nan: return "nan" |
| 748 | 873 | case .date(let d): return "<" + (try formatDate(d)) + ">" |
| @@ -759,7 +884,8 @@ public enum Calc { |
| 759 | 884 | case .sci(let n): (figs, kind) = (n, "sci") |
| 760 | 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 | 889 | if figs < 0 { figs = -figs } |
| 764 | 890 | mant = try scaleRounding(mant, exp + figs) |
| 765 | 891 | var str = String(mant) |
| @@ -1379,12 +1505,11 @@ public enum Calc { |
| 1379 | 1505 | |
| 1380 | 1506 | /// `math-mod`: the remainder with the divisor's sign. |
| 1381 | 1507 | func mod(_ a: Value, _ b: Value) throws -> Value { |
| 1382 | | if case .int(let x) = a, case .int(let y) = b { |
| 1383 | | guard y != 0 else { throw Unsupported("division by zero") } |
| 1384 | | let r = x % y |
| 1385 | | return .int(r != 0 && (r < 0) != (y < 0) ? r + y : r) |
| 1386 | | } |
| 1387 | | let q = try floorOf(try divide(a, b)) |
| 1508 | if isZero(a) { return a } |
| 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)) |
| 1388 | 1513 | return try subtract(a, try multiply(b, q)) |
| 1389 | 1514 | } |
| 1390 | 1515 | |
| @@ -1400,6 +1525,18 @@ public enum Calc { |
| 1400 | 1525 | |
| 1401 | 1526 | func floorOf(_ v: Value) throws -> Value { |
| 1402 | 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 | 1540 | guard let i = Int(exactly: try double(v).rounded(.down)) else { throw Unsupported("integer size") } |
| 1404 | 1541 | return .int(i) |
| 1405 | 1542 | } |
| @@ -1456,8 +1593,6 @@ public enum Calc { |
| 1456 | 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 | 1597 | func call(_ name: String, _ args: [Value]) throws -> Value { |
| 1463 | 1598 | switch name { |
| @@ -1490,7 +1625,7 @@ public enum Calc { |
| 1490 | 1625 | if items.contains(where: { if case .date = $0 { true } else { false } }) { throw Unsupported("arguments") } |
| 1491 | 1626 | try items.sort { try compare($0, $1) < 0 } |
| 1492 | 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 | 1629 | case "if": |
| 1495 | 1630 | guard args.count == 3 else { throw Unsupported("arguments") } |
| 1496 | 1631 | return try truth(args[0]) ? args[1] : args[2] |
| @@ -1542,12 +1677,15 @@ public enum Calc { |
| 1542 | 1677 | return .date(try call(name, [d])) |
| 1543 | 1678 | } |
| 1544 | 1679 | var x = args[0] |
| 1545 | | if case .frac(let n, let d) = x { x = try divide(.int(n), .int(d)) } |
| 1546 | 1680 | if args.count == 2 { |
| 1547 | | guard case .int(let places) = args[1] else { throw Unsupported("arguments") } |
| 1548 | | guard case .float(let m, let e) = x else { return x } |
| 1549 | | return try makeFloat(try scaleRounding(Int128(m), e + places), -places) |
| 1681 | // As `rounde`: scaled to an integer with fractions, then scaled back. |
| 1682 | guard isWhole(args[1]), case let places = try trunc(args[1]) else { throw Unsupported("arguments") } |
| 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 | 1689 | guard case .float(let m, let e) = x else { return x } |
| 1552 | 1690 | guard let i = Int(exactly: try scaleRounding(Int128(m), e)) else { throw Unsupported("integer size") } |
| 1553 | 1691 | return .int(i) |
| @@ -1583,33 +1721,43 @@ public enum Calc { |
| 1583 | 1721 | } |
| 1584 | 1722 | case "sin", "cos", "tan": |
| 1585 | 1723 | let x = try one(args) |
| 1586 | | if modes.degrees, case .int(let i) = x, i % 90 == 0 { |
| 1587 | | let quarter = ((i / 90) % 4 + 4) % 4 |
| 1588 | | switch name { |
| 1589 | | case "sin": return .int([0, 1, 0, -1][quarter]) |
| 1590 | | 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) |
| 1724 | if x == .nan { return x } |
| 1725 | if case .int(let i) = x, modes.degrees ? i % (name == "tan" ? 180 : 90) == 0 : i == 0 { |
| 1726 | if name == "tan" { return .int(0) } |
| 1727 | // `(aref [0 1 0 -1] (math-mod (/ x 90) 4))`: a float quotient in math-mod can put |
| 1728 | // the index out of range, and Calc then leaves the call alone. |
| 1729 | guard case .int(let quarter) = try mod(.int(i / 90), .int(4)), (0...3).contains(quarter) else { |
| 1730 | throw Unsupported("number size") |
| 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 | 1744 | case "arcsin", "arccos", "arctan": |
| 1600 | 1745 | let x = try one(args) |
| 1601 | 1746 | if modes.degrees, case .int(let i) = x { |
| 1602 | 1747 | 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) |
| 1748 | case ("arcsin", -1), ("arcsin", 1): return .int(90 * i) |
| 1749 | case ("arccos", -1), ("arccos", 0): return .int(90 - 90 * i) |
| 1750 | case ("arctan", -1), ("arctan", 1): return .int(45 * i) |
| 1606 | 1751 | default: break |
| 1607 | 1752 | } |
| 1608 | 1753 | } |
| 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)) |
| 1754 | if x == .int(name == "arccos" ? 1 : 0) { return .int(0) } |
| 1755 | if x == .nan { return x } |
| 1756 | return try extra(2) { |
| 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 | 1761 | case "eq", "neq", "lt", "gt", "leq", "geq", "land", "lor": |
| 1614 | 1762 | guard args.count == 2 else { throw Unsupported("arguments") } |
| 1615 | 1763 | let ops: [String: Character] = ["eq": "=", "neq": "≠", "lt": "<", "gt": ">", "leq": "≤", "geq": "≥", "land": "&", "lor": "|"] |
| @@ -1722,7 +1870,8 @@ public enum Calc { |
| 1722 | 1870 | if x == .int(0) { return x } |
| 1723 | 1871 | _ = try sign(x) |
| 1724 | 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 | 1875 | let inverse = try divide(.int(1), e) |
| 1727 | 1876 | return try divide(try subtract(e, inverse), try add(e, inverse)) |
| 1728 | 1877 | } |
| @@ -1750,19 +1899,7 @@ public enum Calc { |
| 1750 | 1899 | guard args.count == 2 else { throw Unsupported("arguments") } |
| 1751 | 1900 | _ = (try sign(args[0]), try sign(args[1])) |
| 1752 | 1901 | 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 | | } |
| 1902 | let r = try arctan2Raw(try float(args[0]), try float(args[1])) |
| 1766 | 1903 | return modes.degrees ? try divide(r, try piOver180()) : r |
| 1767 | 1904 | } |
| 1768 | 1905 | case "deg", "rad": |
| @@ -1774,6 +1911,8 @@ public enum Calc { |
| 1774 | 1911 | case "vgmean", "vhmean": |
| 1775 | 1912 | let items = try elements(args) |
| 1776 | 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 | 1916 | return try extra(2) { |
| 1778 | 1917 | if name == "vhmean" { |
| 1779 | 1918 | let inverses = try items.map { try divide(.int(1), $0, fractions: modes.preferFractions) } |