This question already has answers here:
Working with large numbers in PHP
(8 answers)
Closed 9 years ago.
intval:
Get the integer value of a variable
echo intval('1000000000000');
returns 2147483647.
Why?
The size of an integer in PHP is platform-dependent. The documentation for intval() clearly explains this:
The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.
From the same documentation you shared..
The maximum value depends on the system. 32 bit systems have a maximum
signed integer range of -2147483648 to 2147483647. So for example on
such a system, intval('1000000000000') will return 2147483647. The
maximum signed integer value for 64 bit systems is
9223372036854775807.
echo intval('1000000000000');
returns 2147483647. Why?
is because the system you're trying is 32 bits system
Because
1. You have 32 bit systems.
2. That's why you can operate a maximum signed integer range of -2147483648 to 2147483647`.
For this value there will be no problem if You had 64 bit systems
Antother example:
On 32 bit systems
echo intval('420000000000000000000'); // 2147483647
On 64 bit systems
echo intval('420000000000000000000'); // 420000000000000000000
Related
What is the maximum input value for a seed in PHP? Is it 2^32? 2^64? No limit?
I cannot seem to find an answer in PHP documentation.
php_srand takes a long, and it calls one of srandom, srand48, or srand (depending upon compilation defines).
It casts the seed value to an int in the case of srandom/srand, but passes the full long-typed value through to srand48 - so then it comes down to implementation of the system/stdlib random functions (and actual size of int).
Also, considering that mt_srand takes a uint32, I'd say it's reliable to expect 32-bit seeds to be considered distinct (even if they start the same sequence). A 64-bit seed is the upper value limit (even if the entire seed is not used), but is only realizable in a 64-bit version of PHP.
Any non-integer value (i.e. float) supplied as a seed will be coerced to an integer. The actual php_srand C function is only called after coercing the supplied PHP value to a long in the wrapper.
You can use getrandmax() to determine the maximum value. ref: http://www.php.net/manual/en/function.getrandmax.php
srand takes an int which I assume is a 32-bit number on 32-bit machines according to the docs. and 64 on 64-bit machines.
I am trying this out, but am unable to store large value
$var = rand(100000000000000,999999999999999);
echo $var; // prints a 9 digit value(largest possible)
How to get a desired value ?
From the manual:
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
...
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
BC Math and GMP are the (only?) way to manipulate this limitation.
PHP ints are typically 32 bits. Other packages provide higher-precision ints: http://php.net/manual/en/language.types.integer.php
If you need to work with very large numbers I have found success with BC Math. Here is a link to everything you need to know:
http://php.net/manual/en/book.bc.php
If you want to generate the number and manipulate as a native type, you can't with most PHP installations (either you have 32 or 64 bit ints and nothing else), as the other answers have already stated. However, if you are just generating a number and want to pass it around a possible trick is to just concatenate strings:
$var = rand(0,PHP_INT_MAX).str_pad(rand(0, 999999999), 9, 0, STR_PAD_LEFT);
echo $var;
On a platform in which PHP uses a 32 bit integer, this allows you to get a near random integer (as a string) that is bigger than 32 bits ( > 10 decimal places). Of course, there is a bias in this construction which means you won't cover all the numbers with the same probability. The limits of the rand() calls obey normal decimal rules so its simple to adjust the upper bound of the number you want.
If all you're doing is storing/transmitting/showing this value, the string will be just fine. Equality and greater/less than tests will also work. Just don't do any math with it.
Lets assume we are talking about 32bit system.
PHP doesn't support unsigned INT. It means that INT value should be between -2,147,483,648 and 2,147,483,647 values. And INT takes 4 bytes to store a value which are 32 bits length.
So does it mean that I have only 31 bits for value and 1 bit for sign? Or I can use whole 32 bits to store a value?
You are using the whole 32 bits. It's just that the default output functions interpret it as signed integer. If you want to display the value "raw" use:
printf("%u", -1); // %u for unsigned
Since PHP handles the integers signed internally however, you can only use bit arithmetics, but not addition/multiplication etc. with them - if you expect them to behave like unsigned ints.
2147483647 is the usual value 2^31-1. 1 bit for sign and -1 because we also represent 0.
from the manual:
"The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5."
As far as i know on a 32-bit system the largest positive integer possible is 2147483647 values above will be float values, since a float value in php can take values up to 10000000000000.
First of all, if you want to do calculations with huge numbers (say, regulary greater than 10k), you should use the bcmath arbitrary precision module.
Secondly, the official php implementation uses the Two's complement internally to represent numbers, like virtually all other compilers and interpreters. Since the entropy of the signed bit (if you count 0 as positive[1]) is 1, and the entropy of 31 bits is, well, 31, you can store 232 = 4 294 967 296 distinct values. How you interpret them is up to your application.
[1] - 0 is neither positive nor negative.
Normally with 32 bit integers the difference between signed and unsigned is just how you interpret the value. For example, (-1)+1 would be 1 for both signed and unsigned, for signed it's obvious and for unsigned it's of course only true if you just chop the overflow off. So you do have 32 bits to store values, it just happens to be so that there's 1 bit that is interpreted differently from the rest.
Two's complement is most frequently used to store numbers, which means that 1 bit is not wasted just for the sign.
http://en.wikipedia.org/wiki/Two's_complement
In PHP if a number overflows the INT_MAX for a platform, it converts it to a floating point value.
Yes if it would have used unsigned integer it will use 32 bit to store it as you don't need sign in that case but as it supports only signed integers a 32 bit systems will have 31 bit for value and 1 bit for sign s0 maximum signed integer range is -2147483648 to 2147483647.
I want to produce a natural number (a positive integer between 0 and x) by using lcg_value(). This is what I can see from the actual source code for the function:
/*
* combinedLCG() returns a pseudo random number in the range of (0, 1).
* The function combines two CGs with periods of
* 2^31 - 85 and 2^31 - 249. The period of this function
* is equal to the product of both primes.
*/
Now, how would I turn that float into a natural number so that I do not lose information/entropy. Currently I just do it like this:
$number = pow(2,31) * lcg_value();
However, I have a feeling in my gut that it's not perfect. How about 2^62? Or perhaps 2^31-334? Or 2^62-334? I don't know.
either type float or int are bounded by platform
Integer The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
float The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).
to maintain highest precision of float, u can refer http://www.php.net/manual/en/function.bcmul.php
I want to convert bytes to GB.
value= 8587321344
So it should be 8587321344/1024/1024/1024
But whenever I go to divide, the value is wrong...
If I cast it into integer, it will be limited to 2147....
Can't find any type cast to long data type...
Funny enough...
How to perform this calculation to show correct output...
The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.
Look at the GNU Multiple Precision library for arbitrary size integers in PHP.
Example:
$a = gmp_init("2487234329784238974238974")
$result = gmp_div($a, 2)