On 7 Mar 2013, at 6:03pm, Israel Lins Albuquerque <israelin...@yahoo.com.br> 
wrote:

> But in sqlite for some reason they are:
> 3.5527136788005e-15, 0, -3.5527136788005e-15
> 
> I thing this can be a bug on calculation of doubles.

Please read these:

<http://floating-point-gui.de/basic/>
<http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/>

or any computing text book that explains the problems with floating point 
arithmetic.

On 8 Mar 2013, at 5:49pm, "Michael Black" <mdblac...@yahoo.com> wrote:

> And...the right way to do this which should work on all databases...rounding
> any answers to the max precision you asked for.
> 
> sqlite> SELECT round(22.35 - (5.45 + 16.9),2), 22.35 = round((5.45 +
> 16.9),2), round((5.45 + 16.9) - 22.35,2);
> 0.0|1|0.0

The right way is to handle those numbers as integers.  For instance, if those 
are amounts of money, you should be holding numbers of cents, not numbers of 
dollars, and store them in columns defined as INTEGER.  That way the arithmetic 
is automatically integer and will work correctly.

SELECT 22.35 - (5.45 + 16.9), 22.35 = (5.45 + 16.9), (5.45 + 16.9) - 22.35;

should be

SELECT 2235 - (545 + 1690), 2235 = (545 + 1690), (545 + 1690) - 2235;

Displaying those values as dollars, with '$' in front, or 'US$' in front, or in 
brackets if the number is negative, or in red if the number is negative, is 
about your user interface not datahandling, and belongs elsewhere in your 
software, not in your database library.

UN-altered REPRODUCTION and DISSEMINATION of this IMPORTANT Information is 
ENCOURAGED, ESPECIALLY to COMPUTER BULLETIN BOARDS.

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

Reply via email to