Skip to content

Commit

Permalink
Merge pull request c9s#1125 from c9s/bhwu/grid2/using-dnum
Browse files Browse the repository at this point in the history
FEATURE: [grid2] using dnum
  • Loading branch information
gx578007 authored Mar 24, 2023
2 parents bf9cd78 + aa419e8 commit cd1314e
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 146 deletions.
160 changes: 108 additions & 52 deletions pkg/fixedpoint/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,124 @@
package fixedpoint

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_FormatString(t *testing.T) {
assert := assert.New(t)
cases := []struct {
input string
prec int
expected string
}{
{input: "0.57", prec: 5, expected: "0.57000"},
{input: "-0.57", prec: 5, expected: "-0.57000"},
{input: "0.57123456", prec: 8, expected: "0.57123456"},
{input: "-0.57123456", prec: 8, expected: "-0.57123456"},
{input: "0.57123456", prec: 5, expected: "0.57123"},
{input: "-0.57123456", prec: 5, expected: "-0.57123"},
{input: "0.57123456", prec: 0, expected: "0"},
{input: "-0.57123456", prec: 0, expected: "0"},
{input: "0.57123456", prec: -1, expected: "0"},
{input: "-0.57123456", prec: -1, expected: "0"},
{input: "0.57123456", prec: -5, expected: "0"},
{input: "-0.57123456", prec: -5, expected: "0"},
{input: "0.57123456", prec: -9, expected: "0"},
{input: "-0.57123456", prec: -9, expected: "0"},

t.Run("0.57 with prec = 5, expected 0.57", func(t *testing.T) {
v := MustNewFromString("0.57")
s := v.FormatString(5)
assert.Equal("0.57000", s)
})
{input: "1.23456789", prec: 9, expected: "1.234567890"},
{input: "-1.23456789", prec: 9, expected: "-1.234567890"},
{input: "1.02345678", prec: 9, expected: "1.023456780"},
{input: "-1.02345678", prec: 9, expected: "-1.023456780"},
{input: "1.02345678", prec: 2, expected: "1.02"},
{input: "-1.02345678", prec: 2, expected: "-1.02"},
{input: "1.02345678", prec: 0, expected: "1"},
{input: "-1.02345678", prec: 0, expected: "-1"},
{input: "1.02345678", prec: -1, expected: "0"},
{input: "-1.02345678", prec: -1, expected: "0"},
{input: "1.02345678", prec: -10, expected: "0"},
{input: "-1.02345678", prec: -10, expected: "0"},

t.Run("0.57123456 with prec = 5, expected 0.57123", func(t *testing.T) {
v := MustNewFromString("0.57123456")
s := v.FormatString(5)
assert.Equal("0.57123", s)
})
{input: "0.0001234", prec: 9, expected: "0.000123400"},
{input: "-0.0001234", prec: 9, expected: "-0.000123400"},
{input: "0.0001234", prec: 7, expected: "0.0001234"},
{input: "-0.0001234", prec: 7, expected: "-0.0001234"},
{input: "0.0001234", prec: 5, expected: "0.00012"},
{input: "-0.0001234", prec: 5, expected: "-0.00012"},
{input: "0.0001234", prec: 3, expected: "0.000"},
{input: "-0.0001234", prec: 3, expected: "0.000"},
{input: "0.0001234", prec: 2, expected: "0.00"},
{input: "-0.0001234", prec: 2, expected: "0.00"},
{input: "0.0001234", prec: 0, expected: "0"},
{input: "-0.0001234", prec: 0, expected: "0"},
{input: "0.00001234", prec: -1, expected: "0"},
{input: "-0.00001234", prec: -1, expected: "0"},
{input: "0.00001234", prec: -5, expected: "0"},
{input: "-0.00001234", prec: -5, expected: "0"},
{input: "0.00001234", prec: -9, expected: "0"},
{input: "-0.00001234", prec: -9, expected: "0"},

t.Run("1.23456789 with prec = 9, expected 1.23456789", func(t *testing.T) {
v := MustNewFromString("1.23456789")
s := v.FormatString(9)
assert.Equal("1.234567890", s)
})
{input: "12.3456789", prec: 10, expected: "12.3456789000"},
{input: "-12.3456789", prec: 10, expected: "-12.3456789000"},
{input: "12.3456789", prec: 9, expected: "12.345678900"},
{input: "-12.3456789", prec: 9, expected: "-12.345678900"},
{input: "12.3456789", prec: 7, expected: "12.3456789"},
{input: "-12.3456789", prec: 7, expected: "-12.3456789"},
{input: "12.3456789", prec: 5, expected: "12.34567"},
{input: "-12.3456789", prec: 5, expected: "-12.34567"},
{input: "12.3456789", prec: 1, expected: "12.3"},
{input: "-12.3456789", prec: 1, expected: "-12.3"},
{input: "12.3456789", prec: 0, expected: "12"},
{input: "-12.3456789", prec: 0, expected: "-12"},
{input: "12.3456789", prec: -1, expected: "10"},
{input: "-12.3456789", prec: -1, expected: "-10"},
{input: "12.3456789", prec: -2, expected: "0"},
{input: "-12.3456789", prec: -2, expected: "0"},
{input: "12.3456789", prec: -3, expected: "0"},
{input: "-12.3456789", prec: -3, expected: "0"},

t.Run("1.02345678 with prec = 9, expected 1.02345678", func(t *testing.T) {
v := MustNewFromString("1.02345678")
s := v.FormatString(9)
assert.Equal("1.023456780", s)
})
{input: "12345678.9", prec: 10, expected: "12345678.9000000000"},
{input: "-12345678.9", prec: 10, expected: "-12345678.9000000000"},
{input: "12345678.9", prec: 3, expected: "12345678.900"},
{input: "-12345678.9", prec: 3, expected: "-12345678.900"},
{input: "12345678.9", prec: 1, expected: "12345678.9"},
{input: "-12345678.9", prec: 1, expected: "-12345678.9"},
{input: "12345678.9", prec: 0, expected: "12345678"},
{input: "-12345678.9", prec: 0, expected: "-12345678"},
{input: "12345678.9", prec: -2, expected: "12345600"},
{input: "-12345678.9", prec: -2, expected: "-12345600"},
{input: "12345678.9", prec: -5, expected: "12300000"},
{input: "-12345678.9", prec: -5, expected: "-12300000"},
{input: "12345678.9", prec: -7, expected: "10000000"},
{input: "-12345678.9", prec: -7, expected: "-10000000"},
{input: "12345678.9", prec: -8, expected: "0"},
{input: "-12345678.9", prec: -8, expected: "0"},
{input: "12345678.9", prec: -10, expected: "0"},
{input: "-12345678.9", prec: -10, expected: "0"},

t.Run("-0.57 with prec = 5, expected -0.57", func(t *testing.T) {
v := MustNewFromString("-0.57")
s := v.FormatString(5)
assert.Equal("-0.57000", s)
})
{input: "123000", prec: 7, expected: "123000.0000000"},
{input: "-123000", prec: 7, expected: "-123000.0000000"},
{input: "123000", prec: 2, expected: "123000.00"},
{input: "-123000", prec: 2, expected: "-123000.00"},
{input: "123000", prec: 0, expected: "123000"},
{input: "-123000", prec: 0, expected: "-123000"},
{input: "123000", prec: -1, expected: "123000"},
{input: "-123000", prec: -1, expected: "-123000"},
{input: "123000", prec: -5, expected: "100000"},
{input: "-123000", prec: -5, expected: "-100000"},
{input: "123000", prec: -6, expected: "0"},
{input: "-123000", prec: -6, expected: "0"},
{input: "123000", prec: -8, expected: "0"},
{input: "-123000", prec: -8, expected: "0"},
}

t.Run("-1.23456789 with prec = 9, expected 1.23456789", func(t *testing.T) {
v := MustNewFromString("-1.23456789")
s := v.FormatString(9)
assert.Equal("-1.234567890", s)
})

t.Run("-0.00001234 with prec = 3, expected = 0.000", func(t *testing.T) {
v := MustNewFromString("-0.0001234")
s := v.FormatString(3)
assert.Equal("0.000", s)
})

// comment out negative precision for dnum testing
/*
t.Run("12.3456789 with prec = -1, expected 10", func(t *testing.T) {
v := MustNewFromString("12.3456789")
s := v.FormatString(-1)
assert.Equal("10", s)
})
t.Run("12.3456789 with prec = -3, expected = 0", func(t *testing.T) {
v := MustNewFromString("12.3456789")
s := v.FormatString(-2)
assert.Equal("0", s)
for _, c := range cases {
t.Run(fmt.Sprintf("%s with prec = %d, expected %s", c.input, c.prec, c.expected), func(t *testing.T) {
v := MustNewFromString(c.input)
s := v.FormatString(c.prec)
assert.Equal(t, c.expected, s)
})
*/
}
}
}
36 changes: 29 additions & 7 deletions pkg/fixedpoint/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,25 +270,46 @@ func (dn Value) FormatString(prec int) string {
nd := len(digits)
e := int(dn.exp) - nd
if -maxLeadingZeros <= dn.exp && dn.exp <= 0 {
if prec < 0 {
return "0"
}
// decimal to the left
if prec+e+nd > 0 {
return sign + "0." + strings.Repeat("0", -e-nd) + digits[:min(prec+e+nd, nd)] + strings.Repeat("0", max(0, prec-nd+e+nd))
} else if -e-nd > 0 {
return "0." + strings.Repeat("0", -e-nd)
} else if -e-nd > 0 && prec != 0 {
return "0." + strings.Repeat("0", min(prec, -e-nd))
} else {
return "0"
}
} else if -nd < e && e <= -1 {
// decimal within
dec := nd + e
decimals := digits[dec:min(dec+prec, nd)]
return sign + digits[:dec] + "." + decimals + strings.Repeat("0", max(0, prec-len(decimals)))
if prec > 0 {
decimals := digits[dec:min(dec+prec, nd)]
return sign + digits[:dec] + "." + decimals + strings.Repeat("0", max(0, prec-len(decimals)))
} else if prec == 0 {
return sign + digits[:dec]
}

sigFigures := digits[0:max(dec+prec, 0)]
if len(sigFigures) == 0 {
return "0"
}

return sign + sigFigures + strings.Repeat("0", max(-prec, 0))

} else if 0 < dn.exp && dn.exp <= digitsMax {
// decimal to the right
if prec > 0 {
return sign + digits + strings.Repeat("0", e) + "." + strings.Repeat("0", prec)
} else {
} else if prec+e >= 0 {
return sign + digits + strings.Repeat("0", e)
} else {
if len(digits) <= -prec-e {
return "0"
}

return sign + digits[0:len(digits)+prec+e] + strings.Repeat("0", -prec)
}
} else {
// scientific notation
Expand Down Expand Up @@ -1176,7 +1197,8 @@ func align(x, y *Value) bool {
}
yshift = e
// check(0 <= yshift && yshift <= 20)
y.coef = (y.coef + halfpow10[yshift]) / pow10[yshift]
//y.coef = (y.coef + halfpow10[yshift]) / pow10[yshift]
y.coef = (y.coef) / pow10[yshift]
// check(int(y.exp)+yshift == int(x.exp))
return true
}
Expand Down Expand Up @@ -1342,4 +1364,4 @@ func (x Value) Clamp(min, max Value) Value {
return max
}
return x
}
}
93 changes: 93 additions & 0 deletions pkg/strategy/grid2/grid_dnum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//go:build dnum

