Re: [PHP] Round() behaviour issue

2003-12-16 Thread Eugene Lee
On Tue, Dec 16, 2003 at 11:13:25AM +, Scott McDaid wrote:
: 
: Hi there. I've been looking at the behaviour of the round functionality in 
: PHP. We're currently still using v4.2.3, (but the documentation seems to 
: suggest it's the same for versions after this).
: 
: Doing the following rounds always rounds *up* to the nearest whole number. 
: i.e.
: 
: 0.5 = 1
: 1.5 = 2
: 2.5 = 3
: 3.5 = 4

Any non-zero fractional component that gets rounded to the next whole
number is done with ceil().

: In earlier PHP docs, it stated that halves would be rounded to the nearest 
: even number.
: i.e. 
: 
: 3.5 = 4
: 4.5 = 4

I recall seeing those docs and getting very confused, especially with
your listed example.  Personally, I think the current behavior for
round() is mathematically correct; i.e. any number whose fractional
component is greater-than or equal-to 1/2 is rounded up to the next
whole number, otherwise it's rounded down.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Round() behaviour issue

2003-12-16 Thread Ford, Mike [LSS]
On 16 December 2003 11:13, Scott McDaid wrote:

 Hi there. I've been looking at the behaviour of the round
 functionality in PHP. We're currently still using v4.2.3, (but the
 documentation seems to
 suggest it's the same for versions after this).
 
 Doing the following rounds always rounds *up* to the nearest
 whole number.
 i.e.
 
 0.5 = 1
 1.5 = 2
 2.5 = 3
 3.5 = 4
 
 In earlier PHP docs, it stated that halves would be rounded
 to the nearest
 even number.
 i.e.
 
 3.5 = 4
 4.5 = 4
 
 When did this change. Or is this *actually* the case in a
 version of PHP
 later than 4.2.3. I've had a look at the PHP changelog and
 the bug lists
 and found nothing. :(

From a quick peruse of the source in CVS, round() seems to have been
intended to round positive halves up and negative halves down since at least
PHP 4.0.0 -- although a bug-fix in 4.3.3 seems to suggest this might not
always have been happening in reality.  It seems likely that the reference
to rounding halves to the nearest even number was an error in the
documentation which has been fixed by removing it!

(http://bugs.php.net/bug.php?id=25694, which looks like it might be
relevant, is actually fixed by changing the behaviour of number_format()!)

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php