Example of a Real-root algorithm used to calculate a quadratic equation using Python programming.

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.