Last active
November 5, 2021 20:26
-
-
Save jmlon/5d363638ca38e045bafd23a1055695d1 to your computer and use it in GitHub Desktop.
Solución de la ecuación cuadratica (solo raíces reales)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Módulo para funciones matematicas en los reales | |
# https://docs.python.org/3/library/math.html | |
import math | |
a = float(input("Ingrese a: ")) | |
b = float(input("Ingrese b: ")) | |
c = float(input("Ingrese c: ")) | |
# Comprobar la precondición | |
if b**2-4*a*c>=0: | |
x1 = (-b+math.sqrt(b**2-4*a*c))/(2*a) | |
x2 = (-b-math.sqrt(b**2-4*a*c))/(2*a) | |
print('Primera raiz ', x1) | |
print('Segunda raiz ', x2) | |
# Comprobar las postcondiciones | |
assert math.fabs(a*x1**2 + b*x1 + c) < 0.000000001, "La primera raiz no cumple" | |
assert math.fabs(a*x2**2 + b*x2 + c) < 0.000000001, "La segunda raiz no cumple" | |
else: | |
print('Ecuación sin raices reales') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment