in reply to Re^4: Why am I losing accuracy?
in thread Why am I losing accuracy?

I don't mean to put pressure - I just leave this here for anyone.

both of these fail with an infinite loop printing i=10 on linux fedora 30, perl 5.28.2. 10 and 15 are the min numbers to do this.

use bignum (p=>1); for(my $i = 1;$i->blt(15);$i->binc()){ print "i=$i\n"; }
for(my $i = 1;$i<=10;$i++){ print "i=$i\n"; }

this shows to me that it is not the loop or the overloaded increment but that when p=>1 it thinks that it's adding that value divided by 10 or something

my $i = 10; print "1 i=$i\n"; $i++; print "2 i=$i\n"; $i = $i + 1; print "3 i=$i\n"; $i = $i + 2; print "4 i=$i\n"; $i = $i + 3; print "5 i=$i\n"; $i = $i + 4; print "6 i=$i\n"; $i = $i + 5; print "7 i=$i\n"; $i = $i + 6 ; print "8 i=$i\n";
1 i=10 2 i=10 3 i=10 4 i=10 5 i=10 6 i=10 7 i=20 8 i=30

I have filed a bug to bigint

Replies are listed 'Best First'.
Re^6: Why am I losing accuracy?
by bliako (Abbot) on Jan 11, 2020 at 12:07 UTC

    The maintainer of bigint replied to filing a bug:

    Here is your problem: p=>1 specifies a precision of 1. See these for comparisation: $ perl -Mbignum=p,1 -wle '$i = 1; print $i + 123456788,"\n"' 123456790 $ perl -Mbignum=p,2 -wle '$i = 1; print $i + 123456788,"\n"' 123456800 $ perl -Mbignum=p,5 -wle '$i = 1; print $i + 123456788,"\n"' 123500000 Looks to me it works as designed. Here is the same with different accuracy values: $ perl -Mbignum=a,1 -wle '$i = 1; print $i + 123456788,"\n"' 100000000 $ perl -Mbignum=a,2 -wle '$i = 1; print $i + 123456788,"\n"' 120000000 $ perl -Mbignum=a,3 -wle '$i = 1; print $i + 123456788,"\n"' 123000000 and with negative values of p: $ perl -Mbignum=p,-3 -wle '$i = 1; print $i + 123456789.1,"\n"' 123456790.100 $ perl -Mbignum=p,-6 -wle '$i = 1; print $i + 123456789.1,"\n"' 123456790.100000 Best regards, Tels

      I am thinking to reply with maybe bignum should be put under the ACME tree? but holding back thinking I am too extreme. I still can't digest that forloop behaviour. Fair enough it's a design choice but the unawary, common-sense Perl user is in for a big surprise when trying to ... increase precision.

        Fair enough it's a design choice but the unawary, common-sense Perl user is in for a big surprise when trying to ... increase precision.

        I'm a little confused (about the whole subthread, actually, as well as this one) - isn't p=>1 explicitly asking for reduced precision?