|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-03-30 21:26 UTC] signe at cothlamadh dot net
Description:
------------
On 64-bit operating systems, printf()'s %d and %u formatting codes support 64-bit integers. sscanf, which supposedly uses the same code (and has the same expected behavior) does not support 64-bit values.
Also, printf is not outputting accurate values when it reaches numbers near the upper bounds of a 64-bit int.
Reproduce code:
---------------
<?php
sscanf("2147483647", '%d', $int);
echo "sscanf 32-bit signed int '2147483647' (2^31)-1 = ",$int,"\n";
sscanf("4294967295", '%u', $int);
echo "sscanf 32-bit unsign int '4294967295' (2^32)-1 = ",$int,"\n";
sscanf("9223372036854775807", '%d', $int);
echo "sscanf 64-bit signed int '9223372036854775807' (2^63)-1 = ",$int,"\n";
sscanf("18446744073709551615", '%u', $int);
echo "sscanf 64-bit unsign int '18446744073709551615' (2^64)-1 = ",$int,"\n";
printf("printf 64-bit signed int '9223372036854775807' (2^63)-1 = %d\n", 9223372036854775807);
printf("printf 64-bit signed int '18446744073709551615' (2^64)-1 = %u\n", 18446744073709551615);
echo "\n(2^64)-1 - 100,000,000\n";
printf("printf 64-bit signed int '18446744073609551615' = %u\n", 18446744073609551615);
echo "Output is 257 greater than input\n";
?>
Expected result:
----------------
sscanf 32-bit signed int '2147483647' (2^31)-1 = 2147483647
sscanf 32-bit unsign int '4294967295' (2^32)-1 = 4294967295
sscanf 64-bit signed int '9223372036854775807' (2^63)-1 = 9223372036854775807
sscanf 64-bit unsign int '18446744073709551615' (2^64)-1 = 18446744073709551615
printf 64-bit signed int '9223372036854775807' (2^63)-1 = 9223372036854775807
printf 64-bit signed int '18446744073709551615' (2^64)-1 = 18446744073709551615
Actual result:
--------------
sscanf 32-bit signed int '2147483647' (2^31)-1 = 2147483647
sscanf 32-bit unsign int '4294967295' (2^32)-1 = 4294967295
sscanf 64-bit signed int '9223372036854775807' (2^63)-1 = -1
sscanf 64-bit unsign int '18446744073709551615' (2^64)-1 = 4294967295
printf 64-bit signed int '9223372036854775807' (2^63)-1 = 9223372036854775807
printf 64-bit signed int '18446744073709551615' (2^64)-1 = 0
printf 64-bit signed int '18446744073609551615' ((2^64)-1 - 100000000) = 18446744073609551872
Output is 257 greater than input
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 08:00:01 2025 UTC |
Yes, but the issue mentioned with printf still remains open, ie: printf("printf 64-bit signed int '18446744073709551615' (2^64)-1 = %u\n", 18446744073709551615); see http://3v4l.org/ZeAYt