Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; '16,': 0.03; 'value,': 0.04; 'represents': 0.05; 'subject:Python': 0.06; 'float': 0.07; 'builtin': 0.09; 'currently,': 0.09; 'defines': 0.09; 'implements': 0.09; 'integral': 0.09; 'meaningful': 0.09; 'operand': 0.09; 'storage,': 0.09; 'subset': 0.09; 'sure,': 0.09; 'type,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '(complex)': 0.16; 'cls': 0.16; 'decimal.': 0.16; 'docstring': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'self),': 0.16; 'subclass': 0.16; 'subclasses.': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'bit': 0.19; 'skip:p 40': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'mathematical': 0.24; 'specifies': 0.24; 'mon,': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'possibly': 0.26; 'defined': 0.27; 'header :In-Reply-To:1': 0.27; 'rest': 0.29; 'am,': 0.29; 'mix': 0.30; 'see,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; "skip:' 10": 0.31; 'too.': 0.31; '"",': 0.31; '(although': 0.31; 'contrast,': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'int,': 0.31; 'steven': 0.31; 'file': 0.32; 'class': 0.32; 'probably': 0.32; '(most': 0.33; 'are:': 0.33; 'guess': 0.33; 'skip:d 20': 0.34; 'could': 0.34; "can't": 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'really': 0.36; 'false': 0.36; 'should': 0.36; 'requirements': 0.37; 'list': 0.37; 'implement': 0.38; 'represent': 0.38; 'anything': 0.39; 'recent': 0.39; 'sure': 0.39; 'number,': 0.60; 'conversion': 0.61; 'utilize': 0.61; 'numbers': 0.61; 'back': 0.62; 'kind': 0.63; 'real': 0.63; 'skip:n 10': 0.64; 'places': 0.64; 'sum': 0.64; 'more': 0.64; 'within': 0.65; 'complex,': 0.84; 'conjugate': 0.84; 'etc,': 0.84; 'sets,': 0.84; 'short,': 0.84; 'type(s)': 0.84; '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:content-transfer-encoding; bh=oSuRO2hdLDZ0xAKTyuK6O7eni8/Atp0vp8fy3RHpt+0=; b=xffok4E2PD92G9CJEC/MdY9rZiIDiydaHF+/SDWShy4NFk+ZJa7q4VD6foGfGk1F7u rjot++ybkBKpw9XMcFPJoRgyH8WqP4CKVg+I5pDtIh4LJtC/jQ3VGYS0nuLmaQdVT/b5 t+aUvveOYHLIjqhDsQveLUyVcswQNP8EIRHSHo+ZFyWpo7TpUBTmKbPu7zzJbColnWXy 3u6iBTKFarajT+DMM4u/oFZD7sc8oy+N/XKInmDdIwurdm4juvWNhxlFrQaCYL7Zo0dz N28hB2FZJCOOQWp7nF1kuO8w2OMJqNpSG66o8HC2DbcPBc0WzyXQv0ZTxXilnCMEaUWE w1Bw== MIME-Version: 1.0 X-Received: by 10.53.8.162 with SMTP id dl2mr11238883vdd.24.1402880573875; Sun, 15 Jun 2014 18:02:53 -0700 (PDT) In-Reply-To: <539e3c95$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <539d0a40$0$29988$c3e8da3$5496439d@news.astraweb.com> <539dbcbe$0$29988$c3e8da3$5496439d@news.astraweb.com> <539e3c95$0$29988$c3e8da3$5496439d@news.astraweb.com> Date: Mon, 16 Jun 2014 11:02:53 +1000 Subject: Re: Python's numeric tower From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402880950 news.xs4all.nl 2882 [2001:888:2000:d::a6]:46176 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73300 On Mon, Jun 16, 2014 at 10:38 AM, Steven D'Aprano wrote: > Mathematically, =E2=84=82 (complex) is a superset of =E2=84=9D (real), an= d Decimals are a > kind of real(ish) number, like float: The Python complex type represents a subset of =E2=84=82. The Python Decima= l and float types implement a subset of =E2=84=9D, which as you say is a subs= et of =E2=84=82. The Python int type implements a subset of =E2=84=A4. (Althou= gh if you have infinite storage, you could theoretically represent all of =E2=84=A4 w= ith int, and possibly all of =E2=84=9D with Decimal. But I don't know of any Python implementation that can utilize infinite RAM.) The question isn't really about the mathematical number sets, but about what operations you can do. The numbers.Complex type specifies (3.4.0): class Complex(Number) | Complex defines the operations that work on the builtin complex type. | | In short, those are: a conversion to complex, .real, .imag, +, -, | *, /, abs(), .conjugate, =3D=3D, and !=3D. >From what I can see, all of those operations are defined for Decimal, *as long as you work exclusively with Decimal*. You can check their .real and .imag (.imag will be Decimal('0'), and .real is self), you can conjugate them (returns self), and you can do arithmetic with them. But you can't mix complex and decimal, any more than you can mix float and decimal: >>> Decimal('2')+3.0 Traceback (most recent call last): File "", line 1, in Decimal('2')+3.0 TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'float' >>> Decimal('2')+complex(3.0) Traceback (most recent call last): File "", line 1, in Decimal('2')+complex(3.0) TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'comple= x' Ergo, currently, you can't say that decimal.Decimal can be treated as a complex. (Although you can call complex(d) and get back a meaningful value, within the limits of precision - again, same as with float(d).) To contrast, numbers.Number places very few requirements on its subclasses. And decimal.Decimal isn't a subclass of any of the rest of the tower: >>> for cls in numbers.__all__: print(cls,"-",isinstance(d,getattr(numbers,cls))) Number - True Complex - False Real - False Rational - False Integral - False As I understand it, isinstance(x,numbers.Complex) should be True for anything that you can treat like a complex() - that is, that you can add it to a complex(), do operations on it, etc, etc, etc. I'm not sure what isinstance(x,numbers.Number) promises in terms of usability; I guess if you have a list of Numbers that are all the same type, you can probably sum them, but you can sum non-Numbers too. The docstring is a bit vague - sure, it's a number, but what can you do with it? ChrisA