How can I do calculations in swift

I'm making a Quadratic Solver. As shown on the picture, I have value a,b,c. I want to use the formula x=(-b+-√(b^2-4ac))/2a to solve it. I would like the result to be shown at x1 and x2. What should I write in the code?

My codes are below

User uploaded file

import UIKit


class ViewController: UIViewController {


@IBOutletweakvar a: UITextField!

@IBOutletweakvar b: UITextField!

@IBOutletweakvar c: UITextField!

@IBAction func solve(_ sender: AnyObject) {

equation.text = "\(a.text!) x^2 + \(b.text!) x + \(c.text!) = 0"

x1.text = (-b+(√b^-4ac))/2a

x2.text = (-b-(√b^-4ac))/2a

}

@IBOutlet weak var equation: UILabel!

@IBOutletweakvar x1: UILabel!

@IBOutletweakvar x2: UILabel!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

}


override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}



}

MacBook Pro, iOS 10, Xcode

Posted on Sep 22, 2016 4:35 AM

Reply
6 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I do calculations in swift

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.