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


Groups > comp.lang.python > #83395 > unrolled thread

Re: Decimals and other numbers

Started byDave Angel <davea@davea.name>
First post2015-01-08 21:43 -0500
Last post2015-01-09 08:45 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Decimals and other numbers Dave Angel <davea@davea.name> - 2015-01-08 21:43 -0500
    Re: Decimals and other numbers Marko Rauhamaa <marko@pacujo.net> - 2015-01-09 08:45 +0200

#83395 — Re: Decimals and other numbers

FromDave Angel <davea@davea.name>
Date2015-01-08 21:43 -0500
SubjectRe: Decimals and other numbers
Message-ID<mailman.17499.1420771441.18130.python-list@python.org>
On 01/08/2015 09:33 PM, Devin Jeanpierre wrote:
> I noticed some very PHP-ish behavior today:
>
>>>> import decimal
>>>> x = 0
>>>> y = float(x)
>>>> z = decimal.Decimal(x)
>>>> x == y == z == x
> True
>>>> x ** x
> 1
>>>> y**y
> 1.0
>>>> z**z
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "/usr/lib/python2.7/decimal.py", line 2216, in __pow__
>      return context._raise_error(InvalidOperation, '0 ** 0')
>    File "/usr/lib/python2.7/decimal.py", line 3872, in _raise_error
>      raise error(explanation)
> decimal.InvalidOperation: 0 ** 0
>
>
> I'd file a bug report but I'm anticipating some rational (heh)
> explanation. Any ideas?
>
> Python 3.4 also raises this exception, but the error message is less
> informative. ("decimal.InvalidOperation: [<class
> 'decimal.InvalidOperation'>]").


What you don't say is which behavior you actually expected.  Since 0**0 
is undefined mathematically, I'd expect either an exception or a NAN result.


-- 
DaveA

[toc] | [next] | [standalone]


#83404

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-01-09 08:45 +0200
Message-ID<87egr41mhb.fsf@elektro.pacujo.net>
In reply to#83395
Dave Angel <davea@davea.name>:

> What you don't say is which behavior you actually expected. Since 0**0
> is undefined mathematically, I'd expect either an exception or a NAN
> result.

IEEE 754 mandates that 0**0 should evaluate to 1:

   <URL: http://en.wikipedia.org/wiki/NaN#Operations_generating_NaN>

    The standard pow function and the integer exponent pown function
    define 0**0, 1**∞, and ∞**0 as 1.

    The powr function defines all three indeterminate forms as invalid
    operations and so returns NaN.

Should IEEE 754's anomalies spill over to decimal, is a different
question.


Marko

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web