470,815 Members | 1,136 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

PHP and round()

I'm ok with :
round(118.5) => 119
round(118.15, 1) => 118.2

But why :
round(118.815, 2) => 118.81 ???
why not 118.82 ?

Thanks for Help


Jul 17 '05 #1
8 2179
On Fri, 20 Feb 2004 12:08:07 +0100, Guido Braceletti wrote:
I'm ok with :
round(118.5) => 119
round(118.15, 1) => 118.2

But why :
round(118.815, 2) => 118.81 ???
why not 118.82 ?

Thanks for Help

That's perfectly correct.. according to what I learnt in my Maths
class at school anyway.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2
On Fri, 20 Feb 2004 11:16:13 +0000, Ian.H wrote:
On Fri, 20 Feb 2004 12:08:07 +0100, Guido Braceletti wrote:
I'm ok with :
round(118.5) => 119
round(118.15, 1) => 118.2

But why :
round(118.815, 2) => 118.81 ???
why not 118.82 ?

Thanks for Help

That's perfectly correct.. according to what I learnt in my Maths class at
school anyway.

[ injects more coffee ]
I misread your values above when replying Guido.. the above works fine on
this box:
[tk@hybris:~]$ cat ./round.php
<?php
echo round(118.815, 2), "\n";
?>
[tk@hybris:~]$ php -e ./round.php
118.82

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #3

"Ian.H" <ia*@WINDOZEdigiserv.net> a écrit dans le message de news:
pa****************************@hybris.digiserv.net...
On Fri, 20 Feb 2004 11:16:13 +0000, Ian.H wrote:
On Fri, 20 Feb 2004 12:08:07 +0100, Guido Braceletti wrote:
I'm ok with :
round(118.5) => 119
round(118.15, 1) => 118.2

But why :
round(118.815, 2) => 118.81 ???
why not 118.82 ?

Thanks for Help

That's perfectly correct.. according to what I learnt in my Maths class at school anyway.

[ injects more coffee ]
I misread your values above when replying Guido.. the above works fine on
this box:
[tk@hybris:~]$ cat ./round.php
<?php
echo round(118.815, 2), "\n";
?>
[tk@hybris:~]$ php -e ./round.php
118.82

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/


Then it's a bug in PHP 4.3.1 ?

Jul 17 '05 #4
No, it's the way it goes with floating point arithmatic. 118.815 is stored
as:
64 + 32 + 16 + 4 + 2 + 1/2 + 1/4 + 1/16 + 1/512 + 1/2048 + 1/32768 + 1/65536
+ 1/131072. If this is stored as a 32-bit float, that's all that represents
it, and the number is under-represented by 0.00000518798828125 which would
make it round down. If it's a 64-bit double, it will have a few more
(smaller) terms, which will reduce the error, but will still not represent
it exactly. This number cannot be exactly represented in binary. So it's
somewhat implementation-specific whether this will be over or under, but the
fact of the matter is that you should never expect something to be exactly
correct when it comes to floating point. Hence rounding at <something>5 is
not always doable. (The reason plain-old 118.5 works is that it's
represented as 64 + 32 + 16 + 4 + 2 + 1/2 - it can be exactly represented in
floating point.)

-Ian
Guido Braceletti wrote:
"Ian.H" <ia*@WINDOZEdigiserv.net> a écrit dans le message de news:
pa****************************@hybris.digiserv.net...
On Fri, 20 Feb 2004 11:16:13 +0000, Ian.H wrote:
On Fri, 20 Feb 2004 12:08:07 +0100, Guido Braceletti wrote:

I'm ok with :
round(118.5) => 119
round(118.15, 1) => 118.2

But why :
round(118.815, 2) => 118.81 ???
why not 118.82 ?

Thanks for Help
That's perfectly correct.. according to what I learnt in my Maths
class at school anyway.

[ injects more coffee ]
I misread your values above when replying Guido.. the above works
fine on this box:
[tk@hybris:~]$ cat ./round.php
<?php
echo round(118.815, 2), "\n";

[tk@hybris:~]$ php -e ./round.php
118.82

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/


Then it's a bug in PHP 4.3.1 ?

Jul 17 '05 #5
Guido Braceletti wrote:

