SQLite Forum

sumStep and sumInverse, integer
Login

sumStep and sumInverse, integer

(1) By anonymous on 2023-07-18 21:27:28 [source]

In the sumStep function https://www.sqlite.org/src/file?ci=4dcad2db743fdb9ef72871ca5a4d1384f76cb697161b0f5110e2670a83a18e8a&name=src/func.c&ln=1767

1755:      if( type!=SQLITE_INTEGER ){
1756:        kahanBabuskaNeumaierInit(p, p->iSum);
1757:        p->approx = 1;
1758:        kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
1759:      }else{
1760:        i64 x = p->iSum;
1761:        if( sqlite3AddInt64(&x, sqlite3_value_int64(argv[0]))==0 ){
1762:          p->iSum = x;
1763:        }else{
1764:          p->ovrfl = 1;
1765:          kahanBabuskaNeumaierInit(p, p->iSum);
1766:          p->approx = 1;
1767:          kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
1768:        }
1769:      }
The lines 1760-1768 are for a case when the argument is an integer, on line 1767 the integer argument is converted to double, the kahanBabuskaNeumaierStepInt64 and sqlite3_value_int64 functions should be used here ``` --- src/func.c +++ src/func.c @@ -1755,18 +1755,19 @@ if( type!=SQLITE_INTEGER ){ kahanBabuskaNeumaierInit(p, p->iSum); p->approx = 1; kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0])); }else{

  • i64 iArg = sqlite3_value_int64(argv[0]); i64 x = p->iSum;
  • if( sqlite3AddInt64(&x, sqlite3_value_int64(argv[0]))==0 ){
  • if( sqlite3AddInt64(&x, iArg)==0 ){ p->iSum = x; }else{ p->ovrfl = 1; kahanBabuskaNeumaierInit(p, p->iSum); p->approx = 1;
  • kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
  • kahanBabuskaNeumaierStepInt64(p, iArg); } } }else{ p->approx = 1; if( type==SQLITE_INTEGER ){ ```

In the sumInverse function https://www.sqlite.org/src/file?ci=4dcad2db743fdb9ef72871ca5a4d1384f76cb697161b0f5110e2670a83a18e8a&name=src/func.c&ln=1801

1797:      i64 iVal = sqlite3_value_int64(argv[0]);
1798:      if( iVal!=SMALLEST_INT64 ){
1799:        kahanBabuskaNeumaierStepInt64(p, -iVal);
1800:      }else{
1801:        kahanBabuskaNeumaierStepInt64(p, LARGEST_INT64);
1802:        kahanBabuskaNeumaierStepInt64(p, 1);
1803:      }
double can represent the SMALLEST_INT64 value exactly, so the lines 1801-1802 can be replaced with the following single line:
        kahanBabuskaNeumaierStep(p, -(double)SMALLEST_INT64);

(2) By Nuno Cruces (ncruces) on 2023-07-23 00:05:18 in reply to 1 [link] [source]

These seem reasonable.