On 17 avr. 2012, at 11:35, Richard Hipp wrote:

On Tue, Apr 17, 2012 at 5:12 AM, Paxdo Presse 
<pa...@mac.com<mailto:pa...@mac.com>> wrote:


Hi,

How point numbers are they stored in sqlite?

In a field with REAL affinity:

round(11.578767 / 2 , 4) is displayed "5.7894" in SQLite/Navicat :-),
but "5,7893999999999997" in the cursor of my development language
when I get it to sqlite.

Internally, SQLite works with 5.7894 or 5,7893999999999997?


Neither.  SQLite uses double-precision IEEE floating point numbers.  And
the value 5.7894 is not representable as an IEEE float.  The closest you
can get is 5.78939999999999965751840136363171041011810302734375.  SQLite
only attempts to preserve the first 15 significant digits of a floating
point value, so SQLite is likely to print that number as 5.7894.  But if
your development language tries to show 16 digits of precision (as
apparently it does) then you might see the second value.

Key point:  Floating point numbers are approximations.  This is an inherent
property of IEEE floating point numbers, not a limitation of SQLite.  If
you need an exact answer, use integers.


Perhaps it's worth mentioning again David Goldberg's seminal paper "What Every 
Computer Scientist Should Know About Floating-Point Arithmetic". That paper is 
so well known it's been reprinted many times and is available in many places on 
the internet. For example Oracle put a reprint in its documentation:

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

You could do worse than reading it. Twice.

Jean-Denis

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to