"Ian.H" <ia*@WINDOZEdigiserv.net> a écrit dans le message de news:
pa****************************@hybris.digiserv.net...
> On Fri, 20 Feb 2004 12:08:07 +0100, Guido Braceletti wrote:
>> round(118.815, 2) => 118.81 ???
>> why not 118.82 ?
the above works fine on this box:

[tk@hybris:~]$ cat ./round.php
<?php
echo round(118.815, 2), "\n";
?>
[tk@hybris:~]$ php -e ./round.php
118.82


Then it's a bug in PHP 4.3.1 ?


You cannot rely on the internal (binary) representation of
floating-point numbers to be accurate.

For all we know 118.815 may be represented as 118.81499999999999953071
in your machine/php and as 118.81500000000000007038 in Ian's machine
(as well as mine)

To get the expected result add a small amount to value you are rounding:

<?php
$value = 118.815;
echo round($value+0.000000001, 2);
?>
HTH
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #6
Agelmar wrote:
No, it's the way it goes with floating point arithmatic. 118.815 is stored
as:
64 + 32 + 16 + 4 + 2 + 1/2 + 1/4 + 1/16 + 1/512 + 1/2048 + 1/32768 + 1/65536
+ 1/131072. If this is stored as a 32-bit float, that's all that represents
it, and the number is under-represented by 0.00000518798828125 which would
make it round down. If it's a 64-bit double, it will have a few more
(smaller) terms, which will reduce the error, but will still not represent
it exactly. This number cannot be exactly represented in binary. So it's
somewhat implementation-specific whether this will be over or under, but the
fact of the matter is that you should never expect something to be exactly
correct when it comes to floating point. Hence rounding at <something>5 is
not always doable. (The reason plain-old 118.5 works is that it's
represented as 64 + 32 + 16 + 4 + 2 + 1/2 - it can be exactly represented in
floating point.)


I don't know if your answer is one of Genius or if you're sad for
knowing all this - but I will admit I am impressed!

randelld
Jul 17 '05 #7
In article <pa****************************@hybris.digiserv.ne t>,
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote:
On Fri, 20 Feb 2004 11:16:13 +0000, Ian.H wrote:
On Fri, 20 Feb 2004 12:08:07 +0100, Guido Braceletti wrote:
I'm ok with :
round(118.5) => 119
round(118.15, 1) => 118.2

But why :
round(118.815, 2) => 118.81 ???
why not 118.82 ?

Thanks for Help

That's perfectly correct.. according to what I learnt in my Maths class at
school anyway.

[ injects more coffee ]
I misread your values above when replying Guido.. the above works fine on
this box:
[tk@hybris:~]$ cat ./round.php
<?php
echo round(118.815, 2), "\n";
?>
[tk@hybris:~]$ php -e ./round.php
118.82


It doesn't work for me:
php -r "print round(118.815, 2);" 118.81
php -v

PHP 4.3.0 (cli) (built: Jan 7 2003 11:02:53)
Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies

--
Sandman[.net]
Jul 17 '05 #8

Uzytkownik "Pedro Graca" <he****@hotpop.com> napisal w wiadomosci
news:c1*************@ID-203069.news.uni-berlin.de...
You cannot rely on the internal (binary) representation of
floating-point numbers to be accurate.

For all we know 118.815 may be represented as 118.81499999999999953071
in your machine/php and as 118.81500000000000007038 in Ian's machine
(as well as mine)


Looks like the difference is whether the number is stored as a single float
or a double float. On my computer

$a = unpack("fn", (pack("f", 118.815)));
echo sprintf("%2.32f", $a['n']);

prints 118.81500244140625000000000000000000, while

$b = unpack("dn", (pack("d", 118.815)));
echo sprintf("%2.32f", $b['n']);

prints 118.81499999999999772626324556767941.

PHP could really use a fixed-point numeric type...
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Matias Silva | last post: by
17 posts views Thread by nomenklatura | last post: by
9 posts views Thread by Ronald W. Roberts | last post: by
4 posts views Thread by Fuzzydave | last post: by
10 posts views Thread by David Coleman | last post: by
7 posts views Thread by kkmigas | last post: by
3 posts views Thread by Krishna.K.1900 | last post: by
4 posts views Thread by =?Utf-8?B?UmVuZQ==?= | last post: by
9 posts views Thread by josh logan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.