in reply to losing precision reading numbers from file

I've done some more investigation, and modified the script to also print the number from memory out after using sprintf. Here's the code:
#sample data set $Day = 0; $Seconds = 35247; $MicroSeconds = 755605; #combine times into one variable $Time = $Day + ($Seconds+$MicroSeconds/1000000)/86400; $sTime = sprintf("%1.20f",$Time); #write this time to a file open(FILE,"> test.txt"); printf FILE ("%1.20f",$Time); close(FILE); #read that time back in from file open(FILE,"< test.txt"); @FileContents = <FILE>; $FileTime = $FileContents[0]; #calculate diffence from time stored in memory and written to file $Difference = $Time - $FileTime; #print results print "\nFrom Memory: "; printf("%1.20f",$Time); print "\nFromsprintf: "; printf("%1.20f",$sTime); print "\nFromFile : "; printf("%1.20f",$FileTime); print "\nDifference : "; printf("%1.20f",$Difference);
This produced the output:
From Memory: 0.40796013431712963000 Fromsprintf: 0.40796013431712957000 FromFile : 0.40796013431712957000 Difference : 0.00000000000000005551
Notice that when I print the variable directly using printf I get the number ending in 63. However, if I use sprintf on that number first, it gets changed to the number ending in 57. Anybody know what is going on?

Replies are listed 'Best First'.
Re^2: losing precision reading numbers from file
by iKnowNothing (Scribe) on Nov 12, 2005 at 00:15 UTC
    thanks for the replies. Looks like if I use binary files I can keep the precision. I was hoping I wouldn't have to go there.