How do I make my exponentiation operator work with all numeric types in Swift?
I have created a ^^ operator in Swift. How do I make it work with Integers and Doubles like all other operators?
infix operator ^^ { }
func ^^ (number:Int, power: Int) -> Int {
var result = power > 0 ? number : 0
if power > 1 { for x in 1..<power { result *= number } }
return result
}
Solution 1:
Here is a more type-safe implementation, using operator overloading. For integer types,
you can define ^^
as
infix operator ^^ { associativity left precedence 170 }
func ^^<T : IntegerType, U : IntegerType> (base: T, var power: U) -> T {
if power < 0 { return 0 }
var result : T = 1
var square : T = base
if power > 0 {
if power % 2 == 1 { result *= square }
power /= 2
}
while power > 0 {
square *= square
if power % 2 == 1 { result *= square }
power /= 2
}
return result
}
(I have chosen a more efficient variant called "exponentiation by repeated squaring and multiplication".)
For floating point types, define a protocol the covers all types that can be
converted from and to Double
:
protocol DoubleConvertible {
init(_ value: Double)
var doubleValue : Double { get }
}
and make Float
, Double
and CGFloat
conform to that protocol:
extension Double : DoubleConvertible {
var doubleValue : Double { return self }
}
extension Float : DoubleConvertible {
var doubleValue : Double { return Double(self) }
}
extension CGFloat : DoubleConvertible {
var doubleValue : Double { return Double(self) }
}
Now the floating point exponentiation can simply be defined as
func ^^<T : DoubleConvertible, U:DoubleConvertible> (base: T, power: U) -> T {
return T(pow(base.doubleValue, power.doubleValue))
}
Examples:
let x1 = 2^^3 // Int
let x2 = UInt64(2)^^3 // UInt64
let x3 = 2.0 ^^ 3 // Double
let x4 = Float(2.0) ^^ 3 // Float
let x5 = "a" ^^ "b" // Compiler error
Update for Swift 4:
IntegerType
has become BinaryInteger
(SE-0104 Protocol-oriented integers) and
the syntax for declaring operators has changed
(SE-0077 Improved operator declarations.)
Here is an implementation for all integer and floating point
bases with Int
exponents:
precedencegroup ExponentiationPrecedence { associativity: right higherThan: MultiplicationPrecedence }
infix operator ^^: ExponentiationPrecedence
func ^^<T : BinaryInteger>(base: T, power: Int) -> T {
if power < 0 { return 0 }
var power = power
var result: T = 1
var square = base
if power > 0 {
if power % 2 == 1 { result *= square }
power /= 2
}
while power > 0 {
square *= square
if power % 2 == 1 { result *= square }
power /= 2
}
return result
}
func ^^(base: Float, power: Int) -> Float {
return pow(base, Float(power))
}
func ^^(base: Double, power: Int) -> Double {
return pow(base, Double(power))
}
func ^^(base: CGFloat, power: Int) -> CGFloat {
return pow(base, CGFloat(power))
}
Examples:
let x1 = 2^^3 // Int
let x2 = UInt64(2)^^3 // UInt64
let x3 = 2.0 ^^ -3 // Double
let x4 = Float(2.0) ^^ 3 // Float
// let x6 = "a" ^^ 5 // Compiler error
See SE-0104 Protocol-oriented integers about the new protocol hierarchy introduced in Swift 4.