-5

In an optimization problem,if function zero(x ,y) in the code given below is objective function with initial values of x,y as 0,0.Then, to solve the problem i need to compute the obj. function as well as its derivitive. So, when i am doing so,i am getting ZeroDivisionError: division by zero.

[x,y] = [0,0]
def zero(x ,y):
    return 3 + x/y + 2*xy + 1/x

Output is:

 Traceback (most recent call last):
  File "<ipython-input-5-4e3e5e87c5f1>", line 1, in <module>
    runfile('C:/Users/HP/Documents/Python Scripts/Python Scripts_majrpjct/working/practice_cvxopt.py', wdir='C:/Users/HP/Documents/Python Scripts/Python Scripts_majrpjct/working')
  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/HP/Documents/Python Scripts/Python Scripts_majrpjct/working/practice_cvxopt.py", line 394, in <module>
    print   zero(x,y)
  File "C:/Users/HP/Documents/Python Scripts/Python Scripts_majrpjct/working/practice_cvxopt.py", line 392, in zero
    return 3 + x/y
ZeroDivisionError: division by zero

Can anyone suggest me a solution so as to get correct answer by ignoring ZeroDivisionError values.

  • 2
    Ok, so what _should_ the correct answer be? What would you prefer `zero(0,0)` to return? – Kevin Jun 30 '17 at 17:47
  • 0/0 is not equal to 0. It is undefined. – Tony Tuttle Jun 30 '17 at 17:48
  • [`try/except`](https://docs.python.org/3/reference/compound_stmts.html#try)? – Christian Dean Jun 30 '17 at 17:49
  • 2
    "if we consider P = ( 3+ x/y) , if (x,y) = (0,0), then the value of P = 3" - you need to get your math straight first, because 3+0/0 is not 3. The division operation is not defined for second argument 0. – user2357112 Jun 30 '17 at 17:49
  • 0/0 is undefined, what is your argument for you to say is 0? – eyllanesc Jun 30 '17 at 17:50
  • He said what he wanted to do when y = 0. He wants it to be zero. That's a valid thing to want it to be, depending on the situation. He just doesn't know how to make that happen. Forgive my limited pronouns if you're not a he. – Alan Leuthard Jun 30 '17 at 17:51
  • 1
    Even if you want to set 0/0=0, what about 1/0? – Alex Hall Jun 30 '17 at 17:53
  • See [this answer](https://stackoverflow.com/a/3931746/355230) to another question that describes how/whether to use `try/except` to handle cases like this. – martineau Jun 30 '17 at 18:10

2 Answers2

2

This is what try blocks are for! If you don't want to sanitize your inputs and are coding for a situation where a division by zero IS zero (the other option is infinity which makes for some much more interesting math), then a simple try block is the way to go. If you need precision, sanitize your inputs (make sure y isn't zero before sending it to the function).

Code:

def zero(x ,y):
    try:
        return 3 + x/y
    except ZeroDivisionError:
        return 3
        #  or do whatever else you want to do when y is zero. 
Alan Leuthard
  • 798
  • 5
  • 11
0

How about this:

def zero(x, y):
    if abs(x) < 1e-10 and abs(y) < 1e-10:
        return 3.0
    elif abs(y) < 1e-10:
        return 0.0
    else:
        return 3 + x/y
TMueller83
  • 412
  • 3
  • 11
  • But the thing is,i need this part of code to be used in my optimization problem where the calculations are more complex involving variouscomponents and calculated through iterations. – Manasaveena Jun 30 '17 at 18:03
  • Then you have to make the `if`, `elif`, `else` tests more complex, and you have to specify the result for each limiting case. If `x` and `y` are for example arrays, then you could use – TMueller83 Jun 30 '17 at 18:07
  • ... `y=np.array(y)` and test if there are zeros in `y` by `np.any(abs(y) < 1e-10)`, Where `np` refers to `numpy` imported as `np`. – TMueller83 Jun 30 '17 at 18:17