Skip Menu |

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

Report information
The Basics
Id: 4472
Status: resolved
Priority: 0/
Queue: Math-GMP

People
Owner: Nobody in particular
Requestors: nicholas [...] oxhoej.dk
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 2.03
Fixed in: 2.04



Subject: Base specification for Math::GMP::new()
I am missing at least one (for me) important feature: the ability to specify a base when creating a new Math::GMP object. Our values are represented in base 36, which the gpm library is perfectly able to handle, but there is no way to specify it from Perl. I tried to do the base36 to Math::GPM conversion in Perl, but that was painfully slow with a 200 char long value. Something on the order of 0.1 second. So I would like to suggest to add an optional "base" parameter to Math::GMP::new(), which just passes the value along to mpz_init_set_str(). This enables very, very fast conversion of all kinds of values (especially base36) to a Math::GMP object. I have implemented this feature locally, but thought that it might be a valuable addition to the official version of Math::GMP. Of course this would also mean that I wouldn't have to maintain my own patches to Math::GMP :-) Unfortunately I am no expert at XS coding, so I just created a new version of new_from_scalar(s) called new_from_scalar_with_base(s, b) and then handled the test on a "base" parameter in Math::GMP::new(). If you are interested, I have created the (very small) required diffs against Math-GMP-2.03 diff GMP.xs.orig GMP.xs 73a74,84 Show quoted text
> mpz_t * > new_from_scalar_with_base(s, b) > char * s > int b > > CODE: > RETVAL = malloc (sizeof(mpz_t)); > mpz_init_set_str(*RETVAL, s, b); > OUTPUT: > RETVAL >
diff GMP.pm.orig GMP.pm 99c99,100 < my $ival = shift; --- Show quoted text
> my $ival = shift || 0; > my $base = shift;
103c104,108 < $ival = 0 if $ival =~ /[^\d\-xA-Fa-f]/ || !$ival; --- Show quoted text
> my $ret; > if ($base) { > $ret = Math::GMP::new_from_scalar_with_base($ival, $base); > } else { > $ival = 0 if $ival =~ /[^\d\-xA-Fa-f]/;
104a110,111 Show quoted text
> $ret = Math::GMP::new_from_scalar($ival); > }
106,107d112 < my $ret = Math::GMP::new_from_scalar($ival); <
From: nicholas [...] oxhoej.dk
I have made a patch file (to be applied with "patch -p0") to make the correction even easier. Should have done so in the first place - sorry.
--- Math-GMP-2.03/GMP.xs Sun Feb 17 20:39:39 2002 +++ Math-GMP-2.03/GMP.xs Fri Jan 9 10:42:22 2004 @@ -71,6 +71,17 @@ OUTPUT: RETVAL +mpz_t * +new_from_scalar_with_base(s, b) + char * s + int b + + CODE: + RETVAL = malloc (sizeof(mpz_t)); + mpz_init_set_str(*RETVAL, s, b); + OUTPUT: + RETVAL + void destroy(n) mpz_t *n --- Math-GMP-2.03/lib/Math/GMP.pm Sun Feb 17 20:39:39 2002 +++ Math-GMP-2.03/lib/Math/GMP.pm Fri Jan 9 10:45:53 2004 @@ -96,14 +96,19 @@ sub new { my $class = shift; - my $ival = shift; + my $ival = shift || 0; + my $base = shift; $ival =~ s/^\+//; $ival =~ s/[ _]//g; - $ival = 0 if $ival =~ /[^\d\-xA-Fa-f]/ || !$ival; + my $ret; + if ($base) { + $ret = Math::GMP::new_from_scalar_with_base($ival, $base); + } else { + $ival = 0 if $ival =~ /[^\d\-xA-Fa-f]/; - - my $ret = Math::GMP::new_from_scalar($ival); + $ret = Math::GMP::new_from_scalar($ival); + } return $ret; }
Added by Chip Turner in version 2.04, thanks for the patch.