[Python-Dev] Re: [spambayes-dev] RE: RE: [Spambayes] Question (orpossibly a bug report) (original) (raw)

Scott David Daniels Scott.Daniels@Acm.Org
Fri, 25 Jul 2003 08:55:33 -0700


Tim Peters wrote:

[Scott David Daniels]

I've been seeing a lot of "we'll have to use float(123)/100" messages floating around, and I'd like to point out there is an atof-able form for floating point numbers: 256.7654e-7 = 2567654e-11

Skip is right that this won't help. At compile time, Python doesn't stuff pieces of the source code into a .pyc file, it builds the float objects and marshals them. Here's temp.py: def example(): return 1e-1 Then:

import temp file('temp.pyc', 'rb').read()

';\xf2\r\n\xf1L!?c\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00 @\x00\x00\x00s\r\x00\x00\x00d\x00\x00\x84\x00\x00Z\x00\x00d\x01 \x00S(\x02\x00\x00\x00c\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\x00C\x00\x00\x00s\x08\x00\x00\x00d\x01\x00Sd\x00\x00S(\x02 \x00\x00\x00Nf\x130.10000000000000001(\x00\x00\x00\x00(\x00\x00 \x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s\x07\x00\x00\x00temp.py s\x07\x00\x00\x00example\x01\x00\x00\x00s\x02\x00\x00\x00\x00 \x01N(\x01\x00\x00\x00s\x07\x00\x00\x00example(\x01\x00\x00\x00 s\x07\x00\x00\x00example(\x00\x00\x00\x00(\x00\x00\x00\x00 s\x07\x00\x00\x00temp.pys\x01\x00\x00\x00?\x01\x00\x00\x00s\x00 \x00\x00\x00' The substring f\x130.10000000000000001 is the marshal typecode for a float, a byte saying the float string is in the next 0x13 = 19 bytes, and then the 19-byte string "0.10000000000000001". The source code's exponential notation didn't survive in the .pyc file. The idea is to change the unmarshal code to transform the 19-byte string to the 24-byte string '010000000000000001e-17' before calling atof. I suppose the marshal code could be converted to generate it, but I was suggesting a way of reading the current format by fiddling bytes before calling atof.

-Scott David Daniels