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 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/
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/
"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 ?
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 ?
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 =--
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
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]
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... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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 !!!!
|
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...
|
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=()=>{
|
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
|
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...
| |