Mailing List Archive

Re: [interchange-bugs] [rt.icdevgroup.org #349] Vend::Util::round_to_frac_digits() simplification
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?


Peter

_______________________________________________
interchange-users mailing list
interchange-users@icdevgroup.org
http://www.icdevgroup.org/mailman/listinfo/interchange-users
Re: [interchange-bugs] [rt.icdevgroup.org #349] Vend::Util::round_to_frac_digits() simplification [ In reply to ]
Quoting Peter (peter@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.

--
Mike Heins
Perusion -- Expert Interchange Consulting http://www.perusion.com/
phone +1.765.253.4194 <mike@perusion.com>

Nature, to be commanded, must be obeyed. -- Francis Bacon

_______________________________________________
interchange-users mailing list
interchange-users@icdevgroup.org
http://www.icdevgroup.org/mailman/listinfo/interchange-users
Re: [interchange-bugs] [rt.icdevgroup.org #349] Vend::Util::round_to_frac_digits() simplification [ In reply to ]
On 20/03/12 13:59, Mike Heins wrote:
> Quoting Peter (peter@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

_______________________________________________
interchange-users mailing list
interchange-users@icdevgroup.org
http://www.icdevgroup.org/mailman/listinfo/interchange-users
Re: [interchange-bugs] [rt.icdevgroup.org #349] Vend::Util::round_to_frac_digits() simplification [ In reply to ]
Quoting Peter (peter@pajamian.dhs.org):
> On 20/03/12 13:59, Mike Heins wrote:
> > Quoting Peter (peter@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

Do:

perldoc -q round

to see why I did it that way...we should just fix what
problems there are.

--
Mike Heins
Perusion -- Expert Interchange Consulting http://www.perusion.com/
phone +1.765.253.4194 <mike@perusion.com>

The sun, with all those planets revolving around it and
dependent on it, can still ripen a bunch of grapes as if
it had nothing else in the universe to do. -- Galileo

_______________________________________________
interchange-users mailing list
interchange-users@icdevgroup.org
http://www.icdevgroup.org/mailman/listinfo/interchange-users