note 123952 deleted from language.operators.arithmetic by salathe

From: Date: Mon, 21 Sep 2020 14:58:36 +0000
Subject: note 123952 deleted from language.operators.arithmetic by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215406@lists.php.net to get a copy of this message
Note Submitter: biziclop at vipmail dot hu ---- 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 */ ?>

« previous php.notes (#215406) next »