Abraham Hamidi
Abraham Hamidi

Reputation: 13789

"Binary operator > cannot be applied to two Float operands" error

I have a conditional that looks like:

if sqrtf(powf(startTouch.x - (touches.first as! UITouch).locationInView(self.view).y, 2) + powf(startTouch.y-(touches.first as! UITouch).locationInView(self.view).y, 2)) > dragThreshold as! Float {
        self.drag = false;
}

I am getting an error that says Binary operator > cannot be applied to two Float operands. I cannot understand why I can't check if a float is greater than another float. What is this error trying to tell me?

Upvotes: 1

Views: 3076

Answers (2)

fqdn
fqdn

Reputation: 2843

The members of a CGPoint are of type CGFloat, and assuming you are on a 64-bit architecture the native type used to store a CGFloat is Double - try treating those calculated values as Doubles (use sqrt() and pow() instead of the Float versions)... using dragThreshold as a Double as well

Upvotes: 2

KleMiX
KleMiX

Reputation: 332

Using latest Xcode 7 beta 4 and SpriteKit's function override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) I did break this big chunk to smaller ones like this:

let dragThreshold: Int = 1
let startTouch = CGPointZero
let pow1 = powf(Float(startTouch.x - touches!.first!.locationInView(self.view).y), 2.0)
let pow2 = powf(Float(startTouch.y-touches!.first!.locationInView(self.view).y), 2.0)
let sq = sqrtf(pow1 + pow2)

if sq > Float(dragThreshold) {
    self.drag = false;
}

This works. Basically added more conversions for powf arguments, changed 2 to 2.0

General advice - if you get some strange errors, try to break down your expression into smaller ones to isolate issue.

Upvotes: 1

Related Questions