note 76887 deleted from language.operators.arithmetic by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 15:06:39 +0000 |
| Subject: | note 76887 deleted from language.operators.arithmetic by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189816@lists.php.net to get a copy of this message | ||
Note Submitter: thookerov A@T gmail dot com
----
To the comment from nicholas_rainard: Your function is much more efficient, but should probably be
combined with the first two lines from the function prior to yours:
function int_divide($x, $y) {
if ($x == 0) return 0;
if ($y == 0) return FALSE;
return ($x - ($x % $y)) / $y;
}
Permits for zero in either argument. Also, this always returns the floor of the division. Probably
seems obvious to most, but it didn't to me at first. Maybe a rounding function would look like:
function up_or_dn($x, $y) {
if ($x == 0) return 0;
if ($y == 0) return FALSE;
return ($x % $y >= $y / 2) ?
(($x - ($x % $y)) / $y) + 1 : ($x - ($x % $y)) / $y;
}
Adds 1 if the mod result is more than half $y (which if my math is right means the float result is
.5+, so round up).
Simplest example: $x = 3, $y = 2.