[Python-Dev] pyc files, constant folding and borderline portability issues

Raymond Hettinger python at rcn.com
Mon Apr 6 22:05:37 CEST 2009

> +1 for removing constant folding for floats (besides conversion
> of -<literal>).  There are just too many things to worry about:
> FPU rounding mode and precision, floating-point signals and flags,
> effect of compiler flags, and the potential benefit seems small.

If you're talking about the existing peepholer optimization that
has been in-place for years, I think it would be better to leave
it as-is.  It's better to have the compiler do the work than to
have a programmer thinking he/she needs to do it by hand
(reducing readability by introducing magic numbers).
The code for the lsum() recipe is more readable with a line like:

   exp = long(mant * 2.0 ** 53)

than with

   exp = long(mant * 9007199254740992.0)

It would be ashamed if code written like the former suddenly
started doing the exponentation in the inner-loop or if the code
got rewritten by hand as shown.

The list of "things to worry about" seems like the normal list
of issues associated with doing anything in floating point.
Python is already FPU challenged in that it offers nearly
zero control over the FPU or direct access to signals and flags.
Every step of a floating point calculation in Python gets written-out 
to a PyFloat object and is squeezed back into a C double (potentially
introducing double-rounding if extended precision had be used by
the FPU).  Disabling the peepholer doesn't change this situation.


More information about the Python-Dev mailing list