This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: fractions.gcd results in infinite loop when nan or inf given as parameter.
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, rhettinger, snoeberger
Priority: low Keywords:

Created on 2014-08-20 17:31 by snoeberger, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg225576 - (view) Author: Robert Snoeberger (snoeberger) Date: 2014-08-20 17:31
>>> import fractions
>>> fractions.gcd(16, float('inf'))
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    fractions.gcd(16, float('inf'))
  File "C:\Python34-32bit\lib\fractions.py", line 24, in gcd
    a, b = b, a%b
KeyboardInterrupt
>>> fractions.gcd(16, float('nan'))
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    fractions.gcd(16, float('nan'))
  File "C:\Python34-32bit\lib\fractions.py", line 24, in gcd
    a, b = b, a%b
KeyboardInterrupt
>>> 

With the iterative algorithm that is used 

a, b = b, a%b

b converges to float('nan'). It will never become 0 to break out of the loop. It might be nice to error when the iteration has converged b to a value other than 0.
msg225584 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-08-20 19:51
I don't think this is an actual problem in practice and isn't worth mucking up clear and beautiful code.
msg225587 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-08-20 20:01
Also note that the fractions module is primarily about rational numbers (integer ratios).  The int type has no concept of NaNs and Infs, so I don't see any reason why the fractions module should cater to them.
msg225600 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-08-21 06:42
I agreed with Raymond.
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66434
2014-08-21 06:43:26mark.dickinsonsetstatus: open -> closed
resolution: wont fix
2014-08-21 06:42:58mark.dickinsonsetmessages: + msg225600
2014-08-20 20:01:12rhettingersetmessages: + msg225587
2014-08-20 19:51:23rhettingersetpriority: normal -> low

messages: + msg225584
2014-08-20 19:04:42pitrousetnosy: + rhettinger, mark.dickinson
2014-08-20 17:31:43snoebergercreate