Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Printf/Sprintf Behavior Change

by marinersk (Chaplain)
on Sep 16, 2013 at 18:02 UTC ( #1054317=note: print w/ replies, xml ) Need Help??


in reply to Printf/Sprintf Behavior Change

Everyone else given you the why.

Here's a suggestion towards what to do about it.

  • Rule #{ return int rand 31415927; }: Never let the printf family do your rounding for you.
  • Rule #{ return int rand 31415927; }: Specifically round arithmetically using your own rules and you should at least get consistent results.
  • Rule #{ return int rand 31415927; }: Use the "add half of the next digit" technique if you wish to force 0.0{...}05 to round up.

Examples:

#!/usr/bin/perl -w use strict; my $floatNumber = 0.345; my $roundToTwoDecimals = int($floatNumber * 100) / 100; printf "\$roundToTwoDecimals = %0.02f\n", $roundToTwoDecimals; my $forcedRounding = int(($floatNumber + 0.005) * 100) / 100; printf " \$forcedRounding = %0.02f\n", $forcedRounding; exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-09-16@1955-Rounding>rounding.pl $roundToTwoDecimals = 0.34 $forcedRounding = 0.35


Comment on Re: Printf/Sprintf Behavior Change
Download Code
Re^2: Printf/Sprintf Behavior Change
by McA (Priest) on Sep 16, 2013 at 18:36 UTC
      Nice cheat sheet. Deposited one copy in my back pocket, and thanks for the link.
Re^2: Printf/Sprintf Behavior Change (stupid printf)
by tye (Cardinal) on Sep 16, 2013 at 19:20 UTC

    The problem is NOT that printf is stupid.

    Yes, if you implement your own "round to 2 decimal places" algorithm that doesn't actually round but instead truncates, then you'll (often) get a different answer than if you actually round (whether or not you use sprintf to actually round, which is one of the better choices available).

    Specifically round arithmetically using your own rules and you should at least get consistent results.

    See, that just proves that you didn't learn the lesson that a bunch of the other replies in this thread were trying to teach.

    sub consistentlyRound { my( $w ) = @_; my $p = int( ($w+0.005)*100 ); return $p/100; } my $x = 0.015; my $y = 0.0001; $y += 0.0001 for 1..149; print "x=$x y=$y\n"; printf "x~%.2f y~%.2f (stupid printf rounding)\n", $x, $y; $x = consistentlyRound( $x ); $y = consistentlyRound( $y ); print "x~$x y~$y (consistent arithmetic rounding)\n"; __END__ x=0.015 y=0.015 x~0.01 y~0.01 (stupid printf rounding) x~0.02 y~0.01 (consistent arithmetic rounding)

    It is just that your arithmetic adds more floating point operations that can change the inconsistency! I can find cases where your particular arithmetic happens to be more consistent than sprintf's. Above I show a case where sprintf() is more consistent.

    Now go read the other replies and/or what they link to more carefully and learn why.

    - tye        

      Hello tye,

      Thanks for the working example. You misunderstand some of what I posted, which is exacerbated by my use of nonspecific terminology ("truncation" vs. "rounding" is but one example).

      At any rate, the thread presents a number of very useful references for anyone who wants or needs to understand how floating point operations work.

      I haven't studied the mantissa translation issue for about 35 years; I understood it in a fairly detailed manner then, but have not needed to revisit it.

      Without looking deeper into the issue, I suspect the problem your working example presents involves disparate precision reduction, a working case I have never been presented. All cases (unless my memory is failing, which is a distinct possibility) I have encountered in my career involved identical source- and destination-precision, and I cannot recall the "half next digit" technique ever failing me in that capacity.

      I no longer remember if I proved it sufficient to the need or was merely shown the technique and accepted it. Suffice it is to say that my use of that technique has become so habitual as to defy academic defense.

      Nonetheless, the technical content of your post is wonderful, as always, and I appreciate it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-12-18 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (67 votes), past polls