Floating-point multiplication

Tim Peters tim.one at comcast.net
Mon Feb 4 21:46:35 EST 2002


[Alex Holkner]
> I'm experiencing discrepencies in floating point multiplication
> between the Python interpreter and embedded python.

I don't know what "embedded python" means to you, and the answer is probably
crucial.

> Running Python 2.2 under Win2k, compiling with VC++.  The interpreter
> gives:
>
>   >>> print sys.version
>   '2.2 (#28, Jan  4 2002, 19:01:19) [MSC 32 bit (Intel)]'
>   >>> print 0.785398163397 * 4.0
>   3.14159265359
>   >>> print math.pi
>   3.14159265359
>
> Which is expected.  Under my embeddeded python, however:
>
>   print sys.version
>     '2.2 (#28, Jan  4 2002, 19:01:19) [MSC 32 bit (Intel)]'
>   print 0.785398163397 * 4.0
>     3.14159274101
>   print math.pi
>     3.14159274101
>
> Which is quite wrong,

It's actually correct, provided that fp arithmetic is being done in IEEE
single precision (instead of the IEEE double precision Python normally
uses).  Somehow or other your Pentium FPU has gotten wedged into 24-bit
"precision control" mode.  Core Python doesn't muck with the FPU control
registers, so something outside of core Python you're doing, or calling, is
doing the damage.  Scour the sources for calls to control87, _control87,
controlfp or _controlfp.  If you find exactly one such call, it's wrong
<wink>.

Sometimes using a buggy device driver can do this to you too; for some
unknown reason, printer drivers are notorious for screwing up the FPU state
(they change it internally for a legitimate purpose, but neglect to restore
the FPU control state upon exit).





More information about the Python-list mailing list