Search code examples
iosswiftmultiplication

Binary operator '*' cannot be applied to operands of type 'Double' and 'Int'


I tried some solutions on this website but could not come up with one.

I keep getting this error with this line of code:

func futureValue() {
    let interest = Double(interestInput.text!) ?? 0
    let pv = Double(pvInput.text!) ?? 0
    let years = Double(yearsInput.text!) ?? 0
        
    let result = pv * (1.0 + interest) ^^ years
}

The result variable is where I receive the error. What am I doing wrong?


Solution

  • To answer this question, the custom ^^ operator was set for Int and not Double.

    This is incorrect:

    }
    
    precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence }
    infix operator ^^ : PowerPrecedence
    func ^^ (radix: Int, power: Int) -> Int {
        return Int(pow(Double(radix), Double(power)))
    

    This is the correct way, to match the Double in the futureValue() function:

    }
    
    precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence }
    infix operator ^^ : PowerPrecedence
    func ^^ (radix: Double, power: Double) -> Double {
        return Double(pow(Double(radix), Double(power)))
    }