note 103525 modified in language.operators.arithmetic by danbrown

From: Date: Mon, 18 Apr 2011 15:20:56 +0000
Subject: note 103525 modified in language.operators.arithmetic by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-178945@lists.php.net to get a copy of this message
For larger numbers (above PHP_INT_MAX), use fmod() rather than %. The other operators (+-*/) work correctly with floats and integer overflow, but % uses integer wrap. Eg. <?php var_dump(0xffffffff % 2); //Prints int(-1) which is WRONG var_dump(intval(fmod(0xffffffff,2))); //Prints int(1) which is the right answer ?> (The reason this matters is that PHP's float is actually a double, and can accurately represent integers up to 52-bits, even on 32-bit systems) --was-- For larger numbers (above PHP_INT_MAX), use fmod() rather than %. The other operators (+-*/) work correctly with floats and integer overflow, but % uses integer wrap. Eg. var_dump(0xffffffff % 2); //Prints int(-1) which is WRONG var_dump(intval(fmod(0xffffffff,2))); //Prints int(1) which is the right answer (The reason this matters is that PHP's float is actually a double, and can accurately represent integers up to 52-bits, even on 32-bit systems) http://php.net/manual/en/language.operators.arithmetic.php

« previous php.notes (#178945) next »