gpt4 book ai didi

swift - 尝试在 Swift Playground 中打印函数时出现 EXC_BAD_INSTRUCTION 错误

转载 作者:行者123 更新时间:2023-11-28 06:29:51 24 4
gpt4 key购买 nike

我目前正在从事一个大学数学项目,该项目要求我创建与抵押贷款相关的申请。我正在尝试编写 Swift 代码来计算每月支付的抵押贷款本金和利息。

我写的代码如下(值得一提的是,我是编码新手,对 Python 有一点经验,这些都是自学的,所以欢迎任何批评!):

// Declare all variables
// y = periodic interest rate
// n = time left of repayment period (months)

let y = [Float](arrayLiteral: 0.02/12,0.03/12,0.04/12,0.05/12)
let n = [Float](arrayLiteral: 240,180,120,60)

var x = [Float]()
var interest = [Float]()
var T_1 = [Int]()

// Create Array from 0 to 240
for a in 0...240 {
T_1.append(a)
}

// Create Array of all 240 interest values

for a in 0...239 {
if a < 60 {
interest.append(y[0])
var repay = n[0]
x.append(repay)
}
else if a < 120 {
interest.append(y[1])
var repay = n[1]
x.append(repay)
}
else if a < 180 {
interest.append(y[2])
var repay = n[2]
x.append(repay)
}
else {
interest.append(y[3])
var repay = n[3]
x.append(repay)
}
}
print("Interest array:",interest)

// Calculate Each months Principal paid and Interest Paid

func variablerate(P: inout Float) -> [Float] {
var principal = [Float]()
var i_paid = [Float]()
var repayments = [Float]()
var R = Float(0)
let temporaryP = P
i_paid.append(0)

for a in 0...240 {
if (a>0) && (a<240) && (interest[a-1] == interest[a]) {
let ip = P*(1+interest[a-1])-P
i_paid.append(i_paid[a-1] + ip)
P = (1+interest[a-1])*temporaryP
P = P-R
let h = interest[a]
R = (P*h)/(1-pow(1+h,Float(interest.count-a)))
repayments.append(R)
principal.append(round(100*P)/100)
}
else if a==240 || a==241 {
P = P - R
principal.append(round(100*P)/100)
let ip = P*(1+interest[a-1])-P
i_paid.append(i_paid[a-1] + ip)
let h = interest[a-1]
P = P*(1+h)
}
else {
P = P - R
let h = interest[a]
R = (P*h)/(1-pow(1+h,Float(interest.count-a)))
repayments.append(R)
principal.append(round(100*P)/100)
let ip = P*(1+interest[a])-P
i_paid.append(i_paid[a-1] + ip)
P = P*(1+h)
}
}
return i_paid
return principal

}
var z = Float(100000)

print(variablerate(P: &z)) //This is where the error appears

给出的错误是:

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0).

非常感谢有关此问题的任何帮助或指导!

最佳答案

崩溃的原因是索引超出范围

您正在获取 Index out of range 异常,因为在代码中您正在运行从 0 到 240 的循环,并且从局部变量 a 减去 1 所以在第一次迭代中 a将包含 0,从 0 中减去 1 将得到负索引。

这里是错误

对于 0...240 {
if (a>0) && (a<240) && (兴趣[a-1] == 兴趣[a])

下面的代码有修正

// Declare all variables
// y = periodic interest rate
// n = time left of repayment period (months)

let y = [Float](arrayLiteral: 0.02/12,0.03/12,0.04/12,0.05/12)
let n = [Float](arrayLiteral: 240,180,120,60)

var x = [Float]()
var interest = [Float]()
var T_1 = [Int]()

// Create Array from 0 to 240
for a in 0...240 {
T_1.append(a)
}

// Create Array of all 240 interest values

for a in 0...239 {
if a < 60 {
interest.append(y[0])
var repay = n[0]
x.append(repay)
}
else if a < 120 {
interest.append(y[1])
var repay = n[1]
x.append(repay)
}
else if a < 180 {
interest.append(y[2])
var repay = n[2]
x.append(repay)
}
else {
interest.append(y[3])
var repay = n[3]
x.append(repay)
}
}
print("Interest array:",interest)

// Calculate Each months Principal paid and Interest Paid

func variablerate(P: inout Float) -> [Float] {
var principal = [Float]()
var i_paid = [Float]()
var repayments = [Float]()
var R = Float(0)
let temporaryP = P
i_paid.append(0)

for a in 1...240 { // Code corrected now loop go from 1 to 240
if (a>0) && (a<240) && (interest[a-1] == interest[a]) {
let ip = P*(1+interest[a-1])-P
i_paid.append(i_paid[a-1] + ip)
P = (1+interest[a-1])*temporaryP
P = P-R
let h = interest[a]
R = (P*h)/(1-pow(1+h,Float(interest.count-a)))
repayments.append(R)
principal.append(round(100*P)/100)
}
else if a == 240 || a == 241 {
P = P - R
principal.append(round(100*P)/100)
let ip = P*(1+interest[a-1])-P
i_paid.append(i_paid[a-1] + ip)
let h = interest[a-1]
P = P*(1+h)
}
else {
P = P - R
let h = interest[a]
R = (P*h)/(1-pow(1+h,Float(interest.count-a)))
repayments.append(R)
principal.append(round(100*P)/100)
let ip = P*(1+interest[a])-P
i_paid.append(i_paid[a-1] + ip)
P = P*(1+h)
}
}
//return i_paid
return principal

}
var z = Float(100000)

print("\(variablerate(P: &z))")

关于swift - 尝试在 Swift Playground 中打印函数时出现 EXC_BAD_INSTRUCTION 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40637810/

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