Skip to content

Commit 0fccc71

Browse files
committed
UPDATE fix64 DivPrecise
1 parent 5a9b4d3 commit 0fccc71

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

fix64/fix64.go

+4
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ func nlz(v uint64) int32 {
228228

229229
// DivPrecise Divides two FP values.
230230
func DivPrecise(argA, argB int64) int64 {
231+
if argB == MinValue || argB == 0 {
232+
return 0
233+
}
234+
231235
signDif := argA ^ argB
232236

233237
const b uint64 = 0x100000000 // Number base (32 bits)

fix64/fix64_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package fix64_test
33
import (
44
"testing"
55

6-
"github.com/camry/fp/fix64"
76
"github.com/stretchr/testify/assert"
7+
8+
"github.com/camry/fp/fix64"
89
)
910

1011
func TestAdd(t *testing.T) {
@@ -40,6 +41,12 @@ func TestDiv(t *testing.T) {
4041
assert.Equal(t, fix64.ToFloat32(fix64.Div(f1, f2)), float32(470.13297))
4142
}
4243

44+
func TestDivPrecise(t *testing.T) {
45+
f1 := fix64.FromInt32(2147483647)
46+
f2 := fix64.FromFloat32(4567822)
47+
assert.Equal(t, fix64.ToFloat32(fix64.DivPrecise(f1, f2)), float32(470.13297))
48+
}
49+
4350
func TestFix64(t *testing.T) {
4451
f1 := fix64.FromInt32(300)
4552
f2 := fix64.FromInt32(10)

0 commit comments

Comments
 (0)