Jesus Anaya
Jesus Anaya

Reputation: 21

Defining function using if-statement

def roots4(a,b,c,d):
    d = b * b - 4 * a * c
    if a != 0 and d == 0:
        roots4(a,b,c,d) 
        x = -b/ (2*a)
    if a != 0 and d > 0:
        roots4(a,b,c,d)
        x1 = (-b + math.sqrt(d)) / 2.0 / a
        x2 = (-b - math.sqrt(d)) / 2.0 / a
    if a != 0 and d < 0:
        roots4(a,b,c,d)
        xre = (-b) / (2*a)   
        xim = (math.sqrt(d))/ (2*a)
        print x1  = xre + ixim
        strx1 = "x2 = %6.2f + i %6.2f" %(xre, xim)
        print strx1 

This is part of my code for a project. What I'm trying to do is define roots4(a,b,c,d). For example, in the case that a != 0 and d == 0 then roots4(a,b,c,d) shall then go to to finding x by solving equation x = -b/ (2*a). And so on... I don't know what I'm doing wrong. Any tips?

Upvotes: 0

Views: 74

Answers (3)

zezollo
zezollo

Reputation: 5017

As pointed out in comments and jimmy's answer, there's no need for recursive calls.

Here's an example of how you could correct it (adapt it the way you like):

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import math


def roots4(a, b, c):
    """Prints the solutions of ax² + bx + c = 0, if a != 0"""
    if a == 0:
        print 'This function is meant to solve a 2d degree equation, '\
            'not a 1st degree one.'
    else:
        d = b * b - 4 * a * c
        solutions = []
        if d == 0:
            solutions = [str(-b / (2 * a))]
        elif d > 0:
            solutions = [str((-b + math.sqrt(d)) / 2.0 / a),
                         str((-b - math.sqrt(d)) / 2.0 / a)]
        elif d < 0:
            xre = str((-b) / (2 * a))
            xim = str((math.sqrt(-d)) / (2 * a))
            solutions = [xre + " + i" + xim,
                         xre + " - i" + xim]

        print "\nEquation is: {}x² + {}x + {} = 0".format(a, b, c)

        if len(solutions) == 1:
            print "There's only one solution: " + solutions[0]
        else:
            print "Solutions are: " + " and ".join(solutions)

roots = [(0.0, 0.0, 0.0),
         (0.0, 0.0, 1.0),
         (0.0, 2.0, 4.0),
         (1.0, 2.0, 1.0),
         (1.0, -5.0, 6.0),
         (1.0, 2.0, 3.0)]

for r in roots:
    roots4(*r)

Output:

$ ./test_script2.py
This function is meant to solve a 2d degree equation, not a 1st degree one.
This function is meant to solve a 2d degree equation, not a 1st degree one.
This function is meant to solve a 2d degree equation, not a 1st degree one.

Equation is: 1.0x² + 2.0x + 1.0 = 0
There's only one solution: -1.0

Equation is: 1.0x² + -5.0x + 6.0 = 0
Solutions are: 3.0 and 2.0

Equation is: 1.0x² + 2.0x + 3.0 = 0
Solutions are: -1.0 + i1.41421356237 and -1.0 - i1.41421356237

Upvotes: 0

Azat Ibrakov
Azat Ibrakov

Reputation: 10971

you probably stacked with

    ...
            roots4(a,b,c,d)
    ...

this causes an infinite loop

firstly, why do you need a recursion call? what's d parameter for?

secondly, what is ixim? should it be something like xim * 1j? what do you expect from print x1 = xre + ixim?

if you want print only in case when d < 0 this will be fine

    from math import sqrt


    def roots4(a,b,c):
        if a != 0.:
            x_left = -b/(2*a)

            d = b * b - 4 * a * c
            if d == 0.:
                x_right = 0.
            elif d > 0.:
                x_right = sqrt(d) / (2 * a)
            else:
                xim = sqrt(-d) / (2 * a)
                strx1 = "x1 = %6.2f + i %6.2f" %(x_left, xim)
                print strx1
                strx2 = "x2 = %6.2f - i %6.2f" %(x_left, xim)
                print strx2
                x_right = xim * 1j
            x1 = x_left + x_right
            x2 = x_left - x_right
        else:
            raise ValueError("incorrect leading coefficient in given square equation")

Upvotes: 1

jimmy
jimmy

Reputation: 2021

Well, it looks like you have made an infinite loop with your recursive functions. As it looks now you will never do any calculations apart from

d = b * b - 4 * a * c

over and over again.

Consider the program flow. Each time you reach

roots4(a,b,c,d)

you will end up in the top again.

Upvotes: 0

Related Questions