我如何调整我的 Swift 代码来给出二次方程的假想解?
How can I adapt my Swift code to give imaginary solutions to a quadratic?
我在 Swift 中创建了一个函数来求解并给出二次函数的解。我不知道如何调整我的功能,以便它给出假想的解决方案而不是打印,"There are no real solutions."
我对编程还比较陌生,需要一些帮助。这是我的代码:
func quadraticFormulaSolver(variableA a: Double, variableB b: Double, variableC c: Double) -> (Double, Double) {
let firstSolution: Double = (-b + sqrt((b * b) + (-4.0 * a * c))) / 2.0
let secondSolution: Double = (-b - sqrt((b * b) + (-4.0 * a * c))) / 2.0
let checkSolution: Double = sqrt((b * b) + (-4.0 * a * c))
if checkSolution > 0 {
print("There are two real solutions and they are \(firstSolution) and \(secondSolution)")
return(firstSolution, secondSolution) }
guard firstSolution != 0.0 else {
print("There is one real solution and it is \(firstSolution)")
return(firstSolution, secondSolution) }
guard checkSolution < 0 else {
print("There are no real solutions")
return(firstSolution, secondSolution) }
return(firstSolution, secondSolution)
}
由于您的函数可以 return 几个不同的选择,让我们做一个 Enum
来表示选项:
enum QuadraticSolution {
case TwoReal(firstSolution: Double, secondSolution: Double)
case OneReal(solution: Double)
case TwoNonReal
}
我们稍后会回到 TwoNonReal
。
您的函数现在可以 return 这个枚举的一个实例:
func quadraticFormulaSolver(variableA a: Double, variableB b: Double, variableC c: Double) -> QuadraticSolution {
为了使代码更具可读性,让我们过滤掉判别式:
let discriminant = (b * b) - (4.0 * a * c)
然后我们可以在上面使用switch
语句。如果它是积极的,你有两个真正的根源。如果它为零,则您有一个真正的(重复的)根。如果它是负数,你有两个非实根:
switch discriminant {
case _ where discriminant > 0:
let firstSolution = (-b + sqrt(discriminant)) / (2.0 * a)
let secondSolution = (-b - sqrt(discriminant)) / (2.0 * a)
return .TwoReal(firstSolution: firstSolution, secondSolution: secondSolution)
case _ where discriminant == 0:
let solution = (-b) / (2.0 * a)
return .OneReal(solution: solution)
default: // discriminant is negative
return .TwoNonReal
}
}
Swift 没有非实数的内置类型。我建议您将 swift-pons
嵌入到您的应用程序中,而不是重新发明轮子。
完成后,您可以将 TwoNonReal
枚举更改为 return 两个 Complex
数字:
case TwoNonReal(firstSolution: Complex, secondSolution: Complex)
然后你可以这样计算它们:
default: // discriminant is negative
let base = (-b) / (2.0 * a)
let firstSolution = base + (Complex.sqrt(-1.0 * discriminant)) / (2.0 * a)
let secondSolution = base - (Complex.sqrt(-1.0 * discriminant)) / (2.0 * a)
return .TwoNonReal(firstSolution: firstSolution, secondSolution: secondSolution)
}
我在 Swift 中创建了一个函数来求解并给出二次函数的解。我不知道如何调整我的功能,以便它给出假想的解决方案而不是打印,"There are no real solutions."
我对编程还比较陌生,需要一些帮助。这是我的代码:
func quadraticFormulaSolver(variableA a: Double, variableB b: Double, variableC c: Double) -> (Double, Double) {
let firstSolution: Double = (-b + sqrt((b * b) + (-4.0 * a * c))) / 2.0
let secondSolution: Double = (-b - sqrt((b * b) + (-4.0 * a * c))) / 2.0
let checkSolution: Double = sqrt((b * b) + (-4.0 * a * c))
if checkSolution > 0 {
print("There are two real solutions and they are \(firstSolution) and \(secondSolution)")
return(firstSolution, secondSolution) }
guard firstSolution != 0.0 else {
print("There is one real solution and it is \(firstSolution)")
return(firstSolution, secondSolution) }
guard checkSolution < 0 else {
print("There are no real solutions")
return(firstSolution, secondSolution) }
return(firstSolution, secondSolution)
}
由于您的函数可以 return 几个不同的选择,让我们做一个 Enum
来表示选项:
enum QuadraticSolution {
case TwoReal(firstSolution: Double, secondSolution: Double)
case OneReal(solution: Double)
case TwoNonReal
}
我们稍后会回到 TwoNonReal
。
您的函数现在可以 return 这个枚举的一个实例:
func quadraticFormulaSolver(variableA a: Double, variableB b: Double, variableC c: Double) -> QuadraticSolution {
为了使代码更具可读性,让我们过滤掉判别式:
let discriminant = (b * b) - (4.0 * a * c)
然后我们可以在上面使用switch
语句。如果它是积极的,你有两个真正的根源。如果它为零,则您有一个真正的(重复的)根。如果它是负数,你有两个非实根:
switch discriminant {
case _ where discriminant > 0:
let firstSolution = (-b + sqrt(discriminant)) / (2.0 * a)
let secondSolution = (-b - sqrt(discriminant)) / (2.0 * a)
return .TwoReal(firstSolution: firstSolution, secondSolution: secondSolution)
case _ where discriminant == 0:
let solution = (-b) / (2.0 * a)
return .OneReal(solution: solution)
default: // discriminant is negative
return .TwoNonReal
}
}
Swift 没有非实数的内置类型。我建议您将 swift-pons
嵌入到您的应用程序中,而不是重新发明轮子。
完成后,您可以将 TwoNonReal
枚举更改为 return 两个 Complex
数字:
case TwoNonReal(firstSolution: Complex, secondSolution: Complex)
然后你可以这样计算它们:
default: // discriminant is negative
let base = (-b) / (2.0 * a)
let firstSolution = base + (Complex.sqrt(-1.0 * discriminant)) / (2.0 * a)
let secondSolution = base - (Complex.sqrt(-1.0 * discriminant)) / (2.0 * a)
return .TwoNonReal(firstSolution: firstSolution, secondSolution: secondSolution)
}