Commit a77399700c
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/Calc.swift +15 −8
| @@ -746,10 +746,11 @@ public enum Calc { | ||
| 746 | 746 | return (pow == x, sum) |
| 747 | 747 | } |
| 748 | 748 | |
| 749 | /// `math-round` in `math-rounding-mode` `even` or `up`. | |
| 750 | static func round(_ a: Value, even: Bool) throws -> Value { | |
| 749 | /// `math-round` in `math-rounding-mode` `even` or `up`, or with `away` the default mode, | |
| 750 | /// which rounds halves away from zero. | |
| 751 | static func round(_ a: Value, even: Bool, away: Bool = false) throws -> Value { | |
| 751 | 752 | if isWhole(a) { return .int(try trunc(a)) } |
| 752 | if try sign(a) < 0, even { return try negate(try round(try negate(a), even: even)) } | |
| 753 | if try sign(a) < 0, even || away { return try negate(try round(try negate(a), even: even)) } | |
| 753 | 754 | let b = try add(a, rational(a) != nil ? .frac(1, 2) : .float(5, -1)) |
| 754 | 755 | var f = try floorInt(b) |
| 755 | 756 | if even, isWhole(b), f % 2 != 0 { f -= 1 } |
| @@ -1501,6 +1502,7 @@ public enum Calc { | ||
| 1501 | 1502 | |
| 1502 | 1503 | /// `math-mod`: the remainder with the divisor's sign. |
| 1503 | 1504 | func mod(_ a: Value, _ b: Value) throws -> Value { |
| 1505 | if isZero(a) { return a } | |
| 1504 | 1506 | if case .int(let x) = a, case .int(let y) = b { |
| 1505 | 1507 | guard y != 0 else { throw Unsupported("division by zero") } |
| 1506 | 1508 | let r = x % y |
| @@ -1610,7 +1612,7 @@ public enum Calc { | ||
| 1610 | 1612 | if items.contains(where: { if case .date = $0 { true } else { false } }) { throw Unsupported("arguments") } |
| 1611 | 1613 | try items.sort { try compare($0, $1) < 0 } |
| 1612 | 1614 | if items.count % 2 == 1 { return items[items.count / 2] } |
| 1613 | return try divide(try add(items[items.count / 2 - 1], items[items.count / 2]), .int(2)) | |
| 1615 | return try divide(try add(items[items.count / 2 - 1], items[items.count / 2]), .int(2), fractions: modes.preferFractions) | |
| 1614 | 1616 | case "if": |
| 1615 | 1617 | guard args.count == 3 else { throw Unsupported("arguments") } |
| 1616 | 1618 | return try truth(args[0]) ? args[1] : args[2] |
| @@ -1662,12 +1664,15 @@ public enum Calc { | ||
| 1662 | 1664 | return .date(try call(name, [d])) |
| 1663 | 1665 | } |
| 1664 | 1666 | var x = args[0] |
| 1665 | if case .frac(let n, let d) = x { x = try divide(.int(n), .int(d)) } | |
| 1666 | 1667 | if args.count == 2 { |
| 1667 | guard case .int(let places) = args[1] else { throw Unsupported("arguments") } | |
| 1668 | guard case .float(let m, let e) = x else { return x } | |
| 1669 | return try makeFloat(try scaleRounding(Int128(m), e + places), -places) | |
| 1668 | // As `rounde`: scaled to an integer with fractions, then scaled back. | |
| 1669 | guard isWhole(args[1]), case let places = try trunc(args[1]) else { throw Unsupported("arguments") } | |
| 1670 | _ = try sign(x) | |
| 1671 | if places <= 0, case .int = x { return x } | |
| 1672 | let rounded = try round(try scale(x, places, fractions: true), even: false, away: true) | |
| 1673 | return try scale(rounded, -places, fractions: modes.preferFractions) | |
| 1670 | 1674 | } |
| 1675 | if case .frac(let n, let d) = x { x = try divide(.int(n), .int(d)) } | |
| 1671 | 1676 | guard case .float(let m, let e) = x else { return x } |
| 1672 | 1677 | guard let i = Int(exactly: try scaleRounding(Int128(m), e)) else { throw Unsupported("integer size") } |
| 1673 | 1678 | return .int(i) |
| @@ -1892,6 +1897,8 @@ public enum Calc { | ||
| 1892 | 1897 | case "vgmean", "vhmean": |
| 1893 | 1898 | let items = try elements(args) |
| 1894 | 1899 | guard !items.isEmpty else { throw Unsupported("empty vector") } |
| 1900 | // Calc's root of one float, a power of 1:1, stays unevaluated. | |
| 1901 | if name == "vgmean", items.count == 1, case .float = items[0] { throw Unsupported("arguments") } | |
| 1895 | 1902 | return try extra(2) { |
| 1896 | 1903 | if name == "vhmean" { |
| 1897 | 1904 | let inverses = try items.map { try divide(.int(1), $0, fractions: modes.preferFractions) } |
Tests/OrgCoreTests/CalcNumericTests.swift +31 −1
| @@ -86,8 +86,38 @@ struct CalcNumericTests { | ||
| 86 | 86 | #expect(skipped == [], "\(skipped)") |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | /// Vector results, with and without `N`, and zero with no decimal places. | |
| 89 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | |
| 90 | func fractionModeMatchesCalc() throws { | |
| 91 | var generator = SystemRandomNumberGenerator() | |
| 92 | 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", | |
| 93 | "100", "0.123456789", "0."] | |
| 94 | func number() -> String { numbers.randomElement(using: &generator)! } | |
| 95 | func vector() -> String { "[" + (0..<Int.random(in: 1...5, using: &generator)).map { _ in number() }.joined(separator: ",") + "]" } | |
| 96 | let unary = ["fact", "dfact", "abs", "floor", "ceil", "trunc", "round", "sqrt", "exp", "ln", "log10", "sin", "cos", "tan", "arcsin", | |
| 97 | "arccos", "arctan", "lnot", "percent", "float", "pfloat", "sign", "rounde", "roundu", "frac", "exp10", "sinh", "cosh", | |
| 98 | "tanh", "arcsinh", "arccosh", "arctanh", "deg", "rad"] | |
| 99 | let binary = ["mod", "idiv", "max", "min", "round", "rounde", "roundu", "fdiv", "gcd", "lcm", "choose", "perm", "pow", "nroot", | |
| 100 | "hypot", "log", "arctan2", "eq", "lt", "land", "lor", "agmean"] | |
| 101 | let statistics = ["vsum", "vprod", "vcount", "vmean", "vmax", "vmin", "vmedian", "vvar", "vpvar", "vsdev", "vpsdev", "vflat", "vgmean", "vhmean"] | |
| 102 | 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", | |
| 103 | "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])", | |
| 104 | "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)") } | |
| 105 | for _ in 0..<1500 { | |
| 106 | switch Int.random(in: 0..<4, using: &generator) { | |
| 107 | case 0: cases.append(("\(unary.randomElement(using: &generator)!)(\(number()))", "(float 12)")) | |
| 108 | case 1: cases.append(("\(binary.randomElement(using: &generator)!)(\(number()),\(number()))", "(float 12)")) | |
| 109 | case 2: cases.append(("\(statistics.randomElement(using: &generator)!)(\(vector()))", "(float 12)")) | |
| 110 | default: | |
| 111 | let op = ["+", "-", "*", "/", "^", "%", "\\"].randomElement(using: &generator)! | |
| 112 | cases.append(("(\(number()))\(op)(\(number()))", "(float 12)")) | |
| 113 | } | |
| 114 | } | |
| 115 | for fractions in [true, false] { _ = try CalcTests.compare(cases, fractions: fractions) } | |
| 116 | } | |
| 117 | ||
| 118 | /// Vector results, with and without `N`, zero with no decimal places, and fractions. | |
| 90 | 119 | static let tables = [ |
| 120 | "| 1 | 2 | | |\n|---+---+---+---|\n| 2.5 | 0.25 | | |\n#+TBLFM: $3=vmedian($1..$2);F::$4=round($1,1);F\n", | |
| 91 | 121 | "| 1 | 2 | |\n|---+---+---|\n| 3 | x | |\n| 1.5 | 0. | |\n#+TBLFM: $3=$1..$2;N\n", |
| 92 | 122 | "| 1 | 2 | |\n|---+---+---|\n| 1.5 | 0. | |\n| 1:3 | -2 | |\n#+TBLFM: $3=$1..$2\n", |
| 93 | 123 | "| 1.5 | 0 | |\n|---+---+---|\n| 2 | 4 | |\n#+TBLFM: $3=$1..$2;Nf0\n", |