unusual exponential formatting puzzle

N

Neal Becker

Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

That is, the leading digit is always 0, instead of the first significant
digit. It is fixed width. I can almost get it with '% 16.9E', but not
quite.

My solution is to print to a string with the '% 16.9E' format, then parse it
with re to pick off the pieces and fix it up. Pretty ugly. Any better
ideas?
 
M

mensanator

Neal said:
Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

That is, the leading digit is always 0, instead of the first significant
digit. It is fixed width. I can almost get it with '% 16.9E', but not
quite.

My solution is to print to a string with the '% 16.9E' format, then parse it
with re to pick off the pieces and fix it up. Pretty ugly. Any better
ideas?

If you have gmpy available...

....and your floats are mpf's...
mpf('3.14159265358979323846e0',64)

....you can use the fdigits function

....to create a seperate digit string and exponent...
('31415927', 1, 64)

....which can then be printed in the desired format.
print "0.%sE%02d" % (t[0],t[1])
0.31415927E01
 
M

mensanator

Neal said:
Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

That is, the leading digit is always 0, instead of the first significant
digit. It is fixed width. I can almost get it with '% 16.9E', but not
quite.

My solution is to print to a string with the '% 16.9E' format, then parse it
with re to pick off the pieces and fix it up. Pretty ugly. Any better
ideas?

If you have gmpy available...

...and your floats are mpf's...
mpf('3.14159265358979323846e0',64)

...you can use the fdigits function

...to create a seperate digit string and exponent...
('31415927', 1, 64)

...which can then be printed in the desired format.
print "0.%sE%02d" % (t[0],t[1])
0.31415927E01

Unless your numbers are negative.
print "0.%sE%02d" % (t[0],t[1])
0.-31415927E03

Drat. Needs work.



And does the format permit large negative exponents (2 digits + sign)?
print "0.%sE%02d" % (t[0],t[1])
0.31415927E-13
 
N

Neal Becker

Neal said:
Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

That is, the leading digit is always 0, instead of the first
significant
digit. It is fixed width. I can almost get it with '% 16.9E', but not
quite.

My solution is to print to a string with the '% 16.9E' format, then
parse it
with re to pick off the pieces and fix it up. Pretty ugly. Any better
ideas?

If you have gmpy available...
import gmpy

...and your floats are mpf's...
s = gmpy.pi(64)
s
mpf('3.14159265358979323846e0',64)

...you can use the fdigits function
t = gmpy.fdigits(s,10,8,0,0,2)

...to create a seperate digit string and exponent...
('31415927', 1, 64)

...which can then be printed in the desired format.
print "0.%sE%02d" % (t[0],t[1])
0.31415927E01

Unless your numbers are negative.
print "0.%sE%02d" % (t[0],t[1])
0.-31415927E03

Drat. Needs work.



And does the format permit large negative exponents (2 digits + sign)?

I think the abs (exponent) < 10 for now
print "0.%sE%02d" % (t[0],t[1])
0.31415927E-13
 
P

Paul Rubin

Neal Becker said:
Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

Yeah, that was normal with FORTRAN.
My solution is to print to a string with the '% 16.9E' format, then
parse it with re to pick off the pieces and fix it up. Pretty ugly.
Any better ideas?

That's probably the simplest.
 
N

Neal Becker

Paul said:
Yeah, that was normal with FORTRAN.


That's probably the simplest.

Acutally, I found a good solution using the new decimal module:
def Format(x):
"""Produce strange exponential format with leading 0"""
s = '%.9E' % x

d = decimal.Decimal (s)
(sign, digits, exp) = d.as_tuple()


s = ''
if (sign == 0):
s += ' '
else:
s += '-'

s += '0.'

e = len (digits) + exp
for x in digits:
s += str (x)
s += 'E'
s += '%+03d' % e

return s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top