[sqlite] Problem with accumulating decimal values

2015-12-18 Thread Tim Streater
On 18 Dec 2015 at 00:06, Keith Medcalf wrote: >> I was taught "Round [only] before printing.". These days it would be >> something like "Round [only] before your API returns to the calling >> program. > > Those are not the same. Round only before printing (whether to the screen or > to a

[sqlite] Problem with accumulating decimal values

2015-12-18 Thread Klaas V
On Thu, 17 Dec 2015 13:13:24 Simon wrote: >to round only when you need to be handling integers. Round the result to maximum two digits after the decimal point (or comma) when it handles about money. Kind regards |?Cordiali saluti | Vriendelijke groeten | Freundliche Gr?sse, Klaas `Z4us` V ?-

[sqlite] Problem with accumulating decimal values

2015-12-17 Thread Keith Medcalf
> I was taught "Round [only] before printing.". These days it would be > something like "Round [only] before your API returns to the calling > program. Those are not the same. Round only before printing (whether to the screen or to a printer). In other words, rounding is a way to make things

[sqlite] Problem with accumulating decimal values

2015-12-17 Thread Simon Slavin
On 17 Dec 2015, at 4:22am, R Smith wrote: > it was a seemingly too-convoluted detour for simply saying: "Don't store > rounded numbers. Round only the results." I was taught "Round [only] before printing.". These days it would be something like "Round [only] before your API returns to the

[sqlite] Problem with accumulating decimal values

