Skip to content
Advertisement

MariaDB SUM returns wrong value on column type float

version: 5.5.53-MariaDB

I added this table to my MariaDB:

and add an entry:

and when I do sum on all columns

result:

How do I get the wrong value in sum(val_float) (9600239616 instead of 9600240000) and right values in the other columns

Thanks

Advertisement

Answer

9600240000 is a number which can’t be represented exactly by float datatype (IEEE 754 32-bit single precision)

Simple example in C:

When running it, it will produce the following output:

The binary representation of 9600240000 is 1000111100001110000000100110000000 (34-bits) – even if you would try to store this value in a 32-bit integer, it wouldn’t work. If you need exact results, you should use the DECIMAL column type.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement