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


in reply to Re^2: Math::FixedPrecision and bignum clash
in thread Math::FixedPrecision and bignum clash

Hi mje,

IMHO your assumption of rounding in Math::FixedPrecision is not true. When you have a look at the constructor of Math::FixedPrecision you'll see the the value 3.145 having 3 digits after the point will be rounded to two digits (what is given as second parameter) with ffround (line 80 of sources).

The effect you see is caused by mathematical (odd) rounding (default of Math::BigFloat) with your example. Have a look at these two examples:

#!/usr/bin/perl use strict; use warnings; use 5.010; use Math::FixedPrecision; my $b = Math::FixedPrecision->new(3.145, 2); say $b; # output: 3.14 my $c = Math::FixedPrecision->new(3.155, 2); say $c; # output 3.16

UPDATE: Added output for the reader.

Regards
McA

Replies are listed 'Best First'.
Re^4: Math::FixedPrecision and bignum clash
by mje (Curate) on Oct 08, 2014 at 15:16 UTC

    Yes, thanks. I was wrong and it is using bankers rounding. Looks at the last digit being kept and if odd rounds down and even rounds up so long as first digit beyond the precision is >= 5. That is another bug in the code we're using here which does not want bankers rounding.