note 82691 deleted from function.round by danbrown

From: Date: Sun, 17 May 2009 16:14:10 +0000
Subject: note 82691 deleted from function.round by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154772@lists.php.net to get a copy of this message
Note Submitter: php rookie ---- 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 !

« previous php.notes (#154772) next »