gpt4 book ai didi

arrays - Swift:执行被中断,原因:EXC_BAD_INSTRUCTION错误

转载 作者:行者123 更新时间:2023-11-30 12:58:56 25 4
gpt4 key购买 nike

当我在 Playground 中尝试执行此操作时:

func StockEvolution(S_0:Double, _ down:Double, _ up:Double, _ totalsteps:Int, _ upsteps:Int) -> Double // function being used in calcCall()
{
var S_t:Double = S_0 * pow(up, Double(upsteps)) * pow(down, Double(totalsteps - upsteps))
return S_t
}

func CallPayoff(S:Double, _ K:Double) -> Double // function being used in calcCall()
{
return max(S - K, 0.0)
}

func calcCall(S_0:Double, _ down:Double, _ up:Double, _ r:Double, _ steps:Int, _ K:Double) -> Double //calculate Call-Option
{
var prices = [Double]()
var q = 0.6 //risk-neutral probability factor

var i = 0
while i < steps
{
var payOff = CallPayoff(StockEvolution(S_0, down, up, steps, i), K)
prices.append(payOff)
i += 1
}

var n = steps - 1
while n >= 0
{
var j = 0
while j <= n
{
var value = (1 / r) * (prices[j + 1] * q + (1 - q) * prices[j])
prices.removeAtIndex(j)
prices.insert(value, atIndex: j)
j += 1
}
n -= 1
}
return prices[0]
}

通过这样做:

var checkPrice = calcCall(100, 0.6, 1.5, 1.05, 10, 200)

它给了我这个错误:

执行被中断,原因:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

我似乎无法在我的代码中找到错误。我尝试过使用不同的输入值,但错误仍然发生。

如果您能查看我的代码并帮助我解决此问题,那就太好了。感谢您的努力。

最佳答案

问题在于您的嵌套 while循环。第一次循环时,n设置为9 ,这意味着在最后一次通过嵌套循环时,您最终会得到 j == 9 ,这显然意味着 j + 1 == 10 。但你正在尝试获取 prices[j + 1] == prices[10] ,它不存在。因此发生了崩溃。

要查看这一点,请添加以下 if 语句:

if j + 1 < prices.count {
let value = (1 / r) * (prices[j + 1] * q + (1 - q) * prices[j])
prices.removeAtIndex(j)
prices.insert(value, atIndex: j)
}
j += 1

您将不会再收到错误访问错误。

现在,我根本不知道问题域,所以我无法说出为什么你的算法不正确。因此,我提供的答案可能无法为您提供您期望的值。您可能必须弄清楚为什么要尝试访问不存在的索引,以及如何解决该问题。

最后,请允许我介绍一些一般风格要点:

  • 您可能会注意到我交换了 var在上面的代码示例中 let 。自 value永远不会改变(而不是在每次循环时重新定义),它不需要是可变的。删除可变状态将使您更容易发现代码中的问题。
  • 您已经使用了大量具有可变状态的 while 循环来跟踪它们的进度。这些都可以替换为 for...in循环。例如。第一个 while 可以替换为 for i in 0..<steps对代码没有任何影响。这再次有助于减少可变状态和程序员错误。

我冒昧地重写了您的代码,使其更加“Swifty”,同时还删除了尽可能多的可变状态。这是:

func stockEvolution(s_0: Double, _ down: Double, _ up: Double, _ totalsteps: Int, _ upsteps: Int) -> Double {
let s_t: Double = s_0 * pow(up, Double(upsteps)) * pow(down, Double(totalsteps - upsteps))
return s_t
}

func callPayoff(s: Double, _ k: Double) -> Double {
return max(s - k, 0.0)
}

func calcCall(s_0: Double, _ down: Double, _ up: Double, _ r: Double, _ steps:Int, _ k: Double) -> Double {
var prices = Array(0..<steps).map { step in
return callPayoff(stockEvolution(s_0, down, up, steps, step), k)
}
let q = 0.6 //risk-neutral probability factor

for n in (0..<steps).reverse() {
for j in 0...n where j + 1 < prices.count {
let value = (1 / r) * (prices[j + 1] * q + (1 - q) * prices[j])
prices.removeAtIndex(j)
prices.insert(value, atIndex: j)
}
}
return prices[0]
}

let checkPrice = calcCall(100, 0.6, 1.5, 1.05, 10, 200)

关于arrays - Swift:执行被中断,原因:EXC_BAD_INSTRUCTION错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com