Repository: sagemath/sage

Issue:
sparse polynomial passing to the fraction field raises ZeroDivisionError
### Steps To Reproduce

I have no idea what's going on.
```
sage: P = PolynomialRing(ZZ, "q", sparse=True)
sage: q = P.gen()
sage: q^-1 + 2*q^-99
(q^98 + 2)/q^99
sage: q^-1 + q^-100
(q^99 + 1)/q^100
sage: q^-1 + 2*q^-100
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In [20], line 1
----> 1 q**-Integer(1) + Integer(2)*q**-Integer(100)

File ~/sage-trac/src/sage/rings/integer.pyx:1958, in sage.rings.integer.Integer.__mul__()
   1956         return y
   1957 
-> 1958     return coercion_model.bin_op(left, right, operator.mul)
   1959 
   1960 cpdef _mul_(self, right):

File ~/sage-trac/src/sage/structure/coerce.pyx:1232, in sage.structure.coerce.CoercionModel.bin_op()
   1230     self._record_exception()
   1231 else:
-> 1232     return PyObject_CallObject(op, xy)
   1233 
   1234 if op is mul:

File ~/sage-trac/src/sage/structure/element.pyx:1513, in sage.structure.element.Element.__mul__()
   1511 cdef int cl = classify_elements(left, right)
   1512 if HAVE_SAME_PARENT(cl):
-> 1513     return (<Element>left)._mul_(right)
   1514 if BOTH_ARE_ELEMENT(cl):
   1515     return coercion_model.bin_op(left, right, mul)

File ~/sage-trac/src/sage/rings/fraction_field_element.pyx:667, in sage.rings.fraction_field_element.FractionFieldElement._mul_()
    665 if self._parent.is_exact():
    666     try:
--> 667         d1 = rnum.gcd(sden)
    668         d2 = snum.gcd(rden)
    669         if not d1.is_unit():

File ~/sage-trac/src/sage/structure/element.pyx:4689, in sage.structure.element.coerce_binop.new_method()
   4687 def new_method(self, other, *args, **kwargs):
   4688     if have_same_parent(self, other):
-> 4689         return method(self, other, *args, **kwargs)
   4690     else:
   4691         a, b = coercion_model.canonical_coercion(self, other)

File ~/sage-trac/src/sage/rings/polynomial/polynomial_element_generic.py:973, in Polynomial_generic_sparse.gcd(self, other, algorithm)
    970 sd = self.degree()
    971 od = other.degree()
    972 if max(sd,od) < 100 or \
--> 973    min(len(self.__coeffs)/sd, len(other.__coeffs)/od) > .06:
    974     implementation = "FLINT"
    975 else:

ZeroDivisionError: division by zero
```

### Expected Behavior

I'd expect `(q^99 + 2)/q^100`.


### Actual Behavior

See above

### Additional Information

_No response_

### Environment

```markdown
* OS: 22.04
* Sage Version: 10.2.beta5
```


### Checklist

- [X] I have searched the existing issues for a bug report that matches the one I want to file, without success.
- [X] I have read the documentation and troubleshoot guide


Discussion:
Even simpler:
```
sage: 2*q^-100
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
...
```
The `ZeroDivisionError` arises when taking the `gcd` of a constant with a polynomial of large degree:
```
sage: gcd(1, q^100)
---------------------------------------------------------------------------
ZeroDivisionError
...
```
The offending code (the last line with a `-->` in the original traceback) divides by the degree of each polynomial without checking whether either of these degrees is 0.

We can also get a `ZeroDivisionError` error at a different place in the code:
```
sage: gcd(q^0, q^100)
...
File ~/development/sage/src/sage/rings/integer.pyx:2016, 
in sage.rings.integer.Integer.__truediv__()
   2014 if type(left) is type(right):
   2015     if mpz_sgn((<Integer>right).value) == 0:
-> 2016         raise ZeroDivisionError("rational division by zero")
   2017     x = <Rational> Rational.__new__(Rational)
   2018     mpq_div_zz(x.value, (<Integer>left).value, (<Integer>right).value)

ZeroDivisionError: rational division by zero
```
But it comes from the same line of code:
```
--> 973    min(len(self.__coeffs)/sd, len(other.__coeffs)/od) > .06:
```

Resolving patch:
diff --git a/src/sage/rings/polynomial/polynomial_element_generic.py b/src/sage/rings/polynomial/polynomial_element_generic.py
--- a/src/sage/rings/polynomial/polynomial_element_generic.py
+++ b/src/sage/rings/polynomial/polynomial_element_generic.py
@@ -953,6 +953,17 @@ def gcd(self,other,algorithm=None):
             1
             sage: (6*x).gcd(9)
             3
+
+        Check that :trac:`36427` is fixed::
+
+            sage: P = PolynomialRing(ZZ, "q", sparse=True)
+            sage: q = P.gen()
+            sage: 2*q^-100
+            2/q^100
+            sage: gcd(1, q^100)
+            1
+            sage: gcd(q^0, q^100)
+            1
         """
 
         from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
@@ -969,8 +980,8 @@ def gcd(self,other,algorithm=None):
             # <https://groups.google.com/d/msg/sage-devel/6qhW90dgd1k/Hoq3N7fWe4QJ>
             sd = self.degree()
             od = other.degree()
-            if max(sd,od) < 100 or \
-               min(len(self.__coeffs)/sd, len(other.__coeffs)/od) > .06:
+            if ((sd < 100 or len(self.__coeffs)/sd > .06)
+                    and (od < 100 or len(other.__coeffs)/od > .06)):
                 implementation = "FLINT"
             else:
                 implementation = "NTL"


Test patch: