note 110824 added to function.fmod
| From: | verdy_p | Date: | Wed, 12 Dec 2012 02:38:50 +0000 |
| Subject: | note 110824 added to function.fmod | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192481@lists.php.net to get a copy of this message | ||
Note that fmod is NOT equivalent to this basic function:
function modulo($a, $b) {
return $a - $b * floor($a / $b);
}
because fmod() will return a value with the same sign as $a. In other words the floor() function is
not correct as it rounds towards -INF instead of towards zero.
To emulate fmod($a, $b) the correct way is:
function fmod($a, $b) {
return $a - $b * (($b < 0) ? ceil($a / $b) : floor($a / $b)));
}
Note that both functions will throw a DIVISION BY ZERO if $b is null.
The first function modulo() above is the mathematical function which is useful for working on cyclic
structures (such as calender computions or trignonometric functions :
- fmod($a, 2*PI) returns a value in [0..2*PI) if $a is positive
- fmod($a, 2*PI) returns a value in [-2*PI..0] if $a is negative
- modulo($a, 2*PI) returns a value always in [0..2*PI) independantly of the sign of $a
----
Server IP: 69.147.83.199
Probable Submitter: 72.52.91.2 (proxied: 89.2.191.39)
----
Manual Page -- http://php.net/manual/en/function.fmod.php
Edit -- https://master.php.net/note/edit/110824
Del: integrated -- https://master.php.net/note/delete/110824/integrated
Del: useless -- https://master.php.net/note/delete/110824/useless
Del: bad code -- https://master.php.net/note/delete/110824/bad+code
Del: spam -- https://master.php.net/note/delete/110824/spam
Del: non-english -- https://master.php.net/note/delete/110824/non-english
Del: in docs -- https://master.php.net/note/delete/110824/in+docs
Del: other reasons-- https://master.php.net/note/delete/110824
Reject -- https://master.php.net/note/reject/110824
Search -- https://master.php.net/manage/user-notes.php