http://www.perlmonks.org?node_id=1054329


in reply to Re: Printf/Sprintf Behavior Change
in thread Printf/Sprintf Behavior Change

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