Overflow Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brettsg
Forum Newbie
Posts: 10
Joined: Tue Aug 02, 2005 12:18 pm

Overflow Problem

Post by brettsg »

Is there a way to use a double or long valued variable?

I am doing the following operation:

$val = 538759009 ^ 0xAABBCCDD;
print "val=" . $val;

and the value printed is:
val=-1968766020

This is caused by an overflow when the actual value should be:
val=2326201276

I tried using settype($val, "float"); and casting both 538759009 and 0xAABBCCDD also to float, but no luck. Any suggestions? Thanks!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

$value = (double)23523523535;
brettsg
Forum Newbie
Posts: 10
Joined: Tue Aug 02, 2005 12:18 pm

Post by brettsg »

jshpro2 wrote:

Code: Select all

$value = (double)23523523535;
Can you please explain in more depth?

I tried:

Code: Select all

settype($val, "double");
$val = (double)538759009 ^ (double)0xAABBCCDD;
print "val=" . (double)$val;
val=-1968766020
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

ob_implicit_flush();
set_time_limit(0);
echo 'This is going to take forever...<br /><br />';
$test = (double)0xAABBCC;
$val = bcpow(538759009, $test, 5);
print "val=" . $val;
Like I said in your other thread you need to use bcpow for arbitrary length numbers, I'm not going to run this code myself but it should work, the reason I'm not running it is it's going to take possibly hours to evaluate something like that. I did test it with smaller numbers, worked fine.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

differing compiles of php handle large integers differently.. you can pretty easily create your own binary xor calculator..
brettsg
Forum Newbie
Posts: 10
Joined: Tue Aug 02, 2005 12:18 pm

Post by brettsg »

jshpro2 wrote:

Code: Select all

ob_implicit_flush();
set_time_limit(0);
echo 'This is going to take forever...<br /><br />';
$test = (double)0xAABBCC;
$val = bcpow(538759009, $test, 5);
print "val=" . $val;
Like I said in your other thread you need to use bcpow for arbitrary length numbers, I'm not going to run this code myself but it should work, the reason I'm not running it is it's going to take possibly hours to evaluate something like that. I did test it with smaller numbers, worked fine.
I'm not raising 538759009 to the power of 0xAABBCCDD. I'm XORing the two values.
brettsg
Forum Newbie
Posts: 10
Joined: Tue Aug 02, 2005 12:18 pm

Post by brettsg »

feyd wrote:differing compiles of php handle large integers differently.. you can pretty easily create your own binary xor calculator..
Is there a way to just use floats or doubles?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

unfortunately, floats and doubles are handled slightly differingly as well. Either get ahold of a large integer library for php, or write your own binary xor function..
brettsg
Forum Newbie
Posts: 10
Joined: Tue Aug 02, 2005 12:18 pm

Post by brettsg »

feyd wrote:unfortunately, floats and doubles are handled slightly differingly as well. Either get ahold of a large integer library for php, or write your own binary xor function..
Do you know anything about the GMP libraries?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

brettsg wrote: I'm not raising 538759009 to the power of 0xAABBCCDD. I'm XORing the two values.
in that case gmp like previously mentioned:
http://us3.php.net/manual/en/function.gmp-xor.php
brettsg
Forum Newbie
Posts: 10
Joined: Tue Aug 02, 2005 12:18 pm

Post by brettsg »

feyd wrote:unfortunately, floats and doubles are handled slightly differingly as well. Either get ahold of a large integer library for php, or write your own binary xor function..
I decided to write my own binary functions and they work except for one small bug. For example when I do 2864434397 % 2 I get -1. What is the reason for this and how can I fix this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your version of php converts the value 2864434397 to -716950749, I believe... it's negative for sure, because 2147483647 is the maximum positive value for a signed 32-bit integer.
brettsg
Forum Newbie
Posts: 10
Joined: Tue Aug 02, 2005 12:18 pm

Post by brettsg »

feyd wrote:your version of php converts the value 2864434397 to -716950749, I believe... it's negative for sure, because 2147483647 is the maximum positive value for a signed 32-bit integer.
So there is no way to make a float variable or unsigned int that can handle all these problems?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply