Re: round behavior changed

From: Date: Sat, 20 May 2006 17:46:35 +0000
Subject: Re: round behavior changed
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-236557@lists.php.net to get a copy of this message
At 3:17 PM -0500 5/19/06, Richard Lynch wrote:
On Fri, May 19, 2006 1:27 pm, Sébastien Guay wrote:
I searched the archives but did not found anything. It seem that the round function behavior has changed in php 4.4.x (I don't have 4.4.1 handy for testing). In 4.3.11, the output of round("23.005",2) is 23.01 which is mathematically correct. In 4.4.2, the output is 23 which is not correct. The output of round("23.0051",2) is correct though (23.01). Is there a workaround/patch for this?
The correctness of rounding kinda depends on which mathematical/historical model one subscribes to. There is at least one statistician's model of 'round' wherein a trailing '5' goes 'up' for preceding even numbers and 'down' for preceding odd numbers, to provide more symmetry in statistical analysis of rounding. Or, at least, that's how I recall it.
Your recall is correct. The reasoning is that for a trailing 1-4, you round down; for 6-9, you round up; and for 0 you don't do anything, but for five it becomes a statistical problem as to which way you round. If you always round up, then the bias is up. If you always round down (which I believe php does), then the bias is down. However, if you use the statistician's model (up for even and down for odd whole numbers), then rounding errors are reduced as compared to other methods, but they are not eliminated. tedd -- ------------------------------------------------------------------------------------ http://sperling.com http://ancientstones.com http://earthstones.com

« previous php.general (#236557) next »