aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authordrh <>2023-06-28 12:02:48 +0000
committerdrh <>2023-06-28 12:02:48 +0000
commit8fbb335d9f73dfa61b6da3f99e3408435297b850 (patch)
tree611dcab4bc84f1fdce9bb294985dac9d23106c19 /src/util.c
parent60783f47b26fd584f8a7879697092557ecd0c613 (diff)
parentd847c73153ba7eb77a5b773a691ad0ca6b879157 (diff)
downloadsqlite-8fbb335d9f73dfa61b6da3f99e3408435297b850.tar.gz
sqlite-8fbb335d9f73dfa61b6da3f99e3408435297b850.zip
Enhance the SUM() aggregate (and related AVG() and TOTAL()) so that the running
sum is accurate to about 100 bits. FossilOrigin-Name: a915f15a916af698e0cef46c8b3e7ed11bda19349179d2d414073cd39c4cce24
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index abd36eda8..256ec7c5c 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1750,3 +1750,43 @@ int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
|| defined(SQLITE_ENABLE_STMT_SCANSTATUS)
# include "hwtime.h"
#endif
+
+/***************************************************************************
+** Double-Double arithmetic.
+**
+** Reference:
+** T. J. Dekker, "A Floating-Point Technique for Extending the
+** Available Precision". 1971-07-26.
+*/
+
+/* Compute z = (i64)x */
+void sqlite3DDFromInt(i64 x, double *z){
+ z[0] = (double)x;
+ z[1] = (double)(x - (i64)z[0]);
+}
+
+/* Compute z = x + y */
+void sqlite3DDAdd(double x, double xx, double y, double yy, double *z){
+ double r, s;
+ r = x + y;
+ if( fabs(x)>fabs(y) ){
+ s = x - r + y + yy + xx;
+ }else{
+ s = y - r + x + xx + yy;
+ }
+ z[0] = r+s;
+ z[1] = r - z[0] + s;
+}
+
+/* Compute z = x - y */
+void sqlite3DDSub(double x, double xx, double y, double yy, double *z){
+ double r, s;
+ r = x - y;
+ if( fabs(x)>fabs(y) ){
+ s = x - r - y - yy + xx;
+ }else{
+ s = -y - r + x + xx - yy;
+ }
+ z[0] = r+s;
+ z[1] = r - z[0] + s;
+}