Re: long number multiplication

2004-12-06 Thread Christopher A. Craig
"I.V. Aprameya Rao" <[EMAIL PROTECTED]> writes:

> i have been wondering, how does python store its very long integers and 
> perform aritmetic on it.

In CPython, A long integer is a combination of a signed word
indicating the sign and the size and an unsigned array of N words
where N is the absolute value of the size which contain the value of
the integer in base 32768.

Most operations are done in their simplest fashion (see Knuth's
SemiNumerical Algorithms chapter 4.3) with the exception of multiply,
which uses Karatsuba math for inputs greater than 35 base 15 bit
digits.

> i needed to implement this myself and was thinking of storing the digits 
> of an integer in a list.

That's sort of what Python does except the "digits" are 15 bits,
not base 10.  Doing it in base 10 would be a huge pain because of the
problems with base 10->base 2 conversion.

-- 
Christopher A. Craig <[EMAIL PROTECTED]>
"When all else fails, read the instructions."

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: results of division

2004-12-09 Thread Christopher A. Craig
"Paul McGuire" <[EMAIL PROTECTED]> writes:

> Errr?  How come round() is able to understand 1.775 correctly, whereas
> string interp is not?  I'm guessing that round() adds some small epsilon to
> the value to be rounded, or perhaps even does the brute force rounding I
> learned in FORTRAN back in the 70's:  add 0.5 and truncate.  But this would
> still truncate 1.77999 to two places, so this theory fails also.  What
> magic is round() doing, and should this same be done in the string interp
> code?

Consulting bltinmodule.c would tell you.  round(x,n) in (Python 2.4):

multiplies x by 10**n
adds .5
truncates
divides by 10**n.

Don't confuse this trick with giving us the correct result though,
it's still floating point:

>>> round(1.77499, 2)
1.78

-- 
Christopher A. Craig <[EMAIL PROTECTED]>
"[Windows NT is] not about 'Where do you want to go today?'; it's more like
'Where am I allowed to go today?'" -- Mike Mitchell, NT Systems Administrator

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A rational proposal

2004-12-20 Thread Christopher A. Craig
I've been thinking about doing this for a while.  cRat
(http://sf.net/projects/pythonic) already meets these qualifications
except that I need to add decimal support to it now that decimals are
in the language.  I could rewrite the existing code in Python (it's
currently in C), but there are some very real performance reasons to
do it in C rather than Python (i.e. I'm manipulating the internals of
the numerator and denominator by hand for performance in the GCD
function)

-- 
Christopher A. Craig <[EMAIL PROTECTED]>
"I affirm brethren by the boasting in you which I have in Christ Jesus
our Lord, I die daily" I Cor 15:31 (NASB)

-- 
http://mail.python.org/mailman/listinfo/python-list