Decimal 0**0

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Feb 5 07:26:02 EST 2013


Does anyone have an explanation why Decimal 0**0 behaves so differently from
float 0**0?

Tested in both Python 2.7 and 3.3, float 0**0 returns 1, as I would expect:

py> 0.0**0.0  # floats return 1
1.0


With Decimals, if InvalidOperation is trapped it raised:

py> from decimal import getcontext, InvalidOperation
py> from decimal import Decimal as D
py> getcontext().traps[InvalidOperation] = True
py> D(0)**D(0)  # Decimals raise
Traceback (most recent call last):
  ...
decimal.InvalidOperation: 0 ** 0


or return a NAN:

py> getcontext().traps[InvalidOperation] = False
py> D(0)**D(0)
Decimal('NaN')


I am familiar with the arguments for treating 0**0 as 0, or undefined, but
thought that except for specialist use-cases, it was standard practice for
programming languages to have 0**0 return 1. According to Wikipedia, the
IEEE 754 standard is for "pow" to return 1, although languages can define a
separate "powr" function to return a NAN.

http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_power_of_zero


I suspect this is a bug in Decimal's interpretation of the standard. Can
anyone comment?




-- 
Steven




More information about the Python-list mailing list