Message216253
Python's `decimal.Decimal` doesn't seem to like taking modulo or intdiv of large Decimals by integers (where "large" depends on how many digits are internally stored).
>>> from decimal import *
>>> getcontext().prec
28
>>> Decimal(10**29)%1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.DivisionImpossible'>]
>>> getcontext().prec=50
>>> Decimal(10**29)%1
Decimal('0')
Same for `Decimal(10**29)//1`
This is a logical problem: "Alice has a 100-digit number which begins with 1543256. What is the last digit?"
But I found the error hard to understand. Searching for "DivisionImpossible" didn't turn up anything immediate (it wasn't added to the official docs?). I was most of the way through writing out a question to StackOverflow when it clicked. (Also, why is it an InvalidOperation that holds an exception as a message? Never saw that before.)
Suggestions:
- Improve docs with further examples. The examples of InvalidOperation are logical MATH errors (e.g. division by 0), not logical PROGRAMMING errors.
- Uh, maybe the error message could be changed to something like, "The answer you seek lies beyond the mantissa." Or more sanely, "Not enough precision to compute the answer." Or something else that hints to me to look into precision. |
|
Date |
User |
Action |
Args |
2014-04-15 00:02:39 | leewz | set | recipients:
+ leewz, docs@python |
2014-04-15 00:02:39 | leewz | set | messageid: <1397520159.39.0.440264883295.issue21227@psf.upfronthosting.co.za> |
2014-04-15 00:02:39 | leewz | link | issue21227 messages |
2014-04-15 00:02:38 | leewz | create | |
|