472,958 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 2264
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matias Silva | last post by:
Can anybody tell me why I am getting rounding errors using the ROUND function. 3.7125 rounds to 3.70 when I use the following: TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2))) The correct...
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
17
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
4
by: Fuzzydave | last post by:
I have been using a round command in a few places to round a value to zero decimal places using the following format, round('+value+', 0) but this consistantly returns the rounded result of...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
7
by: kkmigas | last post by:
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555 -".round(20.555,2)."<br>"; echo "number_format 20.545...
3
by: Krishna.K.1900 | last post by:
Does round() always perfectly return the output expected or are there some artifacts which don't allow perfect functionality Using python 2.5: 12.23 12.234 12.199999999999999 but was...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
by: josh logan | last post by:
Hello, I need a round function that _always_ rounds to the higher integer if the argument is equidistant between two integers. In Python 3.0, this is not the advertised behavior of the built-in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.