Doc #53397 [Ver->Csd]: Modulo is not accurate with double

From: Date: Fri, 28 May 2021 14:11:08 +0000
Subject: Doc #53397 [Ver->Csd]: Modulo is not accurate with double
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18813@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53397&edit=1 ID: 53397 Updated by: git@php.net Reported by: mryaggi at hotmail dot com Summary: Modulo is not accurate with double -Status: Verified +Status: Closed Type: Documentation Problem Package: Scripting Engine problem Operating System: All PHP Version: Irrelevant Assigned To: cmb Block user comment: N Private report: N New Comment: Automatic comment on behalf of cmb69 Revision: https://github.com/php/doc-en/commit/e4b889a3e8b9d87ab605aac9bbe85d2c16c69a1b Log: Fix #53397: Modulo is not accurate with double Previous Comments: ------------------------------------------------------------------------ [2021-05-28 14:10:41] cmb@php.net The documentation had already been improved (mentioning fmod()); I'll clarify that the numbers are converted to int (it's not only about stripping the decimal part). ------------------------------------------------------------------------ [2013-09-18 21:50:02] cmbecker69 at gmx dot de The operation a - (floor(a/b)*b) gives not the modulo, but the remainder. Furthermore it seems the suggested patch does neither cater for large divisors nor large results. It *might* be reasonable to stick with the current implementation, and to state clearly in the documentation[1] that the modulus operator gives correct results only for integers in the range [-PHP_INT_MAX - 1, PHP_INT_MAX]. The current wording is somewhat misleading, IMHO. [1] <http://www.php.net/manual/en/language.operators.arithmetic.php> ------------------------------------------------------------------------ [2013-08-06 08:28:02] yohgaki@php.net Test with 5.5.1 32bit. This issue exists still. ------------------------------------------------------------------------ [2010-11-24 15:12:40] mryaggi at hotmail dot com Description: ------------ In C++ : 1290594600000 % 86400000 = 37800000 //correct But In PHP : 1290594600000 % 86400000 = 30811200 //wrong However, in PHP : 1290594600000 - (floor(1290594600000/86400000)*86400000) = 37800000 //correct! Can modulo opertor be implemented this way when left value > PHP_INT_MAX ? See Patch in attachement Test script: --------------- echo 1290594600000 % 86400000; echo "\n"; echo 1290594600000 - (floor(1290594600000/86400000)*86400000); Expected result: ---------------- 37800000 37800000 Actual result: -------------- 30811200 37800000 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=53397&edit=1

« previous php.doc.bugs (#18813) next »