PDA

View Full Version : float to string



hcrisp
03-02-2011, 11:10 AM
How can I convert a 32-bit float scalar value to string and ensure that no precision is long when it is converted back to float?



x = 8.333333
print, x EQ float(string(x))
; 0 ; False
print, x EQ float(string(x, format='(F30.20)'))
; 1 ; True
; But very large or small numbers may fail
x = 1E-30
print, x EQ float(string(x, format='(F30.20)'))
; 0 ; False
x = 1E30
print, x EQ float(string(x, format='(F30.20)'))
;%%%Type conversion error: Unable to convert given STRING to BYTE.
;%%%Detected at: $MAIN$ (FLOAT).
; 0 ; False


Use scientific notation format?

hcrisp
03-02-2011, 11:16 AM
This format does better. Are there any cases that would fail?



x = 1E30
print, x EQ float(string(x, format='(E30.20)'))
; 1 ; True
x = 1E-30
print, x EQ float(string(x, format='(E30.20)'))
; 1 ; True

rwagner
03-24-2011, 12:00 PM
I know of no cases where this would fail.