summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Guschin <guschin.drew@gmail.com>2023-04-19 08:46:27 +0400
committerAndrew Guschin <guschin.drew@gmail.com>2023-04-19 08:46:27 +0400
commite71c022ca01d05d843d9bc163ccf3599427362f6 (patch)
tree3d5d8db99350d3e9df99d23128526a37f126cbaf
parentd3360392cb566ae0bc03bb813e475a4ccf3163dc (diff)
Добавлена реализация double через два float
-rw-r--r--double_3232.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/double_3232.cpp b/double_3232.cpp
new file mode 100644
index 0000000..1e7af4b
--- /dev/null
+++ b/double_3232.cpp
@@ -0,0 +1,116 @@
+#include <iostream>
+#include <iomanip>
+
+struct dsdbl
+{
+ float x, y;
+};
+
+dsdbl ds_set(float a)
+{
+ dsdbl z;
+ z.x = a;
+ z.y = 0.0;
+ return z;
+}
+
+dsdbl ds_mul(dsdbl dsa, dsdbl dsb)
+{
+ dsdbl dsc;
+ float c11, c21, c2, e, t1, t2;
+ float a1, a2, b1, b2, cona, conb, split = 8193.;
+
+ cona = dsa.x * split;
+ conb = dsb.x * split;
+ a1 = cona - (cona - dsa.x);
+ b1 = conb - (conb - dsb.x);
+ a2 = dsa.x - a1;
+ b2 = dsb.x - b1;
+
+ c11 = dsa.x * dsb.x;
+ c21 = a2 * b2 + (a2 * b1 + (a1 * b2 + (a1 * b1 - c11)));
+
+ c2 = dsa.x * dsb.y + dsa.y * dsb.x;
+
+ t1 = c11 + c2;
+ e = t1 - c11;
+ t2 = dsa.y * dsb.y + ((c2 - e) + (c11 - (t1 - e))) + c21;
+
+ dsc.x = t1 + t2;
+ dsc.y = t2 - (dsc.x - t1);
+
+ return dsc;
+}
+
+dsdbl ds_add (dsdbl dsa, dsdbl dsb)
+{
+ dsdbl dsc;
+ float t1, t2, e;
+
+ t1 = dsa.x + dsb.x;
+ e = t1 - dsa.x;
+ t2 = ((dsb.x - e) + (dsa.x - (t1 - e))) + dsa.y + dsb.y;
+
+ dsc.x = t1 + t2;
+ dsc.y = t2 - (dsc.x - t1);
+ return dsc;
+}
+
+dsdbl ds_sub(dsdbl dsa, dsdbl dsb)
+{
+ dsdbl dsc;
+ float e, t1, t2;
+
+ t1 = dsa.x - dsb.x;
+ e = t1 - dsa.x;
+ t2 = ((-dsb.x - e) + (dsa.x - (t1 - e))) + dsa.y - dsb.y;
+
+ dsc.x = t1 + t2;
+ dsc.y = t2 - (dsc.x - t1);
+ return dsc;
+}
+
+float ds_compare(dsdbl dsa, dsdbl dsb)
+{
+ if (dsa.x < dsb.x) return -1.;
+ else if (dsa.x == dsb.x)
+ {
+ if (dsa.y < dsb.y) return -1.;
+ else if (dsa.y == dsb.y) return 0.;
+ else return 1.;
+ }
+ else return 1.;
+}
+
+dsdbl double_to_ds(double d)
+{
+ float x = (float) d;
+ float y = d - (double) x;
+ return dsdbl { x, y };
+}
+
+int main()
+{
+ double x = 0.0194453596504303195813267990388339967467;
+ double y = -1.7868687567166214869018858735216781497002;
+ dsdbl a = double_to_ds(x);
+ dsdbl b = double_to_ds(y);
+
+ dsdbl ab = ds_mul(a, b);
+ double xy = x * y;
+ std::cout << std::fixed << std::setprecision(40) << x << std::endl;
+ std::cout << std::fixed << std::setprecision(40) << y << std::endl;
+ std::cout << std::fixed << std::setprecision(40) << xy << std::endl;
+ std::cout << std::fixed << std::endl;
+ std::cout << std::fixed << std::setprecision(40) << a.x << ' ' << a.y << std::endl;
+ std::cout << std::fixed << std::setprecision(40) << b.x << ' ' << b.y << std::endl;
+ std::cout << std::fixed << std::setprecision(40) << ab.x << ' ' << ab.y << std::endl;
+
+ double abdbl = double(ab.x) + double(ab.y);
+ std::cout << std::endl;
+ std::cout << std::fixed << std::setprecision(40) << xy << std::endl;
+ std::cout << std::fixed << std::setprecision(40) << abdbl << std::endl;
+
+ dsdbl xyds = double_to_ds(xy);
+ std::cout << ds_compare(xyds, ab) << std::endl;
+}