Page 1 of 1

Overflow Problem

Posted: Wed Aug 03, 2005 11:58 am
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!

Posted: Wed Aug 03, 2005 12:16 pm
by josh

Code: Select all

$value = (double)23523523535;

Posted: Wed Aug 03, 2005 12:48 pm
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

Posted: Wed Aug 03, 2005 5:03 pm
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.

Posted: Wed Aug 03, 2005 5:24 pm
by feyd
differing compiles of php handle large integers differently.. you can pretty easily create your own binary xor calculator..

Posted: Wed Aug 03, 2005 5:45 pm
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.

Posted: Wed Aug 03, 2005 5:47 pm
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?

Posted: Wed Aug 03, 2005 7:24 pm
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..

Posted: Thu Aug 04, 2005 11:54 am
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?

Posted: Thu Aug 04, 2005 12:01 pm
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

Posted: Thu Aug 04, 2005 3:11 pm
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?

Posted: Thu Aug 04, 2005 5:08 pm
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.

Posted: Thu Aug 04, 2005 8:49 pm
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?

Posted: Thu Aug 04, 2005 9:07 pm
by feyd