vishy_acts has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am printing perl float value on screen and in file . On screen value is showing correctly but when I am printing it to a file using printf with 2 precision its giving difference of 0.01 . Sample Code : these are not actual values
my $val = 0.001 ; $val = $val * 100 ; $val = $val / 100 ; print "$val ";
printf( FILEHANDLER "%2.2f" , val );

Replies are listed 'Best First'.
Re: printf rounding issue
by LanX (Saint) on Nov 16, 2016 at 13:34 UTC
      I am doing same thing as mentioned in the thread .
      $val = $val * 100 ; $val = $val / 100;
      This is the reason I am getting correct value when I am printing on screen with print . My issue is why I am getting different values when I am using print and printf . Also what is the solution to print value correctly in FILE .
        > I am doing same thing as mentioned in the thread .
        $val = $val * 100 ; $val = $val / 100;

        from the thread:

        The shift must be a string operation. Dividing by 100 reintroduces the problem with floats!

        edit

        > My issue is why I am getting different values when I am using print and printf

        as Eily already told you, you are rounding in printf at the second position not the third.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re: printf rounding issue
by Eily (Monsignor) on Nov 16, 2016 at 13:36 UTC

    You are printing with 2 digits after the . but the first non zero digit is the third, so of course it can't be displayed. And ++LanX on the two links :)

      vishy_acts,

      To expand on ++Eily's post: you probably need to read more about the print FORMAT specifiers at sprintf. Specifically, the value 2 after the the . in your %2.2f indicates a precision of two (hence Eily's statement, "2 digits after the .")

      Regarding your statement, "Also what is the solution to print value correctly in FILE". You haven't told us what you want the data to look like in FILE, so we're not likely to guess what you mean by "correctly". Do you want all your data with only two digits after the decimal point, which is what your printf currently claims? Then it's already printing correctly. Do you want it to always print 3 digits after the decimal point? %.3f. Do you want printf to always use its default number of digits after the decimal point? %f will use its default (which is typically 6 digits). Do you want the print-style "we've got a secret algorithm to determine how many digits to print"? Then use print FILEHANDLE "$val ", which will use the exact same rules as the print "$val " you're already sending to STDOUT (ie, don't use printf to format the number, since you want perl to choose the format for you). Do you want to use scientific notation with two digits after the decimal place (1.00e-3)? Use %.2e.

      To get it to print correctly, you have to tell perl what 'correctly' means. If you do not know how to tell perl what 'correctly' means, you have to at least tell us what 'correctly' means, so we can help you learn how to tell perl how you want it printed.

Re: printf rounding issue
by AnomalousMonk (Archbishop) on Nov 16, 2016 at 16:48 UTC
Re: printf rounding issue
by ikegami (Patriarch) on Nov 16, 2016 at 21:02 UTC

    1/1000 is a periodic number in binary like 1/3 is in decimal. As such, it can't be represented exactly in a floating point number.

    By the way, FILEHANDLER should be FILEHANDLE. It's a handle (something you hold on to), not a handler (code that handles an event).