Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74266

Re: NaN comparisons - Call For Anecdotes

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.035
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; '64-bit': 0.07; 'float': 0.07; '32-bit': 0.09; 'def': 0.12; 'losing': 0.16; 'math,': 0.16; 'nan1': 0.16; 'nans,': 0.16; 'wed,': 0.18; '>>>': 0.22; 'import': 0.22; 'mind.': 0.24; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'equality': 0.31; 'steven': 0.31; 'struct': 0.31; 'received:google.com': 0.35; 'false': 0.36; 'doing': 0.36; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; "you're": 0.61; 'back': 0.62; 'jul': 0.74; 'subject:For': 0.78; 'double,': 0.84; 'cast': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=l8tUpwwvYSF4Hi/Kqldspr5WOpq1jYA7o+JRAFnRzXM=; b=TIwkcX4eX8YkXlfUrEeBBfGaNyXXvCfYDzcpAvSAT6uhiYOS80yOoTiINnECyC2L4W bdGu8I+gh6oNjx0DbL4KDWrXl/CkgKM2JQ8uOqkz+D5aKSrcIo/8/h+R0vOcNDqw2kTC AKiYvykQyT8VL2u8bGFCVUJX9TFyHLDa/X9TtAPiv5MpbpxU83STsVfpDYcWVN4Rix8n d8VFChfg/W9x1MLKRd5nZp+kVST/nAiHWEKM8Og3ucyo05/PaH99uHPtZHekiEYXWolU d/w+pJChSLYhlqzK8QgjCLzPwUIi1YNf7ASYZ+cUriWPFtDV5lhLUuUOqNZOiVK0sf8g ae7A==
X-Received by 10.70.33.228 with SMTP id u4mr12304992pdi.6.1404926826873; Wed, 09 Jul 2014 10:27:06 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <53bd739b$0$29995$c3e8da3$5496439d@news.astraweb.com>
References <mailman.11626.1404831235.18130.python-list@python.org> <53bc26ca$0$29995$c3e8da3$5496439d@news.astraweb.com> <mailman.11653.1404846131.18130.python-list@python.org> <53bc8861$0$29995$c3e8da3$5496439d@news.astraweb.com> <mailman.11692.1404918498.18130.python-list@python.org> <53bd739b$0$29995$c3e8da3$5496439d@news.astraweb.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 9 Jul 2014 11:26:26 -0600
Subject Re: NaN comparisons - Call For Anecdotes
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11700.1404926836.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1404926836 news.xs4all.nl 2877 [2001:888:2000:d::a6]:35328
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74266

Show key headers only | View raw


On Wed, Jul 9, 2014 at 10:53 AM, Steven D'Aprano
> Cast your 64-bit float into a 64-bit int. Or, if it's a C single rather
> than a double, cast the 32-bit float into a 32-bit int. Now you can
> compare them for equality without carrying about NANs, and without losing
> data. Later, when you're ready to start doing some numeric work on them,
> you cast back to floats. That's the idea I had in mind. Perhaps it
> doesn't match your use-case.

Unfortunately, it's not that simple:

>>> import math, struct
>>> def float_to_int(x):
...   return struct.unpack('L', struct.pack('d', x))[0]
...
>>> 0.0 == -0.0
True
>>> float_to_int(0.0) == float_to_int(-0.0)
False
>>> nan1 = struct.unpack('d', b'\x00'*6+b'\xf1\x7f')[0]
>>> math.isnan(nan1)
True
>>> nan2 = struct.unpack('d', b'\x00'*6+b'\xf2\x7f')[0]
>>> math.isnan(nan2)
True
>>> float_to_int(nan1) == float_to_int(nan1)
True
>>> float_to_int(nan2) == float_to_int(nan2)
True
>>> float_to_int(nan1) == float_to_int(nan2)
False

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

NaN comparisons - Call For Anecdotes "Anders J. Munch" <2014@jmunch.dk> - 2014-07-08 16:53 +0200
  Re: NaN comparisons - Call For Anecdotes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-08 17:13 +0000
    Re: NaN comparisons - Call For Anecdotes Chris Angelico <rosuav@gmail.com> - 2014-07-09 03:21 +1000
    Re: NaN comparisons - Call For Anecdotes "Anders J. Munch" <2014@jmunch.dk> - 2014-07-08 21:02 +0200
      Re: NaN comparisons - Call For Anecdotes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-09 00:10 +0000
        Re: NaN comparisons - Call For Anecdotes Terry Reedy <tjreedy@udel.edu> - 2014-07-09 00:57 -0400
          Re: NaN comparisons - Call For Anecdotes Steven D'Aprano <steve@pearwood.info> - 2014-07-09 06:43 +0000
            Re: NaN comparisons - Call For Anecdotes Chris Angelico <rosuav@gmail.com> - 2014-07-09 16:52 +1000
        Re: NaN comparisons - Call For Anecdotes "Anders J. Munch" <2014@jmunch.dk> - 2014-07-09 17:08 +0200
          Re: NaN comparisons - Call For Anecdotes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-09 16:53 +0000
            Re: NaN comparisons - Call For Anecdotes Ian Kelly <ian.g.kelly@gmail.com> - 2014-07-09 11:26 -0600
            Re: NaN comparisons - Call For Anecdotes "Anders J. Munch" <2014@jmunch.dk> - 2014-07-09 19:44 +0200
        Re: NaN comparisons - Call For Anecdotes Chris Angelico <rosuav@gmail.com> - 2014-07-10 01:13 +1000
        Re: NaN comparisons - Call For Anecdotes "Anders J. Munch" <2014@jmunch.dk> - 2014-07-09 18:24 +0200
    Re: NaN comparisons - Call For Anecdotes "Anders J. Munch" <2014@jmunch.dk> - 2014-07-08 21:25 +0200
  Re: NaN comparisons - Call For Anecdotes Rustom Mody <rustompmody@gmail.com> - 2014-07-09 20:07 -0700

csiph-web