mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2014-02-10, 01:52   #1
daxmick
 
daxmick's Avatar
 
Feb 2014

5416 Posts
Default Please help me with my code

I've posted a call for help on stackoverflow as well as Googled the dickens out of the error message I'm receiving but nothing is coming up. So, I thought I'd post my plea for help here and maybe some math guru here can help me solve this issue.

Anyway, I've written (probably poorly written) a PHP program that tests a number for primality and if it is prime adds it to a database. However, about every 6 hours or so the program crashes. I have other programs using PHP GMP and long loops that have run for days without crashing so I don't see what is wrong with this particular program. Maybe one of you can see the issue.

Here is the error I keep getting:
PHP Warning: gmp_mod(): -2147483648 is not a valid GMP integer resource in /SSD/PrimeBasket/PrimeNumberBasket.php on line 100
PHP Warning: gmp_prob_prime(): -2147483648 is not a valid GMP integer resource in /SSD/PrimeBasket/PrimeNumberBasket.php on line 74
PHP Warning: gmp_add(): -2147483648 is not a valid GMP integer resource in /SSD/PrimeBasket/PrimeNumberBasket.php on line 120
PHP Warning: SQLite3::query(): Unable to execute statement: column thenumber is not unique in /SSD/PrimeBasket/PrimeNumberBasket.php on line 115
PHP Notice: Undefined property: SQLite3::$error in /SSD/PrimeBasket/PrimeNumberBasket.php on line 115

Insert failed: INSERT INTO Number (thenumber) VALUES (2)
Attached is a copy of the program itself.

Thanks in advance for any help someone may offer! :-)
Attached Files
File Type: txt PrimeNumberBasket.php.txt (4.8 KB, 151 views)
daxmick is offline   Reply With Quote
Old 2014-02-10, 02:55   #2
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

31×109 Posts
Default

Quote:
Originally Posted by daxmick View Post
I've posted a call for help on stackoverflow as well as Googled the dickens out of the error message I'm receiving but nothing is coming up. So, I thought I'd post my plea for help here and maybe some math guru here can help me solve this issue.

Anyway, I've written (probably poorly written) a PHP program that tests a number for primality and if it is prime adds it to a database. However, about every 6 hours or so the program crashes. I have other programs using PHP GMP and long loops that have run for days without crashing so I don't see what is wrong with this particular program. Maybe one of you can see the issue.

Here is the error I keep getting:
PHP Warning: gmp_mod(): -2147483648 is not a valid GMP integer resource in /SSD/PrimeBasket/PrimeNumberBasket.php on line 100
PHP Warning: gmp_prob_prime(): -2147483648 is not a valid GMP integer resource in /SSD/PrimeBasket/PrimeNumberBasket.php on line 74
PHP Warning: gmp_add(): -2147483648 is not a valid GMP integer resource in /SSD/PrimeBasket/PrimeNumberBasket.php on line 120
PHP Warning: SQLite3::query(): Unable to execute statement: column thenumber is not unique in /SSD/PrimeBasket/PrimeNumberBasket.php on line 115
PHP Notice: Undefined property: SQLite3::$error in /SSD/PrimeBasket/PrimeNumberBasket.php on line 115

Insert failed: INSERT INTO Number (thenumber) VALUES (2)
Attached is a copy of the program itself.

Thanks in advance for any help someone may offer! :-)
231=2147483648

Maybe a 32-bit issue?
EdH is offline   Reply With Quote
Old 2014-02-10, 04:11   #3
potonono
 
potonono's Avatar
 
Jun 2005
USA, IL

193 Posts
Default

See the following page under section "Example #3 Integer overflow on a 32-bit system".

http://us3.php.net/manual/en/language.types.integer.php

That number is treated as a float, not an integer.
potonono is offline   Reply With Quote
Old 2014-02-13, 23:57   #4
daxmick
 
daxmick's Avatar
 
Feb 2014

22·3·7 Posts
Default

Quote:
Originally Posted by potonono View Post
See the following page under section "Example #3 Integer overflow on a 32-bit system".

http://us3.php.net/manual/en/language.types.integer.php

That number is treated as a float, not an integer.
Thanks for the info! Going with this idea I change my "while(1)" to while(is_resource($NumberToTest))" and then added a bit of code at the bottom of my script to print out what $NumberToTest is (I should only ever drop out of the while loop if $NumberToTest is no longer a gmp resource). And indeed I did get dropped out of the while loop and $NumberToTest prints out as "Resource id #-2147483648".

The odd thing is that the only time I modify the $NumberToTest variable is at the end of the loop when I add 2 to it (to get the next odd number) and I haven't even come close to exceeding 2^32 sized number (I'm only in the 550*10^6 range).

So, what do you think this could be???

EDIT: I added a print of $NumberToTest to print out not as a string but as a raw Resource variable every 12345 loops and I find something interesting!
Quote:
2014-02-13 16:59:06 --> NumberToTest = 543804595
NumberToTest as a Resource: Resource id #13912458
2014-02-13 17:00:26 --> NumberToTest = 543913237
NumberToTest as a Resource: Resource id #28141589
2014-02-13 17:01:49 --> NumberToTest = 544021879
NumberToTest as a Resource: Resource id #42184263
Maybe there is a bug in the PHP GMP process that accidentally assigns an out of range value on occasion? Maybe I have a 64 bit GMP library but only a 32 bit PHP process?

Last fiddled with by daxmick on 2014-02-14 at 00:04
daxmick is offline   Reply With Quote
Old 2014-02-14, 00:15   #5
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2×4,643 Posts
Default

Quote:
Originally Posted by daxmick View Post
So, what do you think this could be???
That you don't have a clue what you're doing?
chalsall is offline   Reply With Quote
Old 2014-02-14, 00:27   #6
daxmick
 
