Mysql SUM function returns returns wrong decimal values -


i have following table test data set:

create table `test` (   `id` int(11) not null auto_increment,   `desc` varchar(20) default null,   `amount` double default null,   primary key (`id`) ) engine=innodb auto_increment=10 default charset=utf8      insert `test` (`id`, `desc`, `amount`) values('5',null,'847.3');     insert `test` (`id`, `desc`, `amount`) values('6',null,'-847');     insert `test` (`id`, `desc`, `amount`) values('7',null,'847.3');     insert `test` (`id`, `desc`, `amount`) values('8',null,'-847');     insert `test` (`id`, `desc`, `amount`) values('9',null,'847.4'); 

so table looks like:

enter image description here

now problem when use:

select sum(amount) test;  

i following results 847.9999999999999 instead of expected 848.

any ideas why dont decimals rounded?

update:

i have tested on mysql server: 5.5.17 (windows) , mysql server: 5.5.20 centos

this problem inherent way floating-point numbers represented computer. basically, values in base 10, can written finite number of digits, cannot represented in base 2.

most of time, such approximations go unnoticed, because display small number of decimal digits. when start adding , multiplying these approximate values, error accumulates point becomes noticeable.

this why decimal type exists. represents decimal value integer, divided or multiplied power of 10. such representation, no approximation ever made.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -