Round Function Error

Round Function Error

am 11.11.2003 14:24:23 von Mark Hedges

------=_NextPart_000_007D_01C3A857.1F4F07C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

I'm using mysql 4.0.16-standard on Linux.

I've just noticed this:

mysql> select round(0.5);
+------------+
| round(0.5) |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)

--

mysql> select round(0.500001);
+-----------------+
| round(0.500001) |
+-----------------+
| 1 |
+-----------------+
1 row in set (0.00 sec)

--

Is it not the case that 0.5 should be rounded UP?

--
Mark
------=_NextPart_000_007D_01C3A857.1F4F07C0--

Re: Round Function Error

am 11.11.2003 16:49:41 von Georg Richter

Hi,

=46rom http://www.mysql.com/doc/en/Mathematical_functions.html:

Note that the behaviour of ROUND() when the argument is half way between tw=
o=20
integers depends on the C library implementation. Some round to the nearest=
=20
even number, always up, always down, or always toward zero. If you need one=
=20
kind of rounding, you should use a well-defined function like TRUNCATE() or=
=20
=46LOOR() instead.=20

Regards

Georg

> Hi,
>
> I'm using mysql 4.0.16-standard on Linux.
>
> I've just noticed this:
>
> mysql> select round(0.5);
> +------------+
>
> | round(0.5) |
>
> +------------+
>
> | 0 |
>
> +------------+
> 1 row in set (0.00 sec)
>
> --
>
> mysql> select round(0.500001);
> +-----------------+
>
> | round(0.500001) |
>
> +-----------------+
>
> | 1 |
>
> +-----------------+
> 1 row in set (0.00 sec)
>
> --
>
> Is it not the case that 0.5 should be rounded UP?
>
> --
> Mark


--
MySQL Bugs Mailing List
For list archives: http://lists.mysql.com/bugs
To unsubscribe: http://lists.mysql.com/bugs?unsub=3Dgcdmb-bugs@m.gmane.org

Re: Round Function Error

am 12.11.2003 17:27:45 von indrek siitan

Hi,

> I'm using mysql 4.0.16-standard on Linux.
>=20
> I've just noticed this:
>=20
> mysql> select round(0.5);
> +------------+
> | round(0.5) |
> +------------+
> | 0 |
> +------------+
> 1 row in set (0.00 sec)
>=20
> --
>=20
> mysql> select round(0.500001);
> +-----------------+
> | round(0.500001) |
> +-----------------+
> | 1 |
> +-----------------+
> 1 row in set (0.00 sec)
>=20
> --
>=20
> Is it not the case that 0.5 should be rounded UP?

MySQL uses the mathematical functions of the underlying operating systems
for these kinds of operations, so it depends on how OS has solved them.

In case of rounding, there are multiple possible standards defined on how t=
o
round a number exactly half way between. Most of the operating systems,
including Linux, seem to use the one that tells to round to the nearest eve=
n
number, which in your case is 0.

To demostrate this:

mysql> select round(3.5);
+------------+
| round(3.5) |
+------------+
| 4 |
+------------+
1 row in set (0.01 sec)

mysql> select round(4.5);
+------------+
| round(4.5) |
+------------+
| 4 |
+------------+
1 row in set (0.00 sec)



Rgds,
Indrek

--=20
| Indrek Siitan, MySQL AB, Support Engineer & Bugmaster
| Uuem=F5isa, Haapsalu, Estonia
+-=20
| Are you MySQL Certified? http://www.mysql.com/certification/
--


--
MySQL Bugs Mailing List
For list archives: http://lists.mysql.com/bugs
To unsubscribe: http://lists.mysql.com/bugs?unsub=3Dgcdmb-bugs@m.gmane.org