SQLite Forum

Sum precision ?
After looking at func.c and changing the structure SumCtx to use LONGDOUBLE_TYPE as used in printf.c the result indeed improve,but it goes a bit over the result of decimal_sum (after fixing my mistakes on the numbers, and the alternation on the big/small numbers are intentional for the testing):
create table S(
	id integer primary key not null,
	value real

  cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<6000008)
insert into S select x as id, iif((x % 2) == 0, 0.0000000123, 12345.0000000001) as value
from cnt;

--select * from S limit 5;

--select printf("count %d, sum: %35.15f", count(*), decimal_sum(value)) from S;
select printf("count %d, sum: %35.15f", count(*), sum(value)) from S;

** An instance of the following structure holds the context of a
** sum() or avg() aggregate computation.
typedef struct SumCtx SumCtx;
struct SumCtx {
  LONGDOUBLE_TYPE rSum;      /* Floating point sum */
  i64 iSum;         /* Integer sum */
  i64 cnt;          /* Number of elements summed */
  u8 overflow;      /* True if integer overflow seen */
  u8 approx;        /* True if non-integer value was input to the sum */

/usr/bin/time sqlite3 < sum-long-long-test.sql 
count 6000008, sum:         37035049380.037510000000000
3.27user 0.01system 0:03.29elapsed 100%CPU (0avgtext+0avgdata 115292maxresident)k
0inputs+0outputs (0major+28059minor)pagefaults 0swaps
/usr/bin/time sqlite3 < sum-decimal-test.sql 
count 6000008, sum:         37035049380.037200000000000
5.91user 0.02system 0:05.94elapsed 99%CPU (0avgtext+0avgdata 115428maxresident)k
0inputs+0outputs (0major+28063minor)pagefaults 0swaps
/usr/bin/time sqlite3 < sum-default-test.sql 
count 6000008, sum:         37035049380.000160000000000
3.22user 0.02system 0:03.25elapsed 100%CPU (0avgtext+0avgdata 115432maxresident)k
0inputs+0outputs (0major+28065minor)pagefaults 0swaps