Python Forum
Precision conversion - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Precision conversion (/thread-38435.html)



Precision conversion - garynewport - Oct-12-2022

I have an output file with a series of values. Some of these are high precision values...

1.05200D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 4.99722-235

I want to read these into my Python program, maintaining the level of precision as much as possible.

Most of this is straight forward; I can look for the sign (+ or -) and the D, etc.

However, I am then wondering about how to store the values within Python. Simply taking each mantissa and multiplying it by 10 to the power of the exponent would work, but would Python be capable of holding such a small value as 4.99722-235 or as large as 1.01137D+34 (or larger)?
Always nice to post a question, then solve it because the problem never feels so isolated!

Doing exactly as I have stated and I have maintained the accuracy required.
def convert(oldValue):
	if oldValue.find('D') != -1:
		mantissa = float(oldValue[0: oldValue.find('D')])
	else:
		if oldValue.find('+') != -1:
			mantissa = float(oldValue[0: oldValue.find('+')])
			
		else:
			mantissa = float(oldValue[0: oldValue.find('-')])
			
	if oldValue.find('+') != -1:
		exponent = int(oldValue[oldValue.find('+'):])
	else:
		exponent = int(oldValue[oldValue.find('-'):])
	
	newValue = mantissa * (math.pow(10, exponent))

    return newValue



RE: Precision conversion - deanhystad - Oct-12-2022

Those are not high precision values. There should be no need to do anything special to read the values other than convert the "D" to an "e", and then use float() to convert the modified string to a number.

I think "4.99722-235" is invalid. I would interpret that as 4.99722 minus 235, an equation.
print(convert('9.94376D+31'))
print(convert('4.99722-235'))
print(float('9.94376D+31'.replace('D', 'e')))
print(float('4.99722D-235'.replace('D', 'e')))
Output:
9.94376e+31 4.9972200000000005e-235 9.94376e+31 4.99722e-235



RE: Precision conversion - garynewport - Oct-19-2022

The problem is that the standard output for Fortran is to use the D, except where the exponent is 3 or more digits long; then the D is dropped.

So, in Fortran output 4.99722-235 is equivalent to 4.99722D-235 or 4.99722e-235; and not an equation.

Aside from that (and this only affects the first two lines and only in two results, but these cannot be ignored), your solution is far nicer than mine. Smile