Skip Menu |

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

Report information
The Basics
Id: 87466
Status: rejected
Worked: 1 hour (60 min)
Priority: 0/
Queue: Math-Prime-Util-GMP

People
Owner: DANAJ [...] cpan.org
Requestors: jimm [...] renewalcomputerservices.com
Cc:
AdminCc:

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



Subject: bigint support fails
Date: Tue, 30 Jul 2013 16:36:59 -0700
To: bug-Math-Prime-Util-GMP [...] rt.cpan.org
From: Renewal Computer Services <jimm [...] renewalcomputerservices.com>
D:\prj\twin-primes\math-prime-util>perl twin-primes.pl 1000000000000000000000000 500000 Parameter '1e+024' must be a positive integer at twin-primes.pl line 64. D:\prj\twin-primes\math-prime-util>perl twin-primes.pl 1000000000000000000000000 500000 Parameter '1e+024' must be a positive integer at D:/strawberry/perl/site/lib/Math/Prime/Util.pm line 310. D:\prj\twin-primes\math-prime-util>perl twin-primes.pl 1000000000000000000000000 500000 Parameter '1e+024' must be a positive integer at twin-primes.pl line 64. D:\prj\twin-primes\math-prime-util>perl twin-primes.pl "1000000000000000000000000" "500000" Parameter '1e+024' must be a positive integer at twin-primes.pl line 64. D:\prj\twin-primes\math-prime-util> I have tried the following code using int() and without. no difference. it still fails and tries to turn the bigints into floats. please fix the code? #!/usr/bin/perl #twinprimes.pl #Abstract: generate twin primes, and the differences, showing maximum difference for the set #Author: Jim Michaels #Created: 7/18/2013 #program arguments: # positive integer center # positive integer offset, less than center above #I originally did this with C++, but it was too slow with sieve of Eratosthenes. this had a fast sieve and it already had range capability. #use Math::Big; use Math::Prime::Util::GMP ':all'; #use Math::Prime::Util ':all'; use bigint; sub help { die("twinprimes.pl - generate list of twin primes with differences\n" . "usage:\n" . " twinprimes.pl center offset\n" . " perl twinprimes.pl center offset\n" . "options:\n" . " center: positive integer which twin primes center around\n" . " offset: positive integer which is subtracted from the offset and added to the center to provide upper and lower bounds.\n" . "\n" . "This program generates twin primes in the style of primesieve as far as format, but goes 2 steps further:\n" . "- this handles BigInt for infinite precision math\n" . "- it uses Perl's very flexible number formatting (handles hexadecimal, octal, etc)\n" . "- shows the difference between twin primes\n" . "- shows the maximum and minimum difference at the end, and the count\n" . "By Jim Michaels, 7/18/2013\n" ); } if (2 != $#ARGV+1) { #provide help, user provided invalid quantity of arguments help(); } my ($center, $offset, $i, $max_difference, $min_difference, $difference, $old_i, $upper_bound, $lower_bound,$twin_prine_count,$offset); my @twinprimes = (); $center=int($ARGV[0]); $offset=int($ARGV[1]); $upper_bound = $center+$offset+2; $lower_bound = $center-$offset; my $aref = primes($lower_bound, $upper_bound); my $num_primes_found = $#{$aref} + 1; #my $num_primes_found = $#aref + 1; $twin_prime_count=0; $old_i = -1; $difference = 0; for ($i=0; $i < $num_primes_found - 1; $i++) { if ($$aref[$i]+2==$$aref[$i+1]) {#twin prime? $twin_prime_count++; print "(" . $$aref[$i] . ", " . $$aref[$i+1] . ") index=" . $twin_prime_count; if (-1 != $old_i) { $difference = $i-$old_i; $min_difference = min($min_difference,$difference); $max_difference = max($max_difference,$difference); print " difference=" . $difference; } print "\n"; $old_i = $i; } } print "\n"; print "center=" . $center . "\n"; print "offset=" . $offset . "\n\n"; print "lower bound=" . $lower_bound . "\n"; print "upper bound=" . $upper_bound . "\n"; print "min difference=" . $min_difference . "\n"; print "max difference=" . $max_difference . "\n"; print "count=" . $twin_prime_count . "\n"; print "\n"; sub min { my($mn,$i); if ($#_+1 < 1) { return 0; } $mn=int($_[0]); for ($i=0; $i <= $#_; $i++) { #if (0 == $i) { # $mn=$_[$i]; #} if (int($_[$i]) < $mn) { $mn=int($_[$i]); } } return $mn; } sub max { my($mx,$i); if ($#_+1 < 1) { return 0; } $mx=int($_[0]); for ($i=0; $i <= $#_; $i++) { #if (0 == $i) { # $mx=$_[$i]; #} if (int($_[$i]) > $mx) { $mx=int($_[$i]); } } return $mx; } -- Renewal Computer Services <http://RenewalComputerServices.com> Jim Michaels, Owner 1303 NE 87th Ave <http://www.mapquest.com/maps/1303+NE+87th+Ave+Vancouver+WA+98664-1959/> (87th Ave & 13th St), Vancouver, WA, 98664-1959 USA, 1-360-521-2060 Cell 1-10pm Serving Vancouver, WA DIY Computer Repair Info: Jesusnjim.com <http://Jesusnjim.com> jimm@RenewalComputerServices.com <mailto:jimm@RenewalComputerServices.com> jmichae3@yahoo.com <mailto:jmichae3@yahoo.com>

