Hello, I have run across a problem where I am losing some precision when writing numbers to a file, and then re-reading them for future use. I have captured an example of my problem in the following script:
#sample data set $Day = 0; $Seconds = 35247; $MicroSeconds = 755605; #combine times into one variable $Time = $Day + ($Seconds+$MicroSeconds/1000000)/86400; #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 "\nFromFile : "; printf("%1.20f",$FileTime); print "\nDifference : "; printf("%1.20f",$Difference);
When I ran this on my Windows XP machine, it produced the output:
From Memory: 0.40796013431712963000 FromFile : 0.40796013431712957000 Difference : 0.00000000000000005551
However, when I looked in the "test.txt" file, the number 0.40796013431712963000 was sitting in there. Why is this different from the "FromFile" number? Is there a way to make sure that the number is read exactly as it is from the file? Thanks for the help.

In reply to losing precision reading numbers from file by iKnowNothing

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.