This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "c4ce4ea3415a128e81393b13176e9cbf9f1266c14e2fe3d7588dacdc38cf259a",
|
||||
"verdict": "verified",
|
||||
"summary": "2 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"tool": "godbolt",
|
||||
"compiler": "mojo_1_2_0",
|
||||
"mojoVersion": "1.2.0",
|
||||
"source_sha256": "cbbcc76b6b043a613dc6983512073400b8b622127210bae94c5ea7011b5ddce3",
|
||||
"stdin": "4 3 1 0 1 1 3 0 11 1 0 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 10 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 0 end\n",
|
||||
"args": "--target-cpu x86-64-v3 --fp-mode contract=off",
|
||||
"asked": "2026-11-12",
|
||||
"result": {
|
||||
"compiled": true,
|
||||
"buildStderr": "",
|
||||
"executed": true,
|
||||
"exitCode": 0,
|
||||
"stdout": "6.0\n1.000000000000001\n1.0\n0.0",
|
||||
"stderr": "",
|
||||
"timedOut": false,
|
||||
"asm": null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
function Sum(x: seq<real>): real
|
||||
{
|
||||
if |x| == 0 then 0.0 else Sum(x[..|x| - 1]) + x[|x| - 1]
|
||||
}
|
||||
|
||||
// In exact arithmetic Kahan summation is the plain sum, and its compensation is always 0.
|
||||
method KahanSum(x: seq<real>) returns (total: real)
|
||||
ensures total == Sum(x)
|
||||
{
|
||||
total := 0.0;
|
||||
var c := 0.0;
|
||||
var i := 0;
|
||||
while i < |x|
|
||||
invariant 0 <= i <= |x|
|
||||
invariant total == Sum(x[..i])
|
||||
invariant c == 0.0
|
||||
{
|
||||
assert x[..i + 1][..i] == x[..i];
|
||||
var y := x[i] - c;
|
||||
var t := total + y;
|
||||
c := (t - total) - y;
|
||||
total := t;
|
||||
i := i + 1;
|
||||
}
|
||||
assert x[..|x|] == x;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
double kahan_sum(const double *x, long n) {
|
||||
double total = 0.0, c = 0.0;
|
||||
for (long i = 0; i < n; i++) {
|
||||
double y = x[i] - c;
|
||||
double t = total + y;
|
||||
c = (t - total) - y;
|
||||
total = t;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
def kahan_sum(x: List[Float64]) -> Float64:
|
||||
var total: Float64 = 0.0
|
||||
var c: Float64 = 0.0
|
||||
for i in range(len(x)):
|
||||
var y = x[i] - c
|
||||
var t = total + y
|
||||
c = (t - total) - y
|
||||
total = t
|
||||
return total
|
||||
@@ -0,0 +1,9 @@
|
||||
def kahan_sum(x):
|
||||
total = 0.0
|
||||
c = 0.0
|
||||
for v in x:
|
||||
y = v - c
|
||||
t = total + y
|
||||
c = (t - total) - y
|
||||
total = t
|
||||
return total
|
||||
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"name": "KahanSum",
|
||||
"summary": "The sum of a list of FLOAT values by compensated summation, whose error bound, to first order, does not grow with the length of the list.",
|
||||
"tags": [
|
||||
"summation",
|
||||
"rounding",
|
||||
"precision"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Kahan, Pracniques: further remarks on reducing truncation errors, CACM 8(1), 1965",
|
||||
"url": "https://doi.org/10.1145/363707.363723"
|
||||
},
|
||||
{
|
||||
"title": "Higham, Accuracy and Stability of Numerical Algorithms, 2nd edition (SIAM, 2002), section 4.3, Compensated summation"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "KahanSum([1.0, 2.0, 3.0])"
|
||||
},
|
||||
{
|
||||
"call": "KahanSum([1.0, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16])"
|
||||
},
|
||||
{
|
||||
"call": "KahanSum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])"
|
||||
},
|
||||
{
|
||||
"call": "KahanSum([])"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same compensated loop."
|
||||
},
|
||||
{
|
||||
"lang": "c",
|
||||
"level": "plain",
|
||||
"file": "ref/c/plain.c",
|
||||
"note": "The same loop in C. It survives -O2; it would not survive -ffast-math, which lets the compiler treat (t - total) - y as 0."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same loop in Mojo."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/kahan_sum.dfy",
|
||||
"note": "Over real numbers the compensation C stays 0 and the result is exactly the sum: what Kahan summation corrects is rounding, and nothing else."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// KahanSum: the sum of a list of FLOAT values by compensated summation
|
||||
// (Kahan, 1965). C holds the low-order part the last addition lost, and the
|
||||
// next addition puts it back, so the error bound, about twice the unit
|
||||
// roundoff times the sum of the absolute values (Higham, section 4.3), does
|
||||
// not grow with the length of the list, to first order. It is relative to
|
||||
// that sum of absolute values, not to the result: KahanSum([1.0e100, 1.0,
|
||||
// -1.0e100]) is 0.0. In exact arithmetic C is always 0 and this is the
|
||||
// plain sum: the compensation exists only because of rounding. A compiler
|
||||
// allowed to reorder FLOAT arithmetic (-ffast-math) can simplify C away.
|
||||
FUNCTION KahanSum(X : LIST OF FLOAT) RETURNS FLOAT
|
||||
DECLARE Total : FLOAT <- 0.0
|
||||
DECLARE C : FLOAT <- 0.0
|
||||
FOR I <- 0 TO LENGTH(X)
|
||||
DECLARE Y : FLOAT <- X[I] - C
|
||||
DECLARE T : FLOAT <- Total + Y
|
||||
C <- (T - Total) - Y
|
||||
Total <- T
|
||||
NEXT I
|
||||
RETURN Total
|
||||
ENDFUNCTION
|
||||
Reference in new issue
Block a user