Calculate Big Number

Dave Angel d at davea.name
Mon Jan 7 21:24:18 EST 2013


(forwarding the private reply to the group)

On 01/07/2013 09:03 PM, Nac Temha wrote:
> Thanks. I using version 2.7 .I want to understand how to handling big
> number. Just  want to know logic.  Without going into further details but I
> want to learn logic of this issue. How to keep datas in python? Using data
> structure? and datas how to handling for calculating?
> 
> 

Since I don't understand your questions, I'll make some guesses.  If
these don't cover it, please reword your questions.

I'll assume you do NOT want internal details of how the python
interpreter manages it.   I'll assume you DO want to know how to write
your python program so that as much accuracy as possible is used in the
calculations.

First comment - as soon as you introduce a floating point (non-integer)
value into the expression, you have the potential of losing precision.
In general, when doing mixed arithmetic, python will convert the result
to float.  Lots can be written (and has been written) about floats,
Decimals, quantization and rounding.  I'm again assuming you're NOT
asking about these.

In Python 2.7, there are two kinds of integers, int and long.  An int is
limited to some processor-specific size, usually 32 or 64 bits.  A long
has no practical limit, though after a few hundred million digits, it
gets pretty memory hungry and piggishly slow.  A long is NOT related to
the C type of the same name.

If you have an integer in your source code that's larger than that
limit, it'll automatically be promoted to long.

If you want to see the type of a particular object, you can use
type(myobj).   For example,

print type(2397493749328734972349873)
will print:     <type 'long'>

If you add, subtract, multiply or ** two int objects, and the result is
too big for an int, it'll automatically be promoted to long.

If you divide two integers with /  the result is floored.  Thus the
result is an integer.  That changes in Python 3, where you can get this
result with //.

If you call some library function on a long, it may or may not be able
to handle it, check the docs.  But in your own code, it's pretty easy to
keep things clean.

If you have more questions, please be specific.

-- 

DaveA



More information about the Python-list mailing list