JuJoDi
JuJoDi

Reputation: 14975

Could not find an overload for '^' that accepts the supplied arguments

func squareArea(side:Double) -> Double {
    return side ^ 2
}

I get the error:

could not find an overload for '^' that accepts the supplied arguments

I've also tried

func squareArea(side:Double) -> Double {
    return Double(side ^ 2)
}

and

func squareArea(side:Double) -> Double {
    return side ^ Double(2)
}

but

func squareArea(side:Double) -> Double {
    return side * side
}

works fine. What is the correct syntax?

Upvotes: 2

Views: 403

Answers (2)

Anthony Benavente
Anthony Benavente

Reputation: 330

^ Is the XOR operator not the "power" operator. See here and search "XOR".

The bitwise XOR operator, or “exclusive OR operator” (^), compares the bits of two numbers. The operator returns a new number whose bits are set to 1 where the input bits are different and are set to 0 where the input bits are the same:

let firstBits: UInt8 = 0b00010100
let otherBits: UInt8 = 0b00000101
let outputBits = firstBits ^ otherBits 

Upvotes: 2

Oscar Swanros
Oscar Swanros

Reputation: 19969

There's a function for that:

func squareArea(side:Double) -> Double {
    return pow(side, 2)
}

Upvotes: 2

Related Questions