Alfred Zien
Alfred Zien

Reputation: 1044

Could not find overload for -, although all arguments are double

I try to find value of polynomial from pr. Here is my code:

let x = Double(pr)
let x2 : Double = pow(x, 2.0)
let x3 : Double = pow(x, 3.0)
let x4 : Double = pow(x, 4.0)
let r = CGFloat( 4.8 * x4 - 10.4 * x3 + 5.7 * x2 + 1.05 * x + 0.95 )
let g = CGFloat( 4.8 * x4 - 8.8 * x3  + 3.3 * x2 + 1.65 * x + 0.0 )
let b = CGFloat(0.0)
let color = UIColor(red: r, green: g, blue: b, alpha: 1.0)
return color.CGColor

There is not so much to explain, it just throws complier’s error: “Could not find overload for ‘-‘ that accepts supplied arguments. Also I tried to use powf(x, 2) with float types everywhere. pr is function CGFloat type parameter. Here is screenshot: screenshot.

Thanks!

Upvotes: 4

Views: 121

Answers (1)

Martin R
Martin R

Reputation: 539765

This seems to be a compiler bug (and it might be worth a bug report at Apple). The compiler messages in the Report navigator show

note: expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
let g = CGFloat( 4.8 * x4 - 8.8 * x3  + 3.3 * x2 + 1.65 * x + 0.0 )
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As a workaround, you can assign the expression to a temporary variable first:

let tmp = 4.8 * x4 - 10.4 * x3 + 5.7 * x2 + 1.05 * x + 0.95
let r = CGFloat( tmp )

Remark: A more efficient method to evaluate a polynomial at a given point is Horner's method:

let tmp = (((4.8 * x - 10.4) * x + 5.7) * x + 1.05) * x + 0.95

where you don't have to compute the various powers of x.

Upvotes: 5

Related Questions