Re: [Python-Dev] PEP 485 review (isclose())

2015-03-03 Thread Victor Stinner
2015-03-03 6:25 GMT+01:00 Chris Barker :
> As far as I can tell, the math module is entirely a C extension. So I can:
> (...)
> 2) Write it in Python, and monkey-patch it in to the math module -- I
> honestly have no idea how to do that, but as I can add a new name to the
> math module after importing it, it should be doable --but I have no idea
> where the code would go.

Maybe it's time to rename the math module to _math and create a
math.py module, like _decimal/decimal? math.py should end with "from
_math import *".

It may be interesting to have Python implementation of math.fsum(),
math.factorial(), math.degrees() and math.radians().

Extract of fsum() comment:

   Full precision summation of a sequence of floats.

   def msum(iterable):
   partials = []  # sorted, non-overlapping partial sums
   for x in iterable:
   i = 0
   for y in partials:
   if abs(x) < abs(y):
   x, y = y, x
   hi = x + y
   lo = y - (hi - x)
   if lo:
   partials[i] = lo
   i += 1
   x = hi
   partials[i:] = [x]
   return sum_exact(partials)

The C implementation of factorial is not naive: "Divide-and-conquer
factorial algorithm" (see the C code).

Obvisouly, a expect lower performances from Python code manipulating
numbers (because of the cost of boxing-unboxing, cost of functions
calls, etc.). But it might help other Python implementations to
implement the math module.

Victor
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 485 review (isclose())

2015-03-03 Thread Guido van Rossum
On Mon, Mar 2, 2015 at 10:48 PM, Chris Barker  wrote:

> On Mon, Mar 2, 2015 at 10:39 PM, Guido van Rossum 
> wrote:
>
> (*) Adding it to the decimal module would require a discussion with
>> Raymond Hettinger, but Decimal users can probably copy and paste the
>> formula from the PEP.
>>
>
> yup -- but maybe worth putting in there while we're at it. though as
> Decimal is arbitrary precision, maybe it's not needed
>

It's not really arbitrary precision, it's decimal *floating* point with a
large but finite precision, so all the same arguments apply.

But my reasoning was more that (at least when I was last involved in the
project) the decimal module tries to stick pretty close to the IEEE 754
standard (maybe nowadays a later version?) and random Python-only additions
are controversial.

-- 
--Guido van Rossum (python.org/~guido)
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 485 review (isclose())

2015-03-03 Thread Ethan Furman
On 03/03/2015 01:17 AM, Victor Stinner wrote:
> 2015-03-03 6:25 GMT+01:00 Chris Barker :
>>
>> As far as I can tell, the math module is entirely a C extension. So I can:

>> 2) Write it in Python, and monkey-patch it in to the math module -- I
>> honestly have no idea how to do that, but as I can add a new name to the
>> math module after importing it, it should be doable --but I have no idea
>> where the code would go.

> Maybe it's time to rename the math module to _math and create a
> math.py module, like _decimal/decimal? math.py should end with "from
> _math import *".

+1

--
~Ethan~



signature.asc
Description: OpenPGP digital signature
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com