Message body is not shown because it is too large.

This is an issue with Perl's bigint, not this module. Sadly Perl's string vs. int stuff is a bit wonky here. Change the two lines: $center=int($ARGV[0]); $offset=int($ARGV[1]); to: $center = 0 + $ARGV[0]; $offset = 0 + $ARGV[1]; This is making a bigint 0 then adding the string to it, which does the cast correctly. Unfortunately using int() isn't doing that. Another solution is to replace: use Math::Prime::Util::GMP ':all'; use bigint; with use Math::Prime::Util ':all'; use Math::BigInt try=>"GMP"; then use: $center = Math::BigInt->new($ARGV[0]); $offset = Math::BigInt->new($ARGV[1]); Comments here are - MPU will convert the string results from MPU::GMP into bigints for you. Without that you'd need to do it yourself so you can do the a[i]+2 == a[i+1] test. MPU::GMP is intentionally minimal in this way. - You don't need the try clause on Math::BigInt (or bigint), but I tend to use it because sometimes it can yield large speedups. The default bigint backend is very portable but also slow. It's a non-issue with your current code.
Subject: Re: [rt.cpan.org #87466] bigint support fails
Date: Tue, 13 Aug 2013 03:27:50 -0700
To: bug-Math-Prime-Util-GMP [...] rt.cpan.org
From: Renewal Computer Services <jimm [...] renewalcomputerservices.com>
On 8/1/2013 12:16 PM, Dana Jacobsen via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=87466 > > > This is an issue with Perl's bigint, not this module. Sadly Perl's string vs. int stuff is a bit wonky here. > > Change the two lines: > > $center=int($ARGV[0]); > $offset=int($ARGV[1]); > > to: > > $center = 0 + $ARGV[0]; > $offset = 0 + $ARGV[1]; > > This is making a bigint 0 then adding the string to it, which does the cast correctly. Unfortunately using int() isn't doing that. > > > Another solution is to replace: > use Math::Prime::Util::GMP ':all'; > use bigint; > with > use Math::Prime::Util ':all'; > use Math::BigInt try=>"GMP"; > then use: > $center = Math::BigInt->new($ARGV[0]); > $offset = Math::BigInt->new($ARGV[1]); > > Comments here are > - MPU will convert the string results from MPU::GMP into bigints for you. Without that you'd need to do it yourself so you can do the a[i]+2 == a[i+1] test. MPU::GMP is intentionally minimal in this way. > - You don't need the try clause on Math::BigInt (or bigint), but I tend to use it because sometimes it can yield large speedups. The default bigint backend is very portable but also slow. It's a non-issue with your current code. > >
can bigint and bignum etc be fixed so that int() works? thanks. if it is possible to do, great. I would need the ability to convert to int from hexadecimal, float, string, etc. thanks for the workaround. but I get the following error. Tue 08/13/2013 3:15:58.33|d:\prj\twin-primes|>twin-primes.pl 10000 500 Parameter must be defined at F:/strawberry/perl/site/lib/Math/Prime/Util.pm line 317. this is at the point of validation. there seems to be no sub validate_num, I am not sure where this is defined, maybe in Math::BigInt. looks like I am not going to be doing any math any time soon. -- Renewal Computer Services <http://RenewalComputerServices.com> Jim Michaels, Owner 1303 NE 87th Ave <http://www.mapquest.com/maps/1303+NE+87th+Ave+Vancouver+WA+98664-1959/> (87th Ave & 13th St), Vancouver, WA, 98664-1959 USA, 1-360-521-2060 Cell 1-10pm Serving Vancouver, WA DIY Computer Repair Info: Jesusnjim.com <http://Jesusnjim.com> jimm@RenewalComputerServices.com <mailto:jimm@RenewalComputerServices.com> jmichae3@yahoo.com <mailto:jmichae3@yahoo.com>
On Tue Aug 13 06:28:09 2013, jimm@renewalcomputerservices.com wrote: Show quoted text
> > can bigint and bignum etc be fixed so that int() works? thanks. if it > is > possible to do, great. I would need the ability to convert to int from > hexadecimal, float, string, etc.
You would need to take that up with the authors of those modules. The int() function is a builtin that is meant to operate on the native types. It would be nice if bigint would override it, but there is nothing I can do about it. Show quoted text
> thanks for the workaround. but I get the following error. > Tue 08/13/2013 3:15:58.33|d:\prj\twin-primes|>twin-primes.pl 10000 > 500 > Parameter must be defined at > F:/strawberry/perl/site/lib/Math/Prime/Util.pm line 317. this is at > the > point of validation. > > there seems to be no sub validate_num, I am not sure where this is > defined, maybe in Math::BigInt. > looks like I am not going to be doing any math any time soon.
I can't reproduce that with the script as written above. That error is generated when you calling primes() with an undefined value. Try printing out the values you're handing into primes() before calling it.