Skip to content

Commit f19a00b

Browse files
committed
fix: Rounding to positive infinity by sbalazs
1 parent cc736a3 commit f19a00b

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

baseline/Common/baseline.common.pas

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ TBaseline = class(TObject)
3333
FInputFile: String;
3434
FStationNames: TStringList;
3535
FHashStationList: TFPHashList;
36-
3736
procedure AddToHashList(AStation: String; ATemp: Int64);
3837
procedure BuildHashList;
38+
function RoundEx(x: Double): Double;
39+
function PascalRound(x: Double): Double;
3940
protected
4041
public
4142
constructor Create(AInputFile: String);
@@ -182,6 +183,32 @@ procedure TBaseline.BuildHashList;
182183
end;
183184
end;
184185

186+
function TBaseline.RoundEx(x: Double): Double;
187+
begin
188+
Result := PascalRound(x*10.0)/10.0;
189+
end;
190+
191+
function TBaseline.PascalRound(x: Double): Double;
192+
var
193+
t: Double;
194+
begin
195+
//round towards positive infinity
196+
t := Trunc(x);
197+
if (x < 0.0) and (t - x = 0.5) then
198+
begin
199+
// Do nothing
200+
end
201+
else if Abs(x - t) >= 0.5 then
202+
begin
203+
t := t + Math.Sign(x);
204+
end;
205+
206+
if t = 0.0 then
207+
Result := 0.0
208+
else
209+
Result := t;
210+
end;
211+
185212
procedure TBaseline.Generate;
186213
var
187214
index: Integer;
@@ -199,9 +226,9 @@ procedure TBaseline.Generate;
199226
for index := 0 to FHashStationList.Count - 1 do
200227
begin
201228
weatherStation := FHashStationList.Items[index];
202-
Min := weatherStation^.FMin/10;
203-
Max := weatherStation^.FMax/10;
204-
Mean := weatherStation^.FTot/weatherStation^.FCnt/10;
229+
Min := RoundEx(weatherStation^.FMin/10);
230+
Max := RoundEx(weatherStation^.FMax/10);
231+
Mean := RoundEx(weatherStation^.FTot/weatherStation^.FCnt/10);
205232
strTemp := weatherStation^.FStation + '=' + FormatFloat('0.0', Min) + '/' + FormatFloat('0.0', Mean) + '/' + FormatFloat('0.0', Max) + ',';
206233
FStationNames.Add(strTemp);
207234
end;

entries/sbalazs/src/uweatherstations.pas

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ TWSThread = class(TWSThreadBase)
8484

8585
TWSThreadsWatcher = class(TWSThreadBase)
8686
private
87+
function RoundEx(x: Double): Double;
88+
function PascalRound(x: Double): Double;
8789
procedure CreateFinalList;
8890
protected
8991
procedure Execute; override;
@@ -493,6 +495,32 @@ function Compare(List: TStringList; Index1, Index2: Integer): Integer;
493495
end;
494496
end;
495497

498+
function TWSThreadsWatcher.RoundEx(x: Double): Double;
499+
begin
500+
Result := PascalRound(x*10.0)/10.0;
501+
end;
502+
503+
function TWSThreadsWatcher.PascalRound(x: Double): Double;
504+
var
505+
t: Double;
506+
begin
507+
//round towards positive infinity
508+
t := Trunc(x);
509+
if (x < 0.0) and (t - x = 0.5) then
510+
begin
511+
// Do nothing
512+
end
513+
else if Abs(x - t) >= 0.5 then
514+
begin
515+
t := t + Math.Sign(x);
516+
end;
517+
518+
if t = 0.0 then
519+
Result := 0.0
520+
else
521+
Result := t;
522+
end;
523+
496524
procedure TWSThreadsWatcher.CreateFinalList;
497525
var
498526
I: Integer;
@@ -516,9 +544,9 @@ procedure TWSThreadsWatcher.CreateFinalList;
516544
Continue;
517545
SetString(Name, Pointer(@WS.FStation[0]), Length(WS.FStation));
518546
SetCodePage(Name, CP_UTF8, True);
519-
Min := WS.FData.FMin/10;
520-
Max := WS.FData.FMax/10;
521-
Mean := WS.FData.FTot/WS.FData.FCnt/10;
547+
Min := RoundEx(WS.FData.FMin/10);
548+
Max := RoundEx(WS.FData.FMax/10);
549+
Mean := RoundEx(WS.FData.FTot/WS.FData.FCnt/10);
522550
Str := Name + '=' + FormatFloat('0.0', Min) + '/' + FormatFloat('0.0', Mean) + '/' + FormatFloat('0.0', Max) + ',';
523551
SL.Add(Str);
524552
end;

0 commit comments

Comments
 (0)