Skip Menu |

This queue is for tickets about the Math-Int2Base CPAN distribution.

Report information
The Basics
Id: 64123
Status: open
Priority: 0/
Queue: Math-Int2Base

People
Owner: Nobody in particular
Requestors: desoto [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.04
Fixed in: (no value)



Subject: Large integers break
I like this module. It's a lot cleaner and clearer than Math::BaseCnv. However, I *think* Math::BaseCnv handles larger numbers properly, whereas Math::Int2Base does not. On my 64bit server using perl5.12.2, both modules output identical data (when converting base 10 to base 62) from 1 to 100000000000000071. But after that the output does not match. I tested with this loop: use Math::BaseCnv; use Math::Int2Base qw/ int2base base2int/; for (my $i = $ARGV[0]; $i < $i + 1; $i++) { my $bc1 = cnv($i, 10, 62); my $ib1 = int2base($i, 62); my $bc2 = cnv($bc1, 62, 10); my $ib2 = base2int($ib1, 62); say ("$bc1 : $ib1"); say ("$bc2 : $ib2"); last unless (($bc1 eq $ib1) && ($bc2 == $ib2)); } Changing the following: sub int2base { my( $ret, $num, $base, $minlen ) = ( '', @_ ); $num ||= 0; to: sub int2base { my( $ret, $num, $base, $minlen ) = ( '', @_ ); $num ||= 0; $num = Math::BigInt->new($num); seems to fix the problem converting from an integer. But I could not figure out what to do to fix it going back the other way. Just thought I'd point that out. Thanks for writing this!
Hi, thanks for the feedback. Yes, it appears that Math::BaseCnv operates on Math::BigInt objects by default. As you observed, Math::Int2Base doesn't. You can deal with large numbers and Math::Int2Base a couple of ways. The first example below has "use bigint;" near the top. This makes all the following operations deal with BigInt objects. The second example shows how you can instantiate Math::BigInt objects yourself to deal with large numbers. For my purposes, which seldom need to deal with large integers, not using Math::Bigint objects by default is desirable. I suppose someone (perhaps myself some day) could create Math::Int2Base::BigInt that would operate on Math::BigInt objects. Kind regards, Brad --------- example 1 --------- use Math::BaseCnv; use Math::Int2Base qw/ int2base base2int/; use bigint; my $i = 10**100; my $bc1 = cnv($i, 10, 62); my $ib1 = int2base($i, 62); my $bc2 = cnv($bc1, 62, 10); my $ib2 = base2int($ib1, 62); print join( "\n" => $bc1, $ib1, $bc2, $ib2 ) . "\n"; print "Yes (1)\n" if $bc1 eq $ib1; print "Yes (2)\n" if $bc2 == $ib2; __END__ QCyvrY2MJnQFGlUHTCA95Xz8AHOrLuoIO0fuPkHHCcyXy9ytM5N1lqsa QCyvrY2MJnQFGlUHTCA95Xz8AHOrLuoIO0fuPkHHCcyXy9ytM5N1lqsa 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Yes (1) Yes (2) --------- example 2 --------- #!/usr/bin/perl use Math::BaseCnv; use Math::Int2Base qw/ int2base base2int/; my $i = Math::BigInt->new( 10**100 ); my $bc1 = cnv($i, 10, 62); my $ib1 = int2base($i, 62); my $bc2 = cnv($bc1, 62, 10); my $ib2 = Math::BigInt->new( base2int($ib1, 62) ); print join( "\n" => $bc1, $ib1, $bc2, $ib2 ) . "\n"; print "Yes (1)\n" if $bc1 eq $ib1; print "Yes (2)\n" if $bc2 == $ib2; __END__ QCyvrY2MJnQFGlUHTCA95Xz8AHOrLuoIO0fuPkHHCcyXy9ytM5N1lqsa QCyvrY2MJnQFGlUHTCA95Xz8AHOrLuoIO0fuPkHHCcyXy9ytM5N1lqsa 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Yes (1) Yes (2)