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

Stefan Hornburg via RT interchange at rt.icdevgroup.org
Wed Mar 21 10:57:08 UTC 2012


<URL: http://rt.icdevgroup.org/Ticket/Display.html?id=349 >

Comments from the mailing list:

Quoting Peter (peter at pajamian.dhs.org):
> > 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
Do:

	perldoc -q round

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

-- Mike Heins Perusion 

As was just discussed with Mike on the mailing list, rounding with
sprintf (indeed any rounding that relies on floating point arithmetic)
has issues with floating point errors and should not be relied upon.  I
think it's best to just fix the issues pointed out with the current
function.


Peter





More information about the interchange-bugs mailing list