-1

I have created a function in Swift to solve and give the solutions to a quadratic function. I don't know how to adapt my function so that it will give the imaginary solutions instead of printing, "There are no real solutions."

I am relatively new to programming and could use some help. Here is my code:

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)
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Gabe Garboden
  • 33
  • 1
  • 4

1 Answers1

0

Since your function can return a few different choices, let's make an Enum to represent the options:

enum QuadraticSolution {
    case TwoReal(firstSolution: Double, secondSolution: Double)
    case OneReal(solution: Double)
    case TwoNonReal
}

We'll come back to TwoNonReal in a bit.

Your function can now return an instance of this enum:

func quadraticFormulaSolver(variableA a: Double, variableB b: Double, variableC c: Double) -> QuadraticSolution {

To make the code a bit more readable, let's filter out the discriminant:

    let discriminant = (b * b) - (4.0 * a * c)

Then we can use a switch statement on it. If it's positive, you have two real roots. If it's zero, you have one real (repeated) root. If it's negative, you have two non-real roots:

    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 doesn't have a built-in type for the non-real numbers. Rather than reinvent the wheel, I suggest you embed swift-pons in your app.

Once you do that, then you can change your TwoNonReal enum to return two Complex numbers:

    case TwoNonReal(firstSolution: Complex, secondSolution: Complex)

Then you can calculate them like so:

    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)
    }
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287