Repository: sympy/sympy

Issue:
"0/0 = 0" from `0 / Symbol('z', zero=True)`
@skirpichev noted this in https://github.com/sympy/sympy/issues/8751#issuecomment-68861692 in a now-closed bug and I didn't see it filed elsewhere.

````
S(0)/0   # nan, correct
````

But now:
````
z = Symbol('z', zero=True)
0/z  # 0, wrong, should be nan
(1/z).is_finite   # False, correct    (UPDATED, thanks @debugger22)
0*(1/z)   # 0, wrong, should be nan
````

Similarly:
````
f = Symbol('f', finite=False)
0*f   # 0, this is wrong, should be nan
````

Something should be checking `is_finite`?  I started taking a look inside `mul.py` but scary...!

Discussion:
debugger22: @cbm755 Please review.
debugger22: Now:
```
mrsud@jarvis:~/workspace/sympy$ time ./bin/test sympy/integrals/tests/test_meijerint.py
============================== test process starts ==============================
executable:         /usr/bin/python  (2.7.5-final-0) [CPython]
architecture:       64-bit
cache:              yes
ground types:       python 
random seed:        93926777
hash randomization: on (PYTHONHASHSEED=2679446471)

sympy/integrals/tests/test_meijerint.py[21] .....................            [OK]

================= tests finished: 21 passed, in 788.01 seconds ==================

real	13m8.764s
user	12m12.048s
sys	0m2.916s
```

Earlier:
```
mrsud@jarvis:~/workspace/sympy$ time ./bin/test sympy/integrals/tests/test_meijerint.py
============================= test process starts ==============================
executable:         /usr/bin/python  (2.7.5-final-0) [CPython]
architecture:       64-bit
cache:              yes
ground types:       python 
random seed:        33224468
hash randomization: on (PYTHONHASHSEED=1274036100)

sympy/integrals/tests/test_meijerint.py[21] .....................           [OK]

================= tests finished: 21 passed, in 355.37 seconds =================

real	5m56.866s
user	5m16.792s
sys	0m1.888s
```

This change almost doubled the time taken by meijerint tests. I'm clueless how these couple of ifs caused these.
debugger22: @asmeurer If I'm right then this, at least, shows how badly assumptions affect the performance when they are in core.
cbm755: Sorry @debugger22, I fear this is a bit outside my comfort zone...

Re: Meijerint slow down: IIRC, `is_zero` is cheap but worth double-checking?  Perhaps more likely: some code path in there doesn't really expect NaN (see e.g., #7942).

debugger22: > Perhaps more likely: some code path in there doesn't really expect NaN (see e.g., #7942).

This makes more sense.

Clearly, this isn't a good fix. Or maybe a bigger issue is at play here.
debugger22: This one worked.
**In my branch**
```
mrsud@jarvis:~/workspace/sympy$ ./bin/test sympy/integrals/tests/test_meijerint.py
============================== test process starts ===============================
executable:         /usr/bin/python  (2.7.5-final-0) [CPython]
architecture:       64-bit
cache:              yes
ground types:       python 
random seed:        60322084
hash randomization: on (PYTHONHASHSEED=1973071046)

sympy/integrals/tests/test_meijerint.py[21] .....................             [OK]

================== tests finished: 21 passed, in 112.14 seconds ==================
```

**In master**
```
mrsud@jarvis:~/workspace/sympy$ ./bin/test sympy/integrals/tests/test_meijerint.py
============================== test process starts ===============================
executable:         /usr/bin/python  (2.7.5-final-0) [CPython]
architecture:       64-bit
cache:              yes
ground types:       python 
random seed:        21661312
hash randomization: on (PYTHONHASHSEED=892971179)

sympy/integrals/tests/test_meijerint.py[21] .....................             [OK]

================== tests finished: 21 passed, in 111.44 seconds ==================
```

@cbm755 I was going to leave it unevaluated but thought to give it another spin. 
debugger22: @asmeurer Are you +1 to this?
cbm755: @debugger22: I think this looks good apart except the deleted test mentioned earlier and the tests for the `self.args` stuff...
asmeurer: Looks good to me as well. 
debugger22: @shivamvats Do you mind adding those tests here which you missed in https://github.com/sympy/sympy/pull/8401?
shivamvats: @debugger22 I'm sorry for errors in #8401. 
Which tests do you want me to add (I can  see is_finite mentioned)? I will do it in a day or two as I have tests going on.
debugger22: @shivamvats  Cool.  Probably, you should add tests for `is_finite` and `is_real` handlers for `csch` and `sech` so that the code that I changed in this PR gets covered by them.

Patch:
diff --git a/sympy/core/mul.py b/sympy/core/mul.py
index 30a0231525..982d8db475 100644
--- a/sympy/core/mul.py
+++ b/sympy/core/mul.py
@@ -549,7 +549,10 @@ def _handle_for_oo(c_part, coeff_sign):
 
         # 0
         elif coeff is S.Zero:
-            # we know for sure the result will be 0
+            # we know for sure the result will be 0 except the multiplicand
+            # is infinity
+            if any(c.is_finite == False for c in c_part):
+                return [S.NaN], [], order_symbols
             return [coeff], [], order_symbols
 
         # check for straggling Numbers that were produced


Test patch:
diff --git a/sympy/core/tests/test_assumptions.py b/sympy/core/tests/test_assumptions.py
index 4e29f4e8ca..67bbc3c7fa 100644
--- a/sympy/core/tests/test_assumptions.py
+++ b/sympy/core/tests/test_assumptions.py
@@ -901,7 +901,16 @@ def test_issues_8632_8633_8638_8675_8992():
     n = Dummy(negative=True)
     assert (n - 3).is_nonpositive
 
+
 def test_issue_9115():
     n = Dummy('n', integer=True, nonnegative=True)
     assert (factorial(n) >= 1) == True
     assert (factorial(n) < 1) == False
+
+
+def test_issue_9165():
+    z = Symbol('z', zero=True)
+    f = Symbol('f', finite=False)
+    assert 0/z == S.NaN
+    assert 0*(1/z) == S.NaN
+    assert 0*f == S.NaN