svn: /phpdoc/en/trunk/language/types/ integer.xml

From: Date: Fri, 26 Mar 2010 06:50:18 +0000
Subject: svn: /phpdoc/en/trunk/language/types/ integer.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-6271@lists.php.net to get a copy of this message
aharvey Fri, 26 Mar 2010 06:50:18 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=296829 Log: Update to the integer type page to make it clearer how overflow works on 32- and 64-bit systems and what the critical thresholds are. Prompted by bug #51274, although not an actual fix for it. Bug: http://bugs.php.net/51274 (Feedback) Integer overflow does not cast as float Changed paths: U phpdoc/en/trunk/language/types/integer.xml Modified: phpdoc/en/trunk/language/types/integer.xml =================================================================== --- phpdoc/en/trunk/language/types/integer.xml 2010-03-26 04:31:26 UTC (rev 296828) +++ phpdoc/en/trunk/language/types/integer.xml 2010-03-26 06:50:18 UTC (rev 296829) @@ -82,7 +82,7 @@ <para> The size of an <type>integer</type> is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). - 64-bits platforms usually have the maximum value of about 9E18. PHP + 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned <type>integer</type>s. <type>Integer</type> size can be determined using the constant <constant>PHP_INT_SIZE</constant>, and maximum value using the constant <constant>PHP_INT_MAX</constant> since @@ -118,34 +118,43 @@ <type>integer</type> type will return a <type>float</type> instead. </para> - <informalexample> + <example> + <title>Integer overflow on a 32-bit system</title> <programlisting role="php"> <![CDATA[ <?php -$large_number = 2147483647; -var_dump($large_number); -// output: int(2147483647) +$large_number = 2147483647; +var_dump($large_number); // int(2147483647) -$large_number = 2147483648; -var_dump($large_number); -// output: float(2147483648) +$large_number = 2147483648; +var_dump($large_number); // float(2147483648) -// it's true also for hexadecimal specified integers between 2^31 and 2^32-1: -var_dump( 0xffffffff ); -// output: float(4294967295) +$million = 1000000; +$large_number = 50000 * $million; +var_dump($large_number); // float(50000000000) +?> +]]> + </programlisting> + </example> -// this doesn't go for hexadecimal specified integers above 2^32-1: -var_dump( 0x100000000 ); -// output: int(2147483647) + <example> + <title>Integer overflow on a 64-bit system</title> + <programlisting role="php"> +<![CDATA[ +<?php +$large_number = 9223372036854775807; +var_dump($large_number); // int(9223372036854775807) +$large_number = 9223372036854775808; +var_dump($large_number); // float(9.2233720368548E+18) + $million = 1000000; -$large_number = 50000 * $million; -var_dump($large_number); -// output: float(50000000000) +$large_number = 50000000000000 * $million; +var_dump($large_number); // float(5.0E+19) ?> ]]> </programlisting> - </informalexample> + </example> <warning> <simpara> @@ -217,10 +226,11 @@ <para> If the float is beyond the boundaries of <type>integer</type> (usually - <literal>+/- 2.15e+9 = 2^31</literal>), the result is undefined, since the - <type>float</type> doesn't have enough precision to give an exact - <type>integer</type> result. No warning, not even a notice will be issued - when this happens! + <literal>+/- 2.15e+9 = 2^31</literal> on 32-bit platforms and + <literal>+/- 9.22e+18 = 2^63</literal> on 64-bit platforms), the result is + undefined, since the <type>float</type> doesn't have enough precision to + give an exact <type>integer</type> result. No warning, not even a notice + will be issued when this happens! </para> <warning>

« previous php.doc.cvs (#6271) next »