note 123952 added to language.operators.arithmetic

From: Date: Tue, 18 Jun 2019 18:18:37 +0000
Subject: note 123952 added to language.operators.arithmetic
Groups: php.notes 
Request: Send a blank email to php-notes+get-211305@lists.php.net to get a copy of this message
For positive modulos, here is a mathematically proper modulo which actually works with negative integers. <?php // Inline: $v is value to be divided, $m is the modulus $remainder = ( $v % $m + $m ) % $m; // Or as a function: function modulo( $value, $modulus ){ return ( $value % $modulus + $modulus ) % $modulus; } // Test: for( $x = -6; $x <= 6; ++$x ) echo $x, "\t", modulo( $x, 3 ), "\t<br>\n"; /* prints: -6 0 -5 1 -4 2 -3 0 -2 1 -1 2 0 0 1 1 2 2 3 0 */ ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.195 (proxied: 188.143.70.21) ---- Manual Page -- http://php.net/manual/en/language.operators.arithmetic.php Edit -- https://master.php.net/note/edit/123952 Del: integrated -- https://master.php.net/note/delete/123952/integrated Del: useless -- https://master.php.net/note/delete/123952/useless Del: bad code -- https://master.php.net/note/delete/123952/bad+code Del: spam -- https://master.php.net/note/delete/123952/spam Del: non-english -- https://master.php.net/note/delete/123952/non-english Del: in docs -- https://master.php.net/note/delete/123952/in+docs Del: other reasons-- https://master.php.net/note/delete/123952 Reject -- https://master.php.net/note/reject/123952 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211305) next »