Example of a Real-root algorithm used to calculate a quadratic equation using Python programming.
This article provides an example of a real root algorithm for Python Programming to Implement mathematical operations for a quadratic equation. We will share this with you for your reference. The details are as follows:
Problem:
Define a function quadratic (a, B, c), receive three parameters, and return two solutions of the unary quadratic equation ax ² + bx + c = 0.
Implementation Code:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import mathdef quadratic (a, B, c): if a = 0: raise TypeError ('a cannot be 0') if not isinstance (a, (int, float) or not isinstance (B, (int, float) or not isinstance (c, (int, float): raise TypeError ('bad operand type') delta = math. pow (B, 2)-4 * a * c if delta <0: return 'no real root' x1 = (math. sqrt (delta)-B)/(2 * a) x2 =-(math. sqrt (delta) + B)/(2 * a) return x1, x2print (quadratic (2, 3, 1) print (quadratic (1, 3,-4 ))
Run the following command: