Commit 17afc0f1d0
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/Calc.swift +11
| @@ -1526,6 +1526,17 @@ public enum Calc { | ||
| 1526 | 1526 | func floorOf(_ v: Value) throws -> Value { |
| 1527 | 1527 | if case .int = v { return v } |
| 1528 | 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 | } | |
| 1529 | 1540 | guard let i = Int(exactly: try double(v).rounded(.down)) else { throw Unsupported("integer size") } |
| 1530 | 1541 | return .int(i) |
| 1531 | 1542 | } |
Tests/OrgCoreTests/CalcNumericTests.swift +8
| @@ -127,6 +127,14 @@ struct CalcNumericTests { | ||
| 127 | 127 | #expect(skipped == [], "p\(precision) \(skipped)") |
| 128 | 128 | } |
| 129 | 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 | } | |
| 130 | 138 | } |
| 131 | 139 | |
| 132 | 140 | /// Calc's `math-overflow` and `math-underflow` past an exponent of 4,000,000 leave the formula alone. |