daxmick's Avatar
 
Feb 2014

22·3·7 Posts
Default

Quote:
Originally Posted by chalsall View Post
That you don't have a clue what you're doing?
Thanks for the positive response. Now do you have something to prove your snide comment or are you just being an ass.
daxmick is offline   Reply With Quote
Old 2014-02-14, 00:43   #7
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2×4,643 Posts
Default

Quote:
Originally Posted by daxmick View Post
Thanks for the positive response. Now do you have something to prove your snide comment or are you just being an ass.
You come on this forum, and ask us to debug your code while suggesting that PHP GMP has a bug because of output from your code?

Please give specific examples (exact code, exact input and exact output), and we'll take you seriously.

Otherwise you're just a waste of carbon (sorry if that comes across as mean, but some of us actually have real jobs).
chalsall is offline   Reply With Quote
Old 2014-02-14, 00:54   #8
daxmick
 
daxmick's Avatar
 
Feb 2014

8410 Posts
Default

Quote:
Originally Posted by chalsall View Post
You come on this forum, and ask us to debug your code while suggesting that PHP GMP has a bug because of output from your code?

Please give specific examples (exact code, exact input and exact output), and we'll take you seriously.

Otherwise you're just a waste of carbon (sorry if that comes across as mean, but some of us actually have real jobs).
My comment about it being a bug is only a question/suggestion. I also offered other alternatives if you'd take note.

I thought I did give output from my code... or at least the error that I'm seeing. If you'd read my code you'd see that technically there is no output. The "output" is just a list of prime numbers and those are put in a sqlite db. Just in case you didn't see the first post's attachment here it is again.

Your comments are not only mean but rude. So far you've only just negatively commented without any reason or purpose. Directly attacking someone (especially first off with no obvious reason) isn't a basis for a good interaction. Suggesting someone is a "waste of carbon" leaves little in the way of being socialable. Are you trying to get people to not post questions here by scaring them away or do you just have no social interaction skills.
Attached Files
File Type: txt PrimeNumberBasket.php.txt (5.1 KB, 236 views)
daxmick is offline   Reply With Quote
Old 2014-02-14, 02:29   #9
potonono
 
potonono's Avatar
 
Jun 2005
USA, IL

193 Posts
Default

I hope I am explaining this correctly, but the 32-bit limit is for the number of digits of the integer in base 2. If your process is 32-bit, you can't exceed that limit no matter what base your numbers are in (while dealing with the numbers as just integers).

It's been too long since I've had to troubleshoot PHP. Rather than work out your current code issue, based on what you want, have you considered just calling gmp_nextprime over and over starting at 2, and using the last result in each loop?

Last fiddled with by potonono on 2014-02-14 at 02:42
potonono is offline   Reply With Quote
Old 2014-02-14, 03:24   #10
WraithX
 
WraithX's Avatar
 
Mar 2006

23·59 Posts
Default

Quote:
Originally Posted by daxmick View Post
Thanks for the info! Going with this idea I change my "while(1)" to while(is_resource($NumberToTest))" and then added a bit of code at the bottom of my script to print out what $NumberToTest is (I should only ever drop out of the while loop if $NumberToTest is no longer a gmp resource). And indeed I did get dropped out of the while loop and $NumberToTest prints out as "Resource id #-2147483648".
A couple of things to check would be, print out the value of both NumberToTest and Prime (perhaps both the value and the resource id) to see if one or both are having problems. Also, an example on the php website shows an extra check for empty rows before using the result of fetchArray. You can see that here:
http://www.php.net/manual/en/sqlite3...fetcharray.php

Maybe your query is returning empty rows. To that point, maybe there are empty rows in your db? Try to look in your db to see if this may be the case. Or, maybe you should use fetchArray(SQLITE3_ASSOC) to guarantee you can call into the resultant array by string name 'thenumber'.

Also, can your gmp_cmp use the integer 0 as a value? Or should you be comparing against the string '0'? And, maybe the same thing for your gmp_add. Can you use the integer 1 as a value, or should that be the string value '1'?

I haven't programmed too much in PHP, but hopefully this will give you some things to look into to see if you can narrow down where the problem is coming from.
WraithX is offline   Reply With Quote
Old 2014-02-14, 04:37   #11
daxmick
 
daxmick's Avatar
 
Feb 2014

8410 Posts
Default

Quote:
Originally Posted by potonono View Post
I hope I am explaining this correctly, but the 32-bit limit is for the number of digits of the integer in base 2. If your process is 32-bit, you can't exceed that limit no matter what base your numbers are in (while dealing with the numbers as just integers).

It's been too long since I've had to troubleshoot PHP. Rather than work out your current code issue, based on what you want, have you considered just calling gmp_nextprime over and over starting at 2, and using the last result in each loop?
I've got what you are saying, I think, 100%. I'm not on a 32 bit computer:
Quote:
Linux strieff 3.11.0-12-generic #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
No worries. If it isn't an easy fix I'll just move to another programming language. I just find it weird since I have other PHP GMP related programs that have run MUCH longer than this one without errors.

Thanks for the thought though!
daxmick is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rho code Happy5214 YAFU 3 2015-11-01 21:54
Code Help Andrew Programming 12 2013-02-16 20:53
New Code JohnFullspeed Programming 20 2011-09-04 04:28
Code Primeinator Software 20 2009-06-11 22:22
A little help with VBS code IronBits No Prime Left Behind 6 2008-11-12 14:23

All times are UTC. The time now is 22:20.

Mon Oct 26 22:20:54 UTC 2020 up 46 days, 19:31, 0 users, load averages: 1.60, 1.76, 1.73

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.