@@ -197,9 +197,18 @@ cdef extern from *:
197
197
int trailing_zeros_ullong " __Quicktions_trailing_zeros_ullong" (unsigned long long x)
198
198
199
199
200
- cpdef _gcd(a: int , b: int ):
200
+ def _gcd (a , b ):
201
201
""" Calculate the Greatest Common Divisor of a and b as a non-negative number.
202
202
"""
203
+ if not isinstance (a, int ):
204
+ raise ValueError (f" Expected int, got {type(a).__name__}" )
205
+ if not isinstance (b, int ):
206
+ raise ValueError (f" Expected int, got {type(b).__name__}" )
207
+
208
+ return _igcd(int (a), int (b))
209
+
210
+
211
+ cdef _igcd(a, b):
203
212
if HAS_ISLONGLONG:
204
213
if PyLong_IsLongLong(a) and PyLong_IsLongLong(b):
205
214
return _c_gcd(< unsigned long long > a, < unsigned long long > b)
@@ -211,7 +220,7 @@ cpdef _gcd(a: int, b: int):
211
220
if HAS_OLD_PYLONG_GCD:
212
221
return _PyLong_GCD(a, b)
213
222
214
- return _gcd_fallback(a, b )
223
+ return _gcd_fallback(int (a), int (b) )
215
224
216
225
217
226
ctypedef unsigned long long ullong
@@ -760,7 +769,7 @@ cdef class Fraction:
760
769
numerator = int (numerator)
761
770
if not isinstance (denominator, int ):
762
771
denominator = int (denominator)
763
- g = _gcd (numerator, denominator)
772
+ g = _igcd (numerator, denominator)
764
773
# NOTE: 'is' tests on integers are generally a bad idea, but
765
774
# they are fast and if they fail here, it'll still be correct
766
775
if denominator < 0 :
0 commit comments