Oct-12-2022, 11:49 AM
I have an output file with a series of values. Some of these are high precision values...
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 (
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
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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 |