Subject: | Unexpected behavior of precision(P) vs. new(,,P) |
Date: | Sat, 22 Jul 2017 15:12:52 -0600 |
To: | bug-Math-BigFloat [...] rt.cpan.org |
From: | Glenn Golden <gdg [...] zplane.com> |
Greetings BigFloatoids,
The inline example below gives the following result on my setup:
Perl version v5.24.1
Math::BigFloat version 1.999715
Precision set in new():
x(P=-5): 1234.01000
x(P=-4): 1234.0100
x(P=-3): 1234.010
x(P=-2): 1234.01
x(P=-1): 1234.0
x(P=+0): 1234
x(P=+1): 1234
x(P=+2): 1230
x(P=+3): 1200
x(P=+4): 1000
x(P=+5): 0
Precision set explicitly after new():
x(P=-5): 1234.01000
x(P=-4): 1234.0100
x(P=-3): 1234.010
x(P=-2): 1234.01
x(P=-1): 1234.0
x(P=+0): 1234.01 <== My expectation: '1234'
x(P=+1): 1234
x(P=+2): 1230
x(P=+3): 1200
x(P=+4): 1000
x(P=+5): 0
All is as expected, except for the P=0 case in the second loop, where
the precision is set explicitly. Is there a reason why the two cases
behave differently?
A list of loaded modules from the above example is also attached.
======================== begin inline script =============================
#!/usr/bin/perl
#
# Minimal example showing unexpected (to me) difference in reported BigFloats
# created via new() with a given precision, vs. creation via new() with
# no precision specified, but then setting the precision afterward via
# precision().
#
use strict;
use warnings FATAL => 'all';
use Math::BigFloat;
print "Perl version $^V\n";
print "Math::BigFloat version $Math::BigFloat::VERSION\n";
#
# Set precision in new(): All results as expected
#
printf "\n";
printf "Precision set in new():\n";
for (my $prc = -5; $prc <= 5; ++$prc)
{
my $x = Math::BigFloat->new("1234.01", undef, $prc);
printf " x(P=%+2d): $x\n", $prc;
}
#
# Set precision after new(): Result with P = 0 is 1234.01, expectation
# is 1234.
#
printf "\n";
printf "Precision set explicitly after new():\n";
for (my $prc = -5; $prc <= 5; ++$prc)
{
my $x = Math::BigFloat->new("1234.01");
$x->precision($prc);
printf " x(P=%+2d): $x\n", $prc;
}
======================== end inline script =============================
Message body is not shown because sender requested not to inline it.