Q: About a programming problem with Python!
Tim Peters
tim.one at comcast.net
Mon Jan 8 14:09:02 EST 2007
[followups set to comp.lang.python]
[Danny]
> I am just getting into OOP using Python and
> because of the floating point of large integer (L)
> square roots all the decimal expansions are truncated.
> This has created a problem because I need these
> decimal expansions in my algorithm other wise
> it adds more sub iterations slowing down
> greatly to the programs performance.
>
> Is there any way of retrieving these truncated decimal
> expansions up to a certain (n) with Python?
You want comp.lang.python for this, not sci.math. Be sure to give a
concrete example, giving a specific input and showing exactly as
possible what you /want/ to achieve. There are so many possible
ambiguities in what you wrote here that few people will take time to
guess at what you might have intended to ask.
> Java and I believe c++ do not have this problem
> but I am not about to learn any of these because
> I like Python.
Programming language has little to nothing to do with this, it's the
data type you're using. In Java, C++, and Python, if you're using the
native double-precision floating-point type, you're limited to (on most
machines) at most 53 bits of precision.
If you need more than that, you need to use a different data type. For
example, in recent versions of Python you could use the `decimal`
module, and set its precision to virtually anything you like; e.g.,
under Python 2.5,
>>> import decimal
>>> t = decimal.Decimal(2)
>>> decimal.getcontext().prec # show the default precision
28
>>> print t.sqrt() # sqrt(2) rounded to 28 significant digits
1.414213562373095048801688724
>>> decimal.getcontext().prec = 56 # double the precision
>>> print t.sqrt() # sqrt(2) to rounded to 56 significant digits
1.4142135623730950488016887242096980785696718753769480732
More information about the Python-list
mailing list