View Ticket
Not logged in
Ticket Hash: 8dd9bfc0a3493c8a3bec41cf55333be57ac020b7
Title: spatialite tools: calculating with FLOAT or REAL number seems to add random digits after decimal
Status: Closed Type: Code_Defect
Severity: Important Priority: Immediate
Subsystem: Resolution: Works_As_Designed
Last Modified: 2022-03-07 09:30:21
Version Found In: 4.3 and 5.01
User Comments:
anonymous added on 2021-06-26 19:06:29:
I use spatialite.exe in spatialite-tools (4.3.0 and 5.0.1) and spatialite-gui (NG 5.0.0) both show an issue when entering and then comparing data of floating point or real type:
numbers entered are like this:
7.650000, 7.651000,9.765000, 9.766000, 10,505000 etc. The maximum numbers of really used digits after the decimal is 3.

What I try to do: 
I want to find all the occurrences in a same table where the difference between records is 0.001, so here for instance 7.65 and 7.651. I try to do that by self-joining the table and ask for combinations where left-side number minus right-side number is 0.001.
But when retrieving these sets, I seem to miss a lot of "duo's".
When I try to mimic the calculation with plain numbers, I see weird results.

So if I distract e.g. these numbers in spatialite.exe 
9.766000-9.765000 the result shows  0.00100000000000122
8,605-8,604 shows 0.00100000000000122
10.505-10.506 shows -0.000999999999999446
11.355-11.356 shows -0.000999999999999446
This seems like a bug. I know about data affinity and the conversion to numeric data type in the background when calculating, but this should not lead to these inpredictable results.
Numbers are entered in the database as indicated, so either 3 decimals or 6 decimals filled with 3 zero's: 7.650 or 7.650000. No additional digits after the 3rd decimal value is used.

In spatialite_gui the result will show as expected (0.001) but when retrieving the records that will have difference of 0.001, the records with differences in the "duo's" that show in spatialite.exe as having 0.00100000000000122 as difference still won't show up, so in the lower layer the same thing seems to happen.

anonymous added on 2021-10-20 15:56:18:

The sqlite3.exe is giving about similar results with values

0.000999999999999446 0.00100000000000122

The behaviour seemps to belong to SQLite https://www.sqlite.org/floatingpoint.html.


sandro added on 2022-03-07 09:30:21:

this is not a problem specifically affecting SpatiaLite. as already noted it affects also SQLite, and in more general terms any software using Floating Point arithmetics

https://en.wikipedia.org/wiki/Floating-point_arithmetic

any FP number is intrinsically affected by problems of limited precision and accuracy, and converting from binary FP to Text (and from Text to binary FP) will always introduce some unexpected truncation/approximation.

in other words: many other DBMS support a DECIMAL datatype that's intended for extreme precision at the cost of a substantial slowness. SQLite simply supports the FP (Double) datatype, that's faster but affected by rounding and truncation effects.