Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #74367
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| 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; 'true,': 0.05; 'affected': 0.07; 'context': 0.07; 'float': 0.07; 'suppose': 0.07; 'differently.': 0.09; 'false,': 0.09; 'if,': 0.09; 'obey': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'c-level': 0.16; 'comparison.': 0.16; 'correlation': 0.16; 'first:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function),': 0.16; 'latter,': 0.16; 'losing': 0.16; 'nan': 0.16; 'nans': 0.16; 'nans,': 0.16; 'numpy': 0.16; 'simpson': 0.16; 'subject:alternative': 0.16; 'subject:based': 0.16; 'surprising': 0.16; 'extensions': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '(not': 0.18; 'bit': 0.19; '>>>': 0.22; 'import': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'comparing': 0.24; 'mind.': 0.24; 'please?': 0.24; 'replace': 0.24; 'fairly': 0.24; 'math': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'compare': 0.26; 'extension': 0.26; 'header :In-Reply-To:1': 0.27; 'specifically': 0.29; 'chris': 0.29; 'am,': 0.29; 'array': 0.29; "doesn't": 0.30; 'compared': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'work.': 0.31; '(which': 0.31; 'code': 0.31; 'bunch': 0.31; 'equivalent.': 0.31; 'faster,': 0.31; 'yes.': 0.31; 'class': 0.32; 'probably': 0.32; 'another': 0.32; 'says': 0.33; 'everyone': 0.33; 'implemented': 0.33; "i'd": 0.34; 'good.': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'done': 0.36; "didn't": 0.36; 'possible': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'implement': 0.38; 'manager': 0.38; 'same.': 0.38; 'does': 0.39; '12,': 0.39; 'extremely': 0.39; 'skip:x 10': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'applicable': 0.60; 'skip:y 20': 0.60; 'hope': 0.61; 'free': 0.61; 'no.': 0.61; 'numbers': 0.61; 'simple': 0.61; 'first': 0.61; 'act': 0.63; 'information': 0.63; 'kind': 0.63; 'skip:n 10': 0.64; 'jul': 0.74; 'introduce': 0.78; "it'd": 0.84; 'subject:Proposal': 0.91; 'thing,': 0.91; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=3wV/U8ouAYAiHoLiJmg7glm3BURvKjwa6IVzP7KdDuI=; b=tw14lBoOY73ykD1kDOunjB+t8tpYr7hTayIOizzM2ZUSTdR/q+KoCetcpSdB1Dnopm qhNY3+orXdm7eZAm+FjX/4rfM47wTrUTJtAxpfGwq6vOZQ3bcLV0z00vXrw1WXtR7rvC s2Qgda/a22clgoGMOJIqsQYTlxsgOHbaf5IJcYyoe3y48i6xJwTKjnqOEAdfU/zYMmUl 6gGtTObLTAYsokp+fH19kafYTFpKH1t8rAlnXcVUV4FNVtHx3yMJ/y7n4i0n5DhthZk+ asQf6VUAsBbCRjZhZ/nITZ2D2HLAKwrLzwRL2M8fDsmUC7MvU+Ty0a3sdBakvL2Xu0kR YVFQ== |
| MIME-Version | 1.0 |
| X-Received | by 10.221.4.66 with SMTP id ob2mr3821348vcb.28.1405144385036; Fri, 11 Jul 2014 22:53:05 -0700 (PDT) |
| In-Reply-To | <20140711223755.GA1909@cskk.homeip.net> |
| References | <CAPTjJmoQwfnNzXd6Bf4MoDcrR=AC3NBq_jB_vvfmtyJbxECKkw@mail.gmail.com> <20140711223755.GA1909@cskk.homeip.net> |
| Date | Sat, 12 Jul 2014 15:53:04 +1000 |
| Subject | Re: context manager based alternative to Re: Proposal: === and !=== |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <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.11767.1405144393.18130.python-list@python.org> (permalink) |
| Lines | 92 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1405144393 news.xs4all.nl 2933 [2001:888:2000:d::a6]:38311 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:74367 |
Show key headers only | View raw
On Sat, Jul 12, 2014 at 8:37 AM, Cameron Simpson <cs@zip.com.au> wrote:
> On 11Jul2014 14:37, Chris Angelico <rosuav@gmail.com> wrote:
>>
>> Does C-level code have to check this flag before comparing
>> nans,
>
>
> If you mean:
>
> float x, y;
> [...]
> if (x == y) {
> action...
> }
>
> then no.
>
>
>> or is this applicable only to the Python float objects and only
>> when compared in Python?
>
>
> The former but not the latter, in my mind. Comparing Python float objects
> should obey the rule, whether in pure Python or extensions using a PyFloat.
> But I'd hope they get that for free by indirecting through PyFloat's
> methods.
Suppose you have some C extension that works with numbers (I have
NumPy in mind as I write this, but I'm not familiar with its inner
workings; certainly it'd be possible for this to be implemented how I
describe). You pack a bunch of Python float objects into an array,
pack another bunch into another array, and then compare them.
>>> from numpy import array
>>> x=array([1.0,2.0])
>>> y=array([1.0,2.1])
>>> x==y
array([ True, False], dtype=bool)
So far, so good. Now let's introduce a NaN.
>>> nan=float("nan")
>>> x=array([1.0,2.0,nan])
>>> y=array([1.0,2.1,nan])
>>> x==y
array([ True, False, False], dtype=bool)
I deliberately used the exact same nan so that an identity or
bit-pattern check would return True; but numpy says False, these are
not the same. Now, if I replace array with this list-like thing, then
your context manager would control that last result:
>>> class array(list):
def __eq__(self, other):
if not isinstance(other, type(self)) or len(other)!=len(self):
return False
return [self[i] == other[i] for i in range(len(self))]
But if, instead, the array is packed into a C-level array of floats
(which can be done without losing any information other than object
identity), then this would no longer be affected by your context
manager, unless the extension unnecessarily reconstructs two Python
floats to compare them, or else is made specifically aware of the
change.
>> Is isnan() still usable?
>
> Yes.
>
>> (Consider that x!=x == math.isnan(x) normally.)
>
> Can you elaborate on this statement please?
def isnan(x):
return isinstance(x, float) and x!=x
Fairly standard way of defining isnan(). It would be extremely
surprising if that correlation didn't work. Let's suppose you want to
sort a list of floats with all NaNs coming first:
new_list = sorted(old_list, key=lambda x: (x==x, x))
new_list = sorted(old_list, key=lambda x: (not math.isnan(x), x))
The first one is much briefer (and faster, as it doesn't need to look
up math and math.isnan and then call a function), and people will
assume they're equivalent. If this kind of thing happens deep inside a
call tree, it'll be extremely surprising that the two act differently.
So there has to be some other way to implement isnan(), which will
probably involve poking around with the bit pattern, and everyone MUST
use that instead of the simple comparison.
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: context manager based alternative to Re: Proposal: === and !=== Chris Angelico <rosuav@gmail.com> - 2014-07-12 15:53 +1000
csiph-web