I better do a write up to summarise all four methods
using Printf
function SavingFloat_method01(filename,x)
write(filename, x)
end
function RestoringFloat_method01(filename)
read(filename,Float64)
end
function SavingFloat_method02(filename,x)
write(filename, string(x))
end
function RestoringFloat_method02(filename)
parse(Float64,read(filename,String))
end
function SavingFloat_method03(filename,x)
write(filename, @sprintf("%a",x))
end
function RestoringFloat_method03(filename)
parse(Float64,read(filename,String))
end
function SavingFloat_method04(filename,x)
write(filename, htol(x))
end
function RestoringFloat_method04(filename)
ltoh(read(filename,Float64))
end
function read_rawbytes(filename)
IO = open(filename,"r")
buff = UInt8[]
while !eof(IO)
push!(buff,read(IO,UInt8))
end
buff
end
""" Value to be saved """
n = sqrt(2.0)
""" Framework for testing """
function framework_test(save_func,restore_func,version)
save_func("SavedValue_method$(version)",n)
y = undef
y = restore_func("SavedValue_method$(version)")
println("Method$(version): y === n is ",y === n," y=",y," n=",n)
rawbytes = read_rawbytes("SavedValue_method$(version)")
# println("typeof(rawbytes) is ",typeof(rawbytes))
print("rawbytes: ")
for k in rawbytes
print(string(k,base=16)," ")
end
println()
end
"""
Testing method 01
"""
framework_test(SavingFloat_method01,
RestoringFloat_method01,
"01")
"""
Testing method 02
"""
framework_test(SavingFloat_method02,
RestoringFloat_method02,
"02")
"""
Testing method 03
"""
framework_test(SavingFloat_method03,
RestoringFloat_method03,
"03")
"""
Testing method 04
"""
framework_test(SavingFloat_method04,
RestoringFloat_method04,
"04")
The output is
Method01: y === n is true y=1.4142135623730951 n=1.4142135623730951
rawbytes: cd 3b 7f 66 9e a0 f6 3f
Method02: y === n is true y=1.4142135623730951 n=1.4142135623730951
rawbytes: 31 2e 34 31 34 32 31 33 35 36 32 33 37 33 30 39 35 31
Method03: y === n is true y=1.4142135623730951 n=1.4142135623730951
rawbytes: 30 78 31 2e 36 61 30 39 65 36 36 37 66 33 62 63 64 70 2b 30
Method04: y === n is true y=1.4142135623730951 n=1.4142135623730951
rawbytes: cd 3b 7f 66 9e a0 f6 3f