Commit 3fb1e1409c
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/CalcSymbolic.swift +41 −3
| @@ -220,12 +220,50 @@ extension Calc { | |||
| 220 | 220 | ||
| 221 | static func knownNonneg(_ t: Term) throws -> Bool { [2, 4, 6].contains(try signs(t)) } | 221 | static func knownNonneg(_ t: Term) throws -> Bool { [2, 4, 6].contains(try signs(t)) } |
| 222 | 222 | ||
| 223 | /// `math-known-num-integerp` and `math-known-integerp`: only numbers are known to be | 223 | /// `math-known-num-integerp`. |
| 224 | /// integers, as nothing is declared. | ||
| 225 | static func knownNumInteger(_ t: Term) throws -> Bool { | 224 | static func knownNumInteger(_ t: Term) throws -> Bool { |
| 226 | if let v = number(t) { return isWhole(v) } | 225 | if let v = number(t) { return isWhole(v) } |
| 227 | guard !containsConstant(t) else { throw Unsupported("symbolic arithmetic") } | 226 | guard !containsConstant(t) else { throw Unsupported("symbolic arithmetic") } |
| 228 | return false | 227 | return try possibleTypes(t, num: true) <= 3 |
| 228 | } | ||
| 229 | |||
| 230 | /// `math-possible-types`: the bits 1 integer, 2 integer-valued float, 4 fraction and | ||
| 231 | /// 8 float, with `num` merging 1 and 2 and merging 4 and 8. Nothing is declared, so a | ||
| 232 | /// variable may be anything (63). Results above 15 are all 63 here: only whether a | ||
| 233 | /// result is at most 3 is used, and no larger operand can make it so. | ||
| 234 | static func possibleTypes(_ a: Term, num: Bool = false) throws -> Int { | ||
| 235 | func merge(_ t1: Int, _ t2: Int, _ mask: Int) -> Int { | ||
| 236 | if num { return mask * 3 } | ||
| 237 | return (t1 & 5 > 0 && t2 & 5 > 0 ? mask : 0) | ((t1 | t2) & 10 > 0 ? mask * 2 : 0) | ||
| 238 | } | ||
| 239 | switch a { | ||
| 240 | case .number(.int): return num ? 3 : 1 | ||
| 241 | case .number(.float(_, let e)) where e >= 0: return num ? 3 : 2 | ||
| 242 | case .number(.frac): return num ? 12 : 4 | ||
| 243 | case .number(.float): return num ? 12 : 8 | ||
| 244 | case .number: throw Unsupported("symbolic arithmetic") | ||
| 245 | case .variable: return 63 | ||
| 246 | case .op("neg", let args): return try possibleTypes(args[0]) | ||
| 247 | case .op(let f, let args) where args.count == 2 && ["+", "-", "*", "/", "^"].contains(f): | ||
| 248 | let (t1, t2) = (try possibleTypes(args[0], num: num), try possibleTypes(args[1], num: num)) | ||
| 249 | let t12 = t1 | t2 | ||
| 250 | if f == "^" { | ||
| 251 | guard t2 <= 3, try knownNonneg(args[1]), t1 < 16 else { | ||
| 252 | return try knownNonneg(args[0]) && signs(args[1]) == 4 ? 15 : 63 | ||
| 253 | } | ||
| 254 | let mask = (t1 & 3 > 0 ? 1 : 0) | (t1 & 4) | (t1 & 12 > 0 ? 5 : 0) | ||
| 255 | return merge(t1, t2, mask) | ||
| 256 | } | ||
| 257 | guard t12 < 16 else { return 63 } | ||
| 258 | if f == "/" { | ||
| 259 | if t12 & 10 > 0 { return 10 } | ||
| 260 | return t1 == 4 || t2 == 4 || fractions ? 5 : 15 | ||
| 261 | } | ||
| 262 | let mask = t12 <= 3 ? 1 | ||
| 263 | : ((t1 <= 3 && t2 & 3 == 0) || (t2 <= 3 && t1 & 3 == 0)) && f != "*" ? 4 : 5 | ||
| 264 | return merge(t1, t2, mask) | ||
| 265 | default: throw Unsupported("function \(head(a) ?? "")") | ||
| 266 | } | ||
| 229 | } | 267 | } |
| 230 | 268 | ||
| 231 | // MARK: Arithmetic | 269 | // MARK: Arithmetic |