Sling Academy
Home/Golang/Solving Quadratic Equations Using Go’s Math Functions

Solving Quadratic Equations Using Go’s Math Functions

Last updated: November 24, 2024

In mathematical computations, quadratic equations appear quite frequently. A quadratic equation takes the form of ax² + bx + c = 0, where x represents an unknown variable, and a, b, and c are known coefficients with a ≠ 0. Solving quadratic equations programmatically can be achieved using a variety of methods, one of which involves Go's math functions.

Basics: Quadratic Formula

The quadratic formula, x = (-b ± √(b² - 4ac)) / 2a, provides the roots of a quadratic equation. The expression under the square root, b² - 4ac, is known as the discriminant.

Step 1: Import Necessary Packages

The math package in Go provides the needed functionality for computing square roots.

package main

import (
    "fmt"
    "math"
)

Intermediate: Implementing the Solution

With Go, we can write a function that utilizes the quadratic formula to calculate the solutions.

func solveQuadratic(a, b, c float64) (float64, float64, error) {
    discriminant := b*b - 4*a*c

    if discriminant < 0 {
        return 0, 0, fmt.Errorf("No real roots exist")
    }

    sqrtDiscriminant := math.Sqrt(discriminant)
    root1 := (-b + sqrtDiscriminant) / (2 * a)
    root2 := (-b - sqrtDiscriminant) / (2 * a)

    return root1, root2, nil
}

Advanced: Handling Edge Cases

It’s crucial to handle specific scenarios beyond just positive and negative discriminants:

func solveQuadraticWithEdges(a, b, c float64) (float64, float64, error) {
    if a == 0 {
        return 0, 0, fmt.Errorf("Coefficient 'a' must not be zero")
    }

    discriminant := b*b - 4*a*c

    if discriminant < 0 {
        return 0, 0, fmt.Errorf("No real roots exists")
    } else if discriminant == 0 {
        // One real root
        root := -b / (2 * a)
        return root, root, nil
    }

    // Two real roots
    sqrtDiscriminant := math.Sqrt(discriminant)
    root1 := (-b + sqrtDiscriminant) / (2 * a)
    root2 := (-b - sqrtDiscriminant) / (2 * a)

    return root1, root2, nil
}

Example Usage

Here is how you can use the above function to solve for quadratic equations:

func main() {
    a, b, c := 1.0, 5.0, 6.0

    root1, root2, err := solveQuadraticWithEdges(a, b, c)
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Printf("Roots of quadratic equation: %.2f and %.2f\n", root1, root2)
    }
}

By understanding the above examples, you should be well-equipped to solve quadratic equations with varying degrees of complexity using Go's rich mathematical functions.

Next Article: Implementing Statistical Functions: Mean, Median, Mode in Go

Previous Article: Efficiently Generating Prime Numbers in Go

Series: Numbers and Math in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant