note 82691 added to function.round
| From: | phprookie at osu1 dot php dot net | Date: | Tue, 22 Apr 2008 11:09:08 +0000 |
| Subject: | note 82691 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138650@lists.php.net to get a copy of this message | ||
Someone stated that round() does wrong rounding, e.g.
round(3.145) -> 3.14 instead of expected 3.15
But there's a special case if the last digit is a '5', if you want to have
statistical correct values after rounding. The english term for that needed method is called
'round to even', which means to round to the nearest even number.
For example:
3.145 -> 3.1_4_ because in 3.1_5_ the 5 is not even
3.155 -> 3.1_6_ because 3.1_5_ still is not even :-)
The reason for this is explained quite simply:
Assume to have the following values
0.1, 0.2, 0.3, ... 1.7, 1.8, 1.9
The mean (or average) of this group is 1.
But what happens if you round this numbers before calculating the mean value?
0.1 to 0.4 are round down to zero, 0.6 to 0.9 are round up to 1.
1.1 to 1.4 -> 1, 1.6 to 1.9 -> 2.
Up to here the mean value is 1 (which is correct of course).
If both 0.5 and 1.5 are round up also the mean value would change (to something like 1.05....). So
the solution is to round 0.5 down to 0, and 1.5 up to 2 ... -> mean value is exactly 1 !
----
Server IP: 217.160.72.57
Probable Submitter: 130.149.41.158
----
Manual Page -- http://www.php.net/manual/en/function.round.php
Edit -- https://master.php.net/note/edit/82691
Del: integrated -- https://master.php.net/note/delete/82691/integrated
Del: useless -- https://master.php.net/note/delete/82691/useless
Del: bad code -- https://master.php.net/note/delete/82691/bad+code
Del: spam -- https://master.php.net/note/delete/82691/spam
Del: non-english -- https://master.php.net/note/delete/82691/non-english
Del: in docs -- https://master.php.net/note/delete/82691/in+docs
Del: other reasons-- https://master.php.net/note/delete/82691
Reject -- https://master.php.net/note/reject/82691
Search -- https://master.php.net/manage/user-notes.php