"Unchecked exceptions" to the rescue? While we do not have them in the language / standard library (yet? ever?), here's a recent attempt to implement them manually.
Yeah, maybe not pow
specifically but abs
, negate
or something similar.
It's not hard to implement those missing bits
extension FixedWidthInteger {
mutating func negateReportingOverflow() -> /*overflow*/ Bool {
let result = negativeReportingOverflow
if result.overflow { return true }
self = result.partialValue
return false
}
var negativeReportingOverflow: (partialValue: Self, overflow: Bool) {
let result = Self.zero.subtractingReportingOverflow(self)
if result.overflow { return (self, true) }
return result
}
}
func absReportingOverflow<T: FixedWidthInteger>(_ x: T) -> (partialValue: T, overflow: Bool) {
if x >= 0 { return (x, false) }
return x.negativeReportingOverflow
}
though it would be better to have those available out of the box.