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?

In reply to Re: losing precision reading numbers from file by iKnowNothing
in thread 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.