package grid2

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGrid_HasPrice_Dnum(t *testing.T) {
t.Run("case1", func(t *testing.T) {
upper := number(500.0)
lower := number(100.0)
size := number(5.0)
grid := NewGrid(lower, upper, size, number(0.01))
grid.CalculateArithmeticPins()

assert.True(t, grid.HasPrice(number(500.0)), "upper price")
assert.True(t, grid.HasPrice(number(100.0)), "lower price")
assert.True(t, grid.HasPrice(number(200.0)), "found 200 price ok")
assert.True(t, grid.HasPrice(number(300.0)), "found 300 price ok")
})

t.Run("case2", func(t *testing.T) {
upper := number(0.9)
lower := number(0.1)
size := number(7.0)
grid := NewGrid(lower, upper, size, number(0.00000001))
grid.CalculateArithmeticPins()

assert.Equal(t, []Pin{
Pin(number("0.1")),
Pin(number("0.23333333")),
Pin(number("0.36666666")),
Pin(number("0.50000000")),
Pin(number("0.63333333")),
Pin(number("0.76666666")),
Pin(number("0.9")),
}, grid.Pins)

assert.False(t, grid.HasPrice(number(200.0)), "out of range")
assert.True(t, grid.HasPrice(number(0.9)), "upper price")
assert.True(t, grid.HasPrice(number(0.1)), "lower price")
assert.True(t, grid.HasPrice(number(0.5)), "found 0.49999999 price ok")
})

t.Run("case3", func(t *testing.T) {
upper := number(0.9)
lower := number(0.1)
size := number(7.0)
grid := NewGrid(lower, upper, size, number(0.0001))
grid.CalculateArithmeticPins()

assert.Equal(t, []Pin{
Pin(number(0.1)),
Pin(number(0.2333)),
Pin(number(0.3666)),
Pin(number(0.5000)),
Pin(number(0.6333)),
Pin(number(0.7666)),
Pin(number(0.9)),
}, grid.Pins)

assert.False(t, grid.HasPrice(number(200.0)), "out of range")
assert.True(t, grid.HasPrice(number(0.9)), "upper price")
assert.True(t, grid.HasPrice(number(0.1)), "lower price")
assert.True(t, grid.HasPrice(number(0.5)), "found 0.5 price ok")
assert.True(t, grid.HasPrice(number(0.2333)), "found 0.2333 price ok")
})

t.Run("case4", func(t *testing.T) {
upper := number(90.0)
lower := number(10.0)
size := number(7.0)
grid := NewGrid(lower, upper, size, number(0.001))
grid.CalculateArithmeticPins()

assert.Equal(t, []Pin{
Pin(number("10.0")),
Pin(number("23.333")),
Pin(number("36.666")),
Pin(number("50.00")),
Pin(number("63.333")),
Pin(number("76.666")),
Pin(number("90.0")),
}, grid.Pins)

assert.False(t, grid.HasPrice(number(200.0)), "out of range")
assert.True(t, grid.HasPrice(number("36.666")), "found 36.666 price ok")
})

}
Loading

0 comments on commit cd1314e

Please sign in to comment.