首先,尽量不要把我撕成碎片:)我对编程总的来说比较新,这是我在StackOverflow上的第一个问题!
这是一个简单的问题,如何在下面给出的函数之外返回“var m = ...”行?我正在尝试实现Bisection数值方法(见于rayweiderlich.com),但是对于不同的方程式。我试图访问函数外部的变量,以更新viewcontroller上的标签。注意:我试图解决的迭代变量确实正确输出,该数字是0.8789。因此,我认为算法是正确的。这是教程链接https://www.raywenderlich.com/99559/numeric-algorithms-using-playgrounds和我的教程代码版本:
//: Playground - noun: a place where people can play
import UIKit
func bisection(x: Double) -> Double {
var lower = 0.0
var upper = x
let constant1 = Double((1+2*60))
let constant2 = Double(1+4*60)
var m = (constant1 - pow(constant2, 0.5)) / (2.0*60.0) //This returns the number I want (0.8789)
let epsilon = 1e-3
while (fabs(m * m - x) > epsilon) {
m = (lower + upper)/2
if m * m > x {
upper = m
print(m)
} else {
lower = m
print(m)
}
}
print(m)
return m //This returns the number I DON'T want (0.9995)
}
let bis = bisection(x: 1.0)
//somewhere around here is where I would like to use the "m" variable to update a label in a view controller
答案 0 :(得分:-1)
PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[35m\]$MSYSTEM\[\e[0m\] \[\e[33m\]\w\[\e[0m\]\n\$ '
这是你想要的吗?