Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'debugging': 0.07; 'float': 0.07; 'skip:` 10': 0.07; 'assumed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'python': 0.11; 'bug': 0.12; 'jan': 0.12; 'expression.': 0.16; 'infinity': 0.16; 'integers,': 0.16; 'nan': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'module': 0.19; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'documented': 0.24; 'integer': 0.24; 'skip:` 20': 0.24; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'code': 0.31; '"",': 0.31; 'division': 0.31; 'mod': 0.31; 'file': 0.32; '(most': 0.33; 'third': 0.33; 'something': 0.35; 'convert': 0.35; 'equal': 0.35; 'really': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'case?': 0.84; 'received:fios.verizon.net': 0.84; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Why `divmod(float('inf'), 1) == (float('nan'), float('nan'))` Date: Tue, 16 Sep 2014 21:12:48 -0400 References: <06397cb3-8baf-4af1-8152-ff7c3f6ceb62@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <06397cb3-8baf-4af1-8152-ff7c3f6ceb62@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1410916403 news.xs4all.nl 2894 [2001:888:2000:d::a6]:33923 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77949 On 9/16/2014 5:40 PM, cool-RR wrote: > While debugging my code I found that the bug was because I assumed > that something like `divmod(float('inf'), 1)` would be equal to > `(float('inf'), float('nan'))`, while Python returns `(float('nan'), > float('nan'))`. Why does Python make the division result be NaN in > this case? `float('inf') / 1` is still `float('inf')`. For integers, divmod(x, y) is defined as (x // y, x % y) == ((x - x%y) // y, x % y) == ((x - x%y) / y, x % y). For floats, Python is documented as using the third expression. "Help on built-in function divmod in module builtins: divmod(...) divmod(x, y) -> (div, mod) Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x." It does not really matter, however, as infinity cannot be 'floored' as required for // >>> math.floor(float('inf')) Traceback (most recent call last): File "", line 1, in math.floor(float('inf')) OverflowError: cannot convert float infinity to integer and hence >>> float('inf') // 1.0 nan -- Terry Jan Reedy