aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authordrh <>2023-06-26 19:35:20 +0000
committerdrh <>2023-06-26 19:35:20 +0000
commit12b198f1a22796ef224de71e84867d18a1f2bf2a (patch)
treeaaa73a6e0942ffa2e0dd816fc043175fd9ceb6be /src/util.c
parentd2b9cdd592e407fd3b086285cc76d6011ed7d7fa (diff)
downloadsqlite-12b198f1a22796ef224de71e84867d18a1f2bf2a.tar.gz
sqlite-12b198f1a22796ef224de71e84867d18a1f2bf2a.zip
Use ideas from T. J. Dekker in "A Floating-Point Technique for Extending the
Available Precision" (1971-07-26) to enhance the accuracy of the SUM() aggregate function in cases where input magnitudes vary wildly. FossilOrigin-Name: 439fc00fee62b4db3751860485e21a99cae4fd1f5d911b2c08651a1466245ecc
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 bed86a569..23fbad997 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;
+}