Skip to content

Commit 8115426

Browse files
author
Tommy TIAN
authored
Merge pull request #1 from txaty/bugfix/float_rounding
Fix float number rounding bug and update license
2 parents 3235bb7 + 883d6e3 commit 8115426

8 files changed

+200
-16
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Tommy Xiangan TIAN
3+
Copyright (c) 2022 Tommy TIAN
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

constant.go

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Tommy TIAN
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
123
package complex
224

325
import "math/big"

gaussian_integer.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Tommy TIAN
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
123
package complex
224

325
import (
@@ -155,10 +177,10 @@ func (g *GaussianInt) Div(a, b *GaussianInt) *GaussianInt {
155177

156178
rsInt := iPool.Get().(*big.Int)
157179
defer iPool.Put(rsInt)
158-
roundFloat(realScalar, rsInt)
180+
rsInt = roundFloat(realScalar)
159181
isInt := iPool.Get().(*big.Int)
160182
defer iPool.Put(isInt)
161-
roundFloat(imagScalar, isInt)
183+
isInt = roundFloat(imagScalar)
162184
quotient := NewGaussianInt(rsInt, isInt)
163185
opt := giPool.Get().(*GaussianInt)
164186
defer giPool.Put(opt)

gaussian_integer_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Tommy TIAN
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
123
package complex
224

325
import (

hurwitz_integer.go

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Tommy TIAN
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
123
package complex
224

325
import (
@@ -146,10 +168,10 @@ func (h *HurwitzInt) Val() (r, i, j, k *big.Float) {
146168
// ValInt reveals value of a Hurwitz integer in integer
147169
func (h *HurwitzInt) ValInt() (r, i, j, k *big.Int) {
148170
rF, iF, jF, kF := h.Val()
149-
roundFloat(rF, r)
150-
roundFloat(iF, i)
151-
roundFloat(jF, j)
152-
roundFloat(kF, k)
171+
r = roundFloat(rF)
172+
i = roundFloat(iF)
173+
j = roundFloat(jF)
174+
k = roundFloat(kF)
153175
return
154176
}
155177

@@ -360,16 +382,16 @@ func (h *HurwitzInt) Div(a, b *HurwitzInt) *HurwitzInt {
360382

361383
rsInt := iPool.Get().(*big.Int)
362384
defer iPool.Put(rsInt)
363-
roundFloat(rScalar, rsInt)
385+
rsInt = roundFloat(rScalar)
364386
isInt := iPool.Get().(*big.Int)
365387
defer iPool.Put(isInt)
366-
roundFloat(iScalar, isInt)
388+
isInt = roundFloat(iScalar)
367389
jsInt := iPool.Get().(*big.Int)
368390
defer iPool.Put(jsInt)
369-
roundFloat(jScalar, jsInt)
391+
jsInt = roundFloat(jScalar)
370392
ksInt := iPool.Get().(*big.Int)
371393
defer iPool.Put(ksInt)
372-
roundFloat(kScalar, ksInt)
394+
ksInt = roundFloat(kScalar)
373395

374396
quotient := NewHurwitzInt(rsInt, isInt, jsInt, ksInt, false)
375397
opt := hiPool.Get().(*HurwitzInt)

hurwitz_integer_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,57 @@ func TestHurwitzInt_String(t *testing.T) {
130130
})
131131
}
132132
}
133+
134+
func TestHurwitzInt_ValInt(t *testing.T) {
135+
type fields struct {
136+
dblR *big.Int
137+
dblI *big.Int
138+
dblJ *big.Int
139+
dblK *big.Int
140+
}
141+
tests := []struct {
142+
name string
143+
fields fields
144+
wantR *big.Int
145+
wantI *big.Int
146+
wantJ *big.Int
147+
wantK *big.Int
148+
}{
149+
{
150+
name: "test_1+i+j+k",
151+
fields: fields{
152+
dblR: big.NewInt(2),
153+
dblI: big.NewInt(2),
154+
dblJ: big.NewInt(2),
155+
dblK: big.NewInt(2),
156+
},
157+
wantR: big.NewInt(1),
158+
wantI: big.NewInt(1),
159+
wantJ: big.NewInt(1),
160+
wantK: big.NewInt(1),
161+
},
162+
}
163+
for _, tt := range tests {
164+
t.Run(tt.name, func(t *testing.T) {
165+
h := &HurwitzInt{
166+
dblR: tt.fields.dblR,
167+
dblI: tt.fields.dblI,
168+
dblJ: tt.fields.dblJ,
169+
dblK: tt.fields.dblK,
170+
}
171+
gotR, gotI, gotJ, gotK := h.ValInt()
172+
if !reflect.DeepEqual(gotR, tt.wantR) {
173+
t.Errorf("ValInt() gotR = %v, want %v", gotR, tt.wantR)
174+
}
175+
if !reflect.DeepEqual(gotI, tt.wantI) {
176+
t.Errorf("ValInt() gotI = %v, want %v", gotI, tt.wantI)
177+
}
178+
if !reflect.DeepEqual(gotJ, tt.wantJ) {
179+
t.Errorf("ValInt() gotJ = %v, want %v", gotJ, tt.wantJ)
180+
}
181+
if !reflect.DeepEqual(gotK, tt.wantK) {
182+
t.Errorf("ValInt() gotK = %v, want %v", gotK, tt.wantK)
183+
}
184+
})
185+
}
186+
}

pool.go

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Tommy TIAN
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
123
package complex
224

325
import (

rounding.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Tommy TIAN
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
123
package complex
224

325
import "math/big"
426

527
// roundFloat rounds the given big float to the nearest big integer
6-
func roundFloat(f *big.Float, res *big.Int) {
28+
func roundFloat(f *big.Float) *big.Int {
729
if f.Sign() < 0 {
830
f.Sub(f, rDelta)
931
} else {
1032
f.Add(f, rDelta)
1133
}
12-
if res == nil {
13-
res = new(big.Int)
14-
}
15-
f.Int(res)
34+
res, _ := f.Int(nil)
35+
return res
1636
}

0 commit comments

Comments
 (0)