Register FAQ Search Today's Posts Mark Forums Read

2014-02-10, 01:52   #1
daxmick

Feb 2014

22·3·7 Posts

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: 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  PrimeNumberBasket.php.txt (4.8 KB, 272 views) 2014-02-10, 02:55 #2 EdH "Ed Hall" Dec 2009 Adirondack Mtns 147816 Posts Quote:  Originally Posted by daxmick 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?

 2014-02-10, 04:11 #3 potonono     Jun 2005 USA, IL C116 Posts 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.
2014-02-13, 23:57   #4
daxmick

Feb 2014

1248 Posts

Quote:
 Originally Posted by potonono 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

2014-02-14, 00:15   #5
chalsall
If I May

"Chris Halsall"
Sep 2002

32×1,231 Posts

Quote:
 Originally Posted by daxmick So, what do you think this could be???
That you don't have a clue what you're doing?

2014-02-14, 00:27   #6
daxmick

Feb 2014

22×3×7 Posts

Quote:
 Originally Posted by chalsall 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.

2014-02-14, 00:43   #7
chalsall
If I May

"Chris Halsall"
Sep 2002

32×1,231 Posts

Quote:
 Originally Posted by daxmick 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).

2014-02-14, 00:54   #8
daxmick

Feb 2014

10101002 Posts

Quote:
 Originally Posted by chalsall 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

 2014-02-14, 02:29 #9 potonono     Jun 2005 USA, IL 193 Posts 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
2014-02-14, 03:24   #10
WraithX

Mar 2006

13×41 Posts

Quote:
 Originally Posted by daxmick 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.

2014-02-14, 04:37   #11
daxmick

Feb 2014

22×3×7 Posts

Quote:
 Originally Posted by potonono 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!

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

All times are UTC. The time now is 11:26.

Fri Feb 3 11:26:22 UTC 2023 up 169 days, 8:54, 1 user, load averages: 0.84, 0.88, 0.84