LOADING DATA INTO ARRAYS

John Machin sjmachin at lexicon.net
Thu Jul 10 18:50:50 EDT 2003


Skip Montanaro <skip at pobox.com> wrote in message news:<mailman.1057776255.28630.python-list at python.org>...

>     satish> ValueError: invalid literal for float(): 0.00000000000000D+000
> 
> It's been many years since I programmed any Fortran, so I'm no longer sure
> what the 'D' notation stands for (double-precision?).  As a first guess, I'd
> suggest you change the 'D's to 'E's:
> 
>     >>> line.replace('D', 'E').split()
>  ['1.00000000000000', '0.00000000000000E+000', '0.00000000000000E+000']
>     >>> map(float, line.replace('D', 'E').split())
>     [1.0, 0.0, 0.0]

Perhaps we have a version or platform difference here ... 2.2.3 on
win32 works just fine (see below)

C:\junk>python
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> float('1.0d0')
1.0
>>> float('1.0D0')
1.0
>>> float('2.0D3')
2000.0
>>> float('0.00000000000000D+000')
0.0
>>>

Seeing guessing is tolerated (at least in this thread): Python float()
is defined in terms of the now-deprecated string.atof() which (I
guess) is/are implemented by calling C atof() which in K&R 2 is
defined in terms of strtod() which is not defined with any degree of
precision as to what constitutes legal input -- some implementors of C
libraries may well have confused the two separate concepts of valid
*data* input and valid *code* input.

Satish, for amusement: see what float('2.0e3f') gives you [that's a
legal float constant in C]. For practical results, follow Skip's
advice to replace 'D' with 'E'.

Cheers,
John




More information about the Python-list mailing list