Round diffecrence between MariaDB and SQLite

Round diffecrence between MariaDB and SQLite

Postby Marc Vanzegbroeck » Thu Apr 13, 2017 12:33 pm

Hello,

I just found a strange difference in the round()-function between MariaDB and SQlite

If a field 'bedrag' = 26.90 for factuurnr = 20170001
and we run
Code: Select all  Expand view  RUN
select round(bedrag*0.85,2) from kasv for factuurnr = 20170001

the result with SQLite = 22.86, and with MariaDB it is 22.87 :shock:

22.87 seems to be correct, since it's the same result as in (x)Harbour and foxpro
Does anyone know why, because it's a big problem, since in my program I use sometimes calculations on DBF-files , and sometimes SQL-query's.
It's only correct when I use MariaDB.
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Round diffecrence between MariaDB and SQLite

Postby Enrico Maria Giordano » Thu Apr 13, 2017 12:38 pm

The result is 22.865, so 22.87 is correct. It looks like a bug in SQLite. As a workaround, you can select bedrag and make the calculation later in your code.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Round diffecrence between MariaDB and SQLite

Postby Marc Vanzegbroeck » Thu Apr 13, 2017 12:57 pm

It's seems to be a SQLite know problem.
From the SQLite-FAQ:
Why does ROUND(9.95,1) return 9.9 instead of 10.0? Shouldn't 9.95 round up?

SQLite uses binary arithmetic and in binary, there is no way to write 9.95 in a finite number of bits. The closest to you can get to 9.95 in a 64-bit IEEE float (which is what SQLite uses) is 9.949999999999999289457264239899814128875732421875. So when you type "9.95", SQLite really understands the number to be the much longer value shown above. And that value rounds down.

This kind of problem comes up all the time when dealing with floating point binary numbers. The general rule to remember is that most fractional numbers that have a finite representation in decimal (a.k.a "base-10") do not have a finite representation in binary (a.k.a "base-2"). And so they are approximated using the closest binary number available. That approximation is usually very close, but it will be slightly off and in some cases can cause your results to be a little different from what you might expect.
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Silvio.Falconi and 42 guests