Ignoring the special libraries that allow you to work with very big numbers, what's the largest int value you can store in PHP?
Answers
So I solved the problem, and discovered a lot about PHP (at least in the way it handles Integer overflow).
1) It completely depended on a cross between which platform the machine was running on, which version of PHP, whether or not it had Suhosin Hardened PHP running, and how many bits it was compiled for (32 or 64). 6 machines behaved the way I expected (which was actually wrong, at least wrong according to their documentation) and 3 machines behaved in a way I still can't explain, and 3 machines behaved according to what the intval command says it does in the documentation.
2) Intval is supposed to return PHP_INT_MAX when int > PHP_INT_MAX (not int & 0xffffffff), but this only happens on some versions of PHP4 and PHP5. Different versions of PHP return different values when int > PHP_INT_MAX.
3) The following code can return 3 different results (see 1):
<?php
echo "Php max int: ".PHP_INT_MAX."n";
echo "The Val: ".(-1580033017 + -2072974554)."n";
echo "Intval of the val: ".intval(-3653007571)."n";
echo "And 0xffffffff of the val: ".(-3653007571 & 0xffffffff)."n";
?>
It can return (which appears to be right for Intval but wrong for & 0xffffff)
Php max int: 2147483647
The Val: -3653007571
Intval of the val: -2147483648
And of the val: -2147483648
And it can return (which contradicts the PHP documentation for intval):
Php max int: 2147483647
The Val: -3653007571
Intval of the val: -641959725
And of the val: -641959725
And on 64 Bit machines it returns (which is correct):
Php max int: 2147483647
The Val: -3653007571
Intval of the val: -3653007571
And of the val: -641959725
Solution
Anyhow, I needed a solution that would work on all these platforms, and not be dependent upon quirks of a particular version of PHP compiled with a particular Max int. Thus I cam up with the following cross-PHP thirtyTwoBitIntval function:
function thirtyTwoBitIntval($value)
{
if ($value < -2147483648)
{
return -(-($value) & 0xffffffff);
}
elseif ($value > 2147483647)
{
return ($value & 0xffffffff);
}
return $value;
}
Comment
I do think the designers of PHP should have said an Int is a 32 Bit Int no matter whether it is running on a 32 or 64 or 128 bit machine (like the DotNet CLR for example), and didn't randomly upconvert it to a float depending on the number of Bits that PHP is compiler under.
$max = 0;
foreach($array as $obj)
{
if($obj->dnum > $max)
{
$max = $obj->dnum;
}
}
That function would work correctly if your highest number is not negative (negatives, empty arrays, and 0s will return the max as 0).
Because you are using an object, which can have custom properties/structures, I don't believe there are really any 'predefined' functions you can use to get it. Might as well just use a foreach loop.
You really can't get away from a foreach loop, as even internal functions use a foreach loop, it is just behind the scenes.
Another solution is
$numbers = array();
foreach($array as $obj)
{
$numbers[] = $obj->dnum;
}
$max = max($numbers);
Try using sprintf()
$numbers2[] = sprintf("%+07.3f", $fnum);
In Mysql there is a cheap trick to do this:
mysql> select ~0;
+----------------------+
| ~0 |
+----------------------+
| 18446744073709551615 |
+----------------------+
the tilde is the bitwise negation. The resulting value is a bigint. See: http://dev.mysql.com/doc/refman/5.1/en/bit-functions.html#operator_bitwise-invert
For the other integer flavours, you can use the right bitshift operator >>
like so:
SELECT ~0 as max_bigint_unsigned
, ~0 >> 32 as max_int_unsigned
, ~0 >> 40 as max_mediumint_unsigned
, ~0 >> 48 as max_smallint_unsigned
, ~0 >> 56 as max_tinyint_unsigned
, ~0 >> 1 as max_bigint_signed
, ~0 >> 33 as max_int_signed
, ~0 >> 41 as max_mediumint_signed
, ~0 >> 49 as max_smallint_signed
, ~0 >> 57 as max_tinyint_signed
G
*************************** 1. row ***************************
max_bigint_unsigned: 18446744073709551615
max_int_unsigned: 4294967295
max_mediumint_unsigned: 16777215
max_smallint_unsigned: 65535
max_tinyint_unsigned: 255
max_bigint_signed: 9223372036854775807
max_int_signed: 2147483647
max_mediumint_signed: 8388607
max_smallint_signed: 32767
max_tinyint_signed: 127
1 row in set (0.00 sec)
From the PHP manual: