Skip to content

Commit 250a60f

Browse files
committed
refactor: Simple rounding new baseline.output.gz
1 parent 9de95d5 commit 250a60f

3 files changed

Lines changed: 40 additions & 44 deletions

File tree

README.md

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,51 @@ Submit your implementation and become part of the leader board!
6868

6969
## Rounding
7070

71-
Székely Balázs has provided code for rounding towards positive infinity per the original challenge.
72-
This will be the official way to round the output values:
71+
While I recognize that Székely's rounding code was a good effort, it was not simple and made a lot of people doubt it was even correct for negative temperatures.\
72+
In a discussion with [Mr. Packman](https://pack.ac/) themselves, we came up with a simpler solution. They even added some _Unit Testing_ :D.
73+
74+
This will be the official way to round the output values, so pick your poison:
7375
```pas
74-
function TBaseline.RoundEx(x: Double): Double;
76+
function RoundEx(x: Double): Double; inline;
7577
begin
76-
Result := PascalRound(x*10.0)/10.0;
78+
Result := Ceil(x * 10) / 10;
7779
end;
7880
79-
function TBaseline.PascalRound(x: Double): Double;
81+
function RoundExInteger(x: Double): Integer; inline;
82+
begin
83+
Result := Ceil(x * 10);
84+
end;
85+
86+
function RoundExString(x: Double): String; inline;
8087
var
81-
t: Double;
88+
V, Q, R: Integer;
8289
begin
83-
//round towards positive infinity
84-
t := Trunc(x);
85-
if (x < 0.0) and (t - x = 0.5) then
90+
V := RoundExInteger(x);
91+
if V < 0 then
8692
begin
87-
// Do nothing
93+
Result := '-';
94+
V := -V;
8895
end
89-
else if Abs(x - t) >= 0.5 then
90-
begin
91-
t := t + Math.Sign(x);
92-
end;
93-
94-
if t = 0.0 then
95-
Result := 0.0
9696
else
97-
Result := t;
97+
Result := '';
98+
Q := V div 10;
99+
R := V - (Q * 10);
100+
Result := IntToStr(Q) + '.' + IntToStr(R);
101+
end;
102+
103+
procedure Test;
104+
var
105+
F: Double;
106+
begin
107+
for F in [10.01, 10.04, -10.01, -10.0, 0, -0, -0.01] do
108+
WriteLn(RoundExInteger(F), ' ', RoundExString(F), ' ', RoundEx(F));
109+
//101 10.1 1.0100000000000000E+001
110+
//101 10.1 1.0100000000000000E+001
111+
//-100 -10.0 -1.0000000000000000E+001
112+
//-100 -10.0 -1.0000000000000000E+001
113+
//0 0.0 0.0000000000000000E+000
114+
//0 0.0 0.0000000000000000E+000
115+
//0 0.0 0.0000000000000000E+000
98116
end;
99117
```
100118

@@ -148,7 +166,7 @@ Expected `SHA256` hash:
148166
>
149167
> We are still waiting for the Delphi version to be completed in order for us to have an official `SHA256` hash for the output.
150168
>
151-
> Until then, this is the current one: `db3d79d31b50daa8c03a1e4f2025029cb137f9971aa04129d8bca004795ae524`
169+
> Until then, this is the current one: `4256d19d3e134d79cc6f160d428a1d859ce961167bd01ca528daca8705163910`
152170
> There's also an archived version of the [baseline output](./data/baseline.output.gz)
153171
154172
## Differences From Original
@@ -211,7 +229,8 @@ I'd like to thank [@paweld](https://github.com/paweld) for taking us from my mis
211229
I'd like to thank [@mobius](https://github.com/mobius1qwe) for taking the time to provide the Delphi version of the generator.\
212230
I'd like to thank [@dtpfl](https://github.com/dtpfl) for his invaluable work on maintaining the `README.md` file up to date with everything.\
213231
I'd like to thank Székely Balázs for providing many patches to make everything compliant with the original challenge.\
214-
I'd like to thank [@corneliusdavid](https://github.com/corneliusdavid) for giving some of the information files a once over and making things more legible and clear.
232+
I'd like to thank [@corneliusdavid](https://github.com/corneliusdavid) for giving some of the information files a once over and making things more legible and clear.\
233+
I'd like to thank Mr. **Pack**man, aka O, for clearing the fog around the rounding issues.
215234

216235
## Links
217236
The original repository: https://github.com/gunnarmorling/1brc \

baseline/Common/baseline.common.pas

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ TBaseline = class(TObject)
3636
procedure AddToHashList(AStation: String; ATemp: Int64);
3737
procedure BuildHashList;
3838
function RoundEx(x: Double): Double;
39-
function PascalRound(x: Double): Double;
4039
protected
4140
public
4241
constructor Create(AInputFile: String);
@@ -185,28 +184,7 @@ procedure TBaseline.BuildHashList;
185184

186185
function TBaseline.RoundEx(x: Double): Double;
187186
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;
187+
Result := Ceil(x * 10) / 10;
210188
end;
211189

212190
procedure TBaseline.Generate;
@@ -221,7 +199,6 @@ procedure TBaseline.Generate;
221199

222200
BuildHashList;
223201

224-
//FStationNames.DefaultEncoding := TEncoding.UTF8;
225202
FStationNames.BeginUpdate;
226203
for index := 0 to FHashStationList.Count - 1 do
227204
begin

data/baseline.output.gz

-68 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)