2015-12-17 Thread R Smith
On 2015/12/17 3:26 AM, James K. Lowden wrote: >> Calculated errors are fine because we can at any time revisit the >> calculation procedures, we can refine and perhaps opt for more >> significant digits - but we can ALWAYS guarantee the accuracy-level >> of the calculated result. However,

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread R Smith
On 2015/12/16 4:05 PM, E.Pasma wrote: > 16 dec 2015, Keith Medcalf: >>> BEGIN; >>> UPDATE fmtemp SET bal = ROUND(bal,2) + 123.45; >>> (repeat a 1.000.001 times >>> END; >>> SELECT bal FROM fmtemp; >>> 123450123.45 >> >> You should NEVER round as you have done above. You may get lucky and >>

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread James K. Lowden
On Wed, 16 Dec 2015 15:05:34 +0100 "E.Pasma" wrote: > 16 dec 2015, Keith Medcalf: > >> BEGIN; > >> UPDATE fmtemp SET bal = ROUND(bal,2) + 123.45; > >> (repeat a 1.000.001 times > >> END; > >> SELECT bal FROM fmtemp; > >> 123450123.45 > > > > You should NEVER round as you have done above. You

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread James K. Lowden
On Wed, 16 Dec 2015 20:33:40 +0200 R Smith wrote: > > Ok this does not work of any scale of numbers. But a solution with > > integers neither does > > I think the bit that Keith tried to highlight is that we should > always refrain from storing errors. Keith recommended against storing

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread E.Pasma
16 dec 2015, 16:17, Bernardo Sulzbach: > > On Wed, Dec 16, 2015 at 12:05 PM, E.Pasma wrote: > >> Ok this does not work of any scale of numbers. But a solution with >> integers >> neither does >> >> E.Pasma >> > ...I like integer better than floating points and text for > currencies ... Good

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread Simon Slavin
On 16 Dec 2015, at 3:46pm, Adam Devita wrote: > As a matter of interest, when calculating interest on a sum of money > expressed in pennies, how do you handle int arithmetic truncating? > Is that an accounting design rule thing when dealing with fractions of > a penny to round? When writing

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread E.Pasma
16 dec 2015, Keith Medcalf: >> BEGIN; >> UPDATE fmtemp SET bal = ROUND(bal,2) + 123.45; >> (repeat a 1.000.001 times >> END; >> SELECT bal FROM fmtemp; >> 123450123.45 > > You should NEVER round as you have done above. You may get lucky > and the errors may cancel each other out, or you may get

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread Bernardo Sulzbach
On Wed, Dec 16, 2015 at 1:54 PM, Simon Slavin wrote: > > On 16 Dec 2015, at 3:46pm, Adam Devita wrote: > > When writing accounting software, there will be a specific rule for rounding > attached to each calculation. For instance a process for working out a > mortgage will include its own

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread Bernardo Sulzbach
On Wed, Dec 16, 2015 at 9:43 AM, Keith Medcalf wrote: > >> Hello, so in short, rounding the column anywhere it is used, is >> another solution. I confirmed this below. Thanks, E. Pasma. >> >> BEGIN; >> UPDATE fmtemp SET bal = ROUND(bal,2) + 123.45; >> (repeat a 1.000.001 times >> END; >> SELECT

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread Domingo Alvarez Duarte
Hello ! I said once and I'll say again for some applications it would make sense to use _Decimal64 (_Decimal32, _Decimal128) instead of floating points. Even if it's done in software the performance is acceptable on most common cases. See a sqlite3.c/sqlite3.h modified to use "_Decimal64"

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread E.Pasma
16 dec 2015, James K. Lowden: > On Fri, 11 Dec 2015 16:21:30 +0200 > "Frank Millman" wrote: > >> sqlite> UPDATE fmtemp SET balance = balance + 123.45; >> sqlite> SELECT bal FROM fmtemp; >> 5925.599 > > To a question like that you'll receive a lot of answers about > numerical >

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread Adam Devita
Good day, As a matter of interest, when calculating interest on a sum of money expressed in pennies, how do you handle int arithmetic truncating? Is that an accounting design rule thing when dealing with fractions of a penny to round? Is this an arbitrary quantization? Once upon a time there

[sqlite] Problem with accumulating decimal values

2015-12-16 Thread Keith Medcalf
> Hello, so in short, rounding the column anywhere it is used, is > another solution. I confirmed this below. Thanks, E. Pasma. > > BEGIN; > UPDATE fmtemp SET bal = ROUND(bal,2) + 123.45; > (repeat a 1.000.001 times > END; > SELECT bal FROM fmtemp; > 123450123.45 Absolutely not! You should

[sqlite] Problem with accumulating decimal values

2015-12-15 Thread James K. Lowden
On Fri, 11 Dec 2015 16:21:30 +0200 "Frank Millman" wrote: > sqlite> UPDATE fmtemp SET balance = balance + 123.45; > sqlite> SELECT bal FROM fmtemp; > 5925.599 To a question like that you'll receive a lot of answers about numerical accuracy. And it's true that there are ways to "do the

[sqlite] Problem with accumulating decimal values

2015-12-12 Thread Bernardo Sulzbach
On Sat, Dec 12, 2015 at 6:51 AM, Darren Duncan wrote: > > Per another suggestion, the best workaround is to use an INTEGER type > instead, and store an even multiple of whatever your smallest currency unit > size is, eg cents rather than dollars. > As I understood, he is doing the math in

[sqlite] Problem with accumulating decimal values

2015-12-12 Thread Frank Millman
From: Frank Millman Sent: Friday, December 11, 2015 4:21 PM To: sqlite-users at mailinglists.sqlite.org Subject: [sqlite] Problem with accumulating decimal values > Hi all > > I am having a problem accumulating decimal values. > > I am actually using Python, but

[sqlite] Problem with accumulating decimal values

2015-12-12 Thread Domingo Alvarez Duarte
Hello !then I did a modification to sqlite3 that basically define a new type "sqlite_double" and use it instead of "double" (#define sqlite_double? double) then I can redefine it to _Decimal64 (#define sqlite_double? _Decimal64) this way with a modern C compiler we can have sqlite3 using

[sqlite] Problem with accumulating decimal values

2015-12-12 Thread Darren Duncan
Frank, The problem you are having is due to SQLite not following the SQL standard regarding non-integral numeric types. The SQL standard specifies that the DECIMAL type is exact numeric and able to represent decimal numbers exactly. However, when you ask SQLite for a DECIMAL column, that is

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread Frank Millman
Hi all I am having a problem accumulating decimal values. I am actually using Python, but I can reproduce it in the sqlite3 interactive terminal. SQLite version 3.8.6 2014-08-15 11:46:33 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread Nelson, Erik - 2
Frank Millman Friday, December 11, 2015 9:21 AM > > I am having a problem accumulating decimal values. > > sqlite> UPDATE fmtemp SET balance = balance + 123.45; SELECT bal FROM > sqlite> fmtemp; > 6049.049 > > Can anyone explain what is going o

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread Rob Willett
I *think* this is due to you creating an integer when you first create the entries Try changing from INSERT INTO fmtemp VALUES (1, 0); to INSERT INTO fmtemp VALUES (1, 0.0); Just did macpro:js rwillett$ sqlite3 SQLite version 3.8.10.2 2015-05-20 18:17:19 Enter ".help" for usage hints.

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread Bernardo Sulzbach
Looks like you thought you could have a DECIMAL type (such as MySQL DECIMAL) here. But SQLite does not allow for this. My workaround usually is: create table accounts(account_number integer, balance integer); create view accounts_view as select account_number, balance / 100.0 from

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread Bernardo Sulzbach
Hi Frank, You want to store an INTEGER type using the lowest used unit (cents or mills). This page https://www.sqlite.org/datatype3.html may be of assistance next time -- Bernardo Sulzbach

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread Scott Robison
On Fri, Dec 11, 2015 at 8:18 AM, Adam Devita wrote: > A good start at the long answer can be found in the archives of this list. > > > http://sqlite.1065341.n5.nabble.com/Simple-Math-Question-td85140.html#a85157 > also found at > > https://www.mail-archive.com/sqlite-users at >

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread Adam Devita
A good start at the long answer can be found in the archives of this list. http://sqlite.1065341.n5.nabble.com/Simple-Math-Question-td85140.html#a85157 also found at https://www.mail-archive.com/sqlite-users at mailinglists.sqlite.org/msg04587.html (web search sqlite "simple math question") It

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread Richard Hipp
On 12/11/15, Frank Millman wrote: > > Can anyone explain what is going on, and is there a way to avoid it? > Short answer: https://www.sqlite.org/faq.html#q16 I don't have a longer answer readily at hand, but as questions about floating point numbers come up a lot, probably I should write up a

[sqlite] Problem with accumulating decimal values

2015-12-11 Thread John McKown
On Fri, Dec 11, 2015 at 8:21 AM, Frank Millman wrote: > Hi all > > I am having a problem accumulating decimal values. > > I am actually using Python, but I can reproduce it in the sqlite3 > interactive terminal. > > SQLite version 3.8.6 2014-08-15 11:46:33 > Enter ".help" for usage hints. >