Commit 17afc0f1d0

17afc0f1d0d19beccdd2b861b4ef096bbb32c1d1

parent: 64e174a198

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 20:11 UTC

Floor Calc floats exactly in mod and idiv

Layout: unified · split

Sources/OrgCore/Compute/Calc.swift +11
@@ -1526,6 +1526,17 @@ public enum Calc {
15261526 func floorOf(_ v: Value) throws -> Value {
15271527 if case .int = v { return v }
15281528 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 }
15291540 guard let i = Int(exactly: try double(v).rounded(.down)) else { throw Unsupported("integer size") }
15301541 return .int(i)
15311542 }
Tests/OrgCoreTests/CalcNumericTests.swift +8
@@ -127,6 +127,14 @@ struct CalcNumericTests {
127127 #expect(skipped == [], "p\(precision) \(skipped)")
128128 }
129129 }
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 }
130138 }
131139
132140 /// Calc's `math-overflow` and `math-underflow` past an exponent of 4,000,000 leave the formula alone.