extend methods of decimal module

Mark H. Harris harrismh777 at gmail.com
Thu Feb 27 23:41:09 EST 2014


On Thursday, February 27, 2014 9:15:36 PM UTC-6, Steven D'Aprano wrote:


> Decimal uses base 10, so it is a better fit for numbers we 
> write out in base 10 like "0.12345", but otherwise it suffers from the 
> same sort of floating point rounding issues as floats do.
> 

> 
> py> Decimal('1.2345').as_tuple()
> DecimalTuple(sign=0, digits=(1, 2, 3, 4, 5), exponent=-4)
> 
> py> Decimal('1234.5').as_tuple()
> DecimalTuple(sign=0, digits=(1, 2, 3, 4, 5), exponent=-1)
> 

Steven, thank you, your explanation here is splendid, and has cleared up some of my confusion about Decimal, and given me a tool besides ('preciate it !)   I did not investigate .as_tuple()   nice.

Take a look at this:  From IDLE

>>> from decimal import *
>>> s=Decimal(.1)
>>> s.as_tuple()
DecimalTuple(sign=0, digits=(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 1, 1, 1, 5, 1, 2, 3, 1, 2, 5, 7, 8, 2, 7, 0, 2, 1, 1, 8, 1, 5, 8, 3, 4, 0, 4, 5, 4, 1, 0, 1, 5, 6, 2, 5), exponent=-55)
>>>
>>> s=Decimal('.1')    <===== .1 as string
>>> s.as_tuple()
DecimalTuple(sign=0, digits=(1,), exponent=-1)
>>> 

Big difference, yes?    You have hit the nail on the head, because as you say, it is very unfortunate that the function does not know whether it has been typed in by hand (a big problem) or whether it comes from an intermediate calculated result (maybe an even bigger problem.   rats().

So, I am thinking I need to mods...   maybe an idmath.py for interactive sessions, and then dmath.py for for running within my scientific scripts...  ??

Thanks for your input.

kind regards,






More information about the Python-list mailing list