我试图从pr找到多项式的值.这是我的代码: 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 = C
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
没有太多要解释的,它只是抛出编译器的错误:“找不到’ – ‘接受提供的参数的重载.此外,我尝试在各处使用浮点类型的powf(x,2). pr是函数CGFloat类型参数.这是截图:.
谢谢!
这似乎是一个编译器错误(它可能值得在Apple的错误报告).报告导航器中的编译器消息显示
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 ) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
作为解决方法,您可以首先将表达式分配给临时变量:
let tmp = 4.8 * x4 - 10.4 * x3 + 5.7 * x2 + 1.05 * x + 0.95 let r = CGFloat( tmp )
备注:在给定点评估多项式的更有效方法
是Horner’s method:
let tmp = (((4.8 * x - 10.4) * x + 5.7) * x + 1.05) * x + 0.95
你不必计算x的各种权力.