-1
def ec_grad2(p, q):
    delta = p**2 - 4*q
    sqrt_delta = delta**0.5
    if delta < 0:
        return "No real solutions"
    elif delta == 0:
        x1 = -p/2
        x2 = x1
        return (x1, x2)
    elif delta > 0:
        x1 = -2*q/(p + sqrt_delta)
        x2 = 2*q/(sqrt_delta - p)
        return (x1, x2)
    
        
def printing(function_return):
    if type(function_return) == str:
        print(function_return)
    else:
        a, b = function_return
        print("x1 = {:.20f} si x2 = {:.20f}".format(a, b))

When I try to find the roots for the quadratic equation for large numbers I get the overflowError. Does anybody have any clue on how to solve this. I tried with the Vieta's formulas and also with Decimal.

nonDucor
  • 2,057
  • 12
  • 17
Fabi
  • 3
  • 3

1 Answers1

0

As the error points out, the int is too large to be converted to a float normally. Python has this module named decimal, you could use decimal.Decimal to do your float conversions.

from decimal import Decimal

def ec_grad2(p, q):
    delta = p ** 2 - 4 * q
    sqrt_delta = Decimal(delta) ** Decimal(0.5)
    if delta < 0:
        return "No real solutions"
    elif delta == 0:
        x1 = -p / 2
        x2 = x1
        return (x1, x2)
    elif delta > 0:
        x1 = -2 * q / (p + sqrt_delta)
        x2 = 2 * q / (sqrt_delta - p)
        return (x1, x2)


def printing(function_return):
    if type(function_return) == str:
        print(function_return)
    else:
        a, b = function_return
        print("x1 = {:.20f} si x2 = {:.20f}".format(a, b))

This should work for you

Ryuga
  • 132
  • 1
  • 1
  • 8