J'ai créé une fonction dans Swift pour résoudre et donner les solutions à une fonction quadratique. Je ne sais pas comment adapter ma fonction pour qu'elle donne des solutions imaginaires au lieu d'imprimer: "Il n'y a pas de vraies solutions". Je suis relativement nouveau à la programmation et pourrait utiliser de l'aide. Voici mon code:Comment puis-je adapter mon code Swift pour donner des solutions imaginaires à un quadratique?
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)
}