pbm with SUM

pbm with SUM

am 15.07.2003 17:03:55 von Edouard Vanbelle

When I do:

mysql> select montant from virement_ordres;
+---------+
| montant |
+---------+
| 1234.4 |
| 0.5 |
+---------+
2 rows in set (0.00 sec)

mysql> select sum(montant) from virement_ordres;
+-----------------+
| sum(montant) |
+-----------------+
| 1234.9000244141 |
+-----------------+
1 row in set (0.00 sec)

Is the error of rounding normal ?

For information, I'm using:
Server version: 4.0.13-log
The server is running under linux.

And "montant" is a float.

Best regards,
Edouard.

--
OVH
140 Quai du Sartel
59100 Roubaix
France

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

Re: pbm with SUM

am 15.07.2003 20:25:26 von Alexander Keremidarski

Edouard,

Edouard Vanbelle wrote:
> When I do:
>
> mysql> select montant from virement_ordres;
> +---------+
> | montant |
> +---------+
> | 1234.4 |
> | 0.5 |
> +---------+
> 2 rows in set (0.00 sec)
>
> mysql> select sum(montant) from virement_ordres;
> +-----------------+
> | sum(montant) |
> +-----------------+
> | 1234.9000244141 |
> +-----------------+
> 1 row in set (0.00 sec)
>
> Is the error of rounding normal ?

Yes it is normal. See below




> And "montant" is a float.

This is the reason. Rounding errors are normal with floats.

When dealing with float values MySQL users floating point arithmetic which is
never precise in computers world. Or in other words it is precise up to some digit
after decimal point.

See more examples.

create table f (f float);
insert into f values (1234.4), (0.5);

mysql> select f, f+0, round(f, 20) from f;
+--------+-----------------+---------------------------+
| f | f+0 | round(f, 20) |
+--------+-----------------+---------------------------+
| 1234.4 | 1234.4000244141 | 1234.40002441406250000000 |
| 0.5 | 0.5 | 0.50000000000000000000 |
+--------+-----------------+---------------------------+


See also MySQL manual:

http://www.mysql.com/doc/en/Problems_with_float.html




> Best regards,
> Edouard.
>
> --
> OVH
> 140 Quai du Sartel
> 59100 Roubaix
> France

Best regards



--
Are you MySQL certified? -> http://www.mysql.com/certification
For technical support contracts, visit https://order.mysql.com/?ref=msal
__ ___ ___ ____ __
/ |/ /_ __/ __/ __ \/ / Mr. Alexander Keremidarski
/ /|_/ / // /\ \/ /_/ / /__ MySQL AB, Full-Time Developer
/_/ /_/\_, /___/\___\_\___/ Sofia, Bulgaria
<___/ www.mysql.com





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