mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Programming (https://www.mersenneforum.org/forumdisplay.php?f=29)
-   -   Please help me with my code (https://www.mersenneforum.org/showthread.php?t=19133)

daxmick 2014-02-10 01:52

Please help me with my code
 
1 Attachment(s)
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:[INDENT]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)
[/INDENT]
Attached is a copy of the program itself.

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

EdH 2014-02-10 02:55

[QUOTE=daxmick;366549]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:[INDENT]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)
[/INDENT]Attached is a copy of the program itself.

Thanks in advance for any help someone may offer! :-)[/QUOTE]
2[SUP]31[/SUP]=2147483648

Maybe a 32-bit issue?

potonono 2014-02-10 04:11

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

[URL="http://us3.php.net/manual/en/language.types.integer.php"]http://us3.php.net/manual/en/language.types.integer.php[/URL]

That number is treated as a float, not an integer.

daxmick 2014-02-13 23:57

[QUOTE=potonono;366560]See the following page under section "Example #3 Integer overflow on a 32-bit system".

[URL="http://us3.php.net/manual/en/language.types.integer.php"]http://us3.php.net/manual/en/language.types.integer.php[/URL]

That number is treated as a float, not an integer.[/QUOTE]

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
[/QUOTE]
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?

chalsall 2014-02-14 00:15

[QUOTE=daxmick;366880]So, what do you think this could be???[/QUOTE]

That you don't have a clue what you're doing?

daxmick 2014-02-14 00:27

[QUOTE=chalsall;366882]That you don't have a clue what you're doing?[/QUOTE]

Thanks for the positive response. Now do you have something to prove your snide comment or are you just being an ass.

chalsall 2014-02-14 00:43

[QUOTE=daxmick;366884]Thanks for the positive response. Now do you have something to prove your snide comment or are you just being an ass.[/QUOTE]

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).

daxmick 2014-02-14 00:54

1 Attachment(s)
[QUOTE=chalsall;366887]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).[/QUOTE]

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.

potonono 2014-02-14 02:29

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?

WraithX 2014-02-14 03:24

[QUOTE=daxmick;366880]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".[/QUOTE]

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:
[url]http://www.php.net/manual/en/sqlite3result.fetcharray.php[/url]

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.

daxmick 2014-02-14 04:37

[QUOTE=potonono;366892]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?[/QUOTE]

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[/QUOTE]

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!


All times are UTC. The time now is 10:04.

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