为什么
decimal.Decimal(0)**decimal.Decimal(0)
不返回1而是一个错误?import decimal
decimal.Decimal(0)**5 # works and returns Decimal('0') as it should
decimal.Decimal(0)**0 # doesn't not work and should return 1
decimal.Decimal(0)**decimal.Decimal(0) # same
0**0 # works and returns 1
我可以使用if
语句来绕过错误(decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
),但这样做看起来很不优雅。
编辑:我在学校一直学习0的0次方等于1,但是(见评论)实际上它不是。所以如果我想让它成为1,我猜我需要手动设置(在我的情况下这是期望的行为),我不知道有争议关于它的值。