-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.KahanFloatingSum.pas
More file actions
58 lines (44 loc) · 1.3 KB
/
Copy pathMaxLogic.KahanFloatingSum.pas
File metadata and controls
58 lines (44 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
unit MaxLogic.KahanFloatingSum;
{ Inspired by:
http://stackoverflow.com/questions/6699066/in-which-order-should-floats-be-added-to-get-the-most-precise-result#comment7935766_6699451 }
interface
uses
System.SysUtils, System.Classes;
type
TKahan = record
private
fCompensation: Double; // Tracks the small compensation value
fSum: Double; // Accumulated sum
public
// Initializes fields when the record is created
class operator Initialize(out ADest: TKahan);
// Add a value to the sum with Kahan compensation
procedure Add(const AValue: Double);
// Initialize the sum with a first value
procedure AddFirst(const AValue: Double);
// Public property to expose the accumulated sum
property Sum: Double read fSum;
end;
implementation
{ TKahan }
class operator TKahan.Initialize(out ADest: TKahan);
begin
// Ensure fields are initialized to zero
ADest.fCompensation := 0.0;
ADest.fSum := 0.0;
end;
procedure TKahan.Add(const AValue: Double);
var
lTempSum, lCompensatedValue: Double;
begin
lCompensatedValue := AValue - fCompensation;
lTempSum := fSum + lCompensatedValue;
fCompensation := (lTempSum - fSum) - lCompensatedValue;
fSum := lTempSum;
end;
procedure TKahan.AddFirst(const AValue: Double);
begin
fSum := AValue;
fCompensation := 0.0;
end;
end.