473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2278
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.