[ic] [interchange-bugs] [rt.icdevgroup.org #349] Vend::Util::round_to_frac_digits() simplification

Peter peter at pajamian.dhs.org
Tue Mar 20 02:04:18 UTC 2012


On 20/03/12 13:59, Mike Heins wrote:
> Quoting Peter (peter at pajamian.dhs.org):
>> CCing the userlist because I want Mike to see this.
>>
>> On 20/03/12 04:47, Anonymous via RT wrote:
>>> round_to_frac_digits()  produces ".00" when called with (0, 2).  It
>>> also parses the integers incorrectly: in normal Perl "1." is a valid
>>> number, as is "+3", but "-" is not valid.
>>>
>>> Nearly all of the function logic can be replaced with:
>>>
>>> printf "%.*f", $digist, $num;
>>
>> Agreed, except it needs to be sprintf.
>>
>> Mike, is there any reason to not make this change?
> 
> Absolutely. Sprintf doesn't really round.

It does, but there are floating point errors in the rounding:

Correct:
$ perl -le 'print sprintf "%.*f", 2, 0.124'
0.12
$ perl -le 'print sprintf "%.*f", 2, 0.135'
0.14
$ perl -le 'print sprintf "%.*f", 2, -0.124'
-0.12
$ perl -le 'print sprintf "%.*f", 2, -0.135'
-0.14

Wrong (due to floating point errors):
$ perl -le 'print sprintf "%.*f", 2, 0.125'
0.12
$ perl -le 'print sprintf "%.*f", 2, -0.125'
-0.12

We could compensate for those floating point errors like this:
$ perl -le 'print sprintf "%.*f", 2, 0.125 * 1.000000000001'
0.13
$ perl -le 'print sprintf "%.*f", 2, -0.125 * 1.000000000001'
-0.13


Peter



More information about the interchange-users mailing list