Subject: | Rounding to a positive precision is off by one digit |
Rounding to a positive precision seems to be off by one digit. I dug into the history, and discovered that this bug seems to have been introduced in the version of Math::BigFloat that shipped with perl 5.7.3.
In the versions of Math::BigFloat that shipped with Perl up until Perl 5.7.1, a comment in the code says "round $x at the 10 to the $scale digit place". This is the common convention, although som swap the sign of $scale, so a higher value of $scale means higher precision. The behaviour matched the comment in the code, so $scale = 2 rounds to nearest 100, since 10**2 = 100:
$ perl -I/path/to/perl-5.7.1/lib -MMath::BigFloat -wle 'print Math::BigFloat -> new("12345") -> ffround(2)'
+123E+2
The version of Math::BigFloat that shipped with Perl 5.7.2 has a different comment saying "precision: round to the $Nth digit left (+$n) or right (-$n) from the '.'", which is not the same as "round $x at the 10 to the $scale digit place". The actual behaviour was unchanged, though:
$ perl -I/path/to/perl-5.7.2/lib -MMath::BigFloat -wle 'print Math::BigFloat -> new("12345") -> ffround(2)'
12300
In the version of Math::BigFloat that shipped with Perl 5.7.3, the behaviour changed to match the comment "precision: round to the $Nth digit left (+$n) or right (-$n) from the '.'". Now a positive $scale really seems to mean "count digits to the left of the dot", so $scale = 2 means round to the second digit to the left of the dot:
$ perl -I/path/to/perl-5.7.3/lib -MMath::BigFloat -wle 'print Math::BigFloat -> new("12345") -> ffround(2)'
12340
This has been the behaviour ever since. Did someone misunderstand what precision is, believing that the old behaviour was wrong?