468,247 Members | 1,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

Possible bug in round function

PHP 5.0.5 running on Win2k Server

$rate = 0.015;
$value = 15;

echo $rate * $value;
echo "<BR>";
echo round($rate * $value , 2);

output:

0.225
0.22

Shouldn't this be?
0.225
0.23

Feb 13 '06 #1
4 1709
Little update

if I added echo "<BR>" echo round(.225, 2) the output is .23

Feb 13 '06 #2
On 13 Feb 2006 12:29:18 -0800, "Wescotte" <we******@earthlink.net> wrote:
PHP 5.0.5 running on Win2k Server

$rate = 0.015;
$value = 15;

echo $rate * $value;
echo "<BR>";
echo round($rate * $value , 2);

output:

0.225
0.22

Shouldn't this be?
0.225
0.23


On 5.1.2 it does output 0.23.

Perhaps see http://bugs.php.net/bug.php?id=24142

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Feb 13 '06 #3
Andy Hassall wrote:
On 5.1.2 it does output 0.23.


Not on my WinXP installation, which produces 0.22

One of the related bug reports (http://bugs.php.net/bug.php?id=35986)
mentions that this isn't a bug, but due to the limited precision of floats.
Perhaps it's processor related...
JW
Feb 13 '06 #4
On Mon, 13 Feb 2006 22:24:45 +0100, "Janwillem Borleffs" <jw@jwscripts.com>
wrote:
Andy Hassall wrote:
On 5.1.2 it does output 0.23.


Not on my WinXP installation, which produces 0.22

One of the related bug reports (http://bugs.php.net/bug.php?id=35986)
mentions that this isn't a bug, but due to the limited precision of floats.
Perhaps it's processor related...


Hum, 0.23 on Linux, 0.22 on Windows 2000. Both are Intel chips. Looks like
there's Windows-specific code in ext/standard/round.c :

#ifndef PHP_ROUND_FUZZ
# ifndef PHP_WIN32
# define PHP_ROUND_FUZZ 0.50000000001
# else
# define PHP_ROUND_FUZZ 0.5
# endif
#endif

#define PHP_ROUND_WITH_FUZZ(val, places) { \
double tmp_val=val, f = pow(10.0, (double) places); \
tmp_val *= f; \
if (tmp_val >= 0.0) { \
tmp_val = floor(tmp_val + PHP_ROUND_FUZZ); \
} else { \
tmp_val = ceil(tmp_val - PHP_ROUND_FUZZ); \
} \
tmp_val /= f; \
val = !zend_isnan(tmp_val) ? tmp_val : val; \
} \
Shame there's no comments explaining what this is there for.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Feb 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by joeandtel | last post: by
2 posts views Thread by Matias Silva | last post: by
3 posts views Thread by shank | last post: by
3 posts views Thread by ORC | last post: by
17 posts views Thread by nomenklatura | last post: by
9 posts views Thread by Ronald W. Roberts | last post: by
3 posts views Thread by Altman | last post: by
9 posts views Thread by josh logan | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.