FEATURE: [grid2] using dnum

This commit is contained in:
gx578007 2023-03-20 18:00:41 +08:00
parent b48f9701fb
commit 0e2e8306b4
4 changed files with 191 additions and 89 deletions

View File

@ -1176,7 +1176,8 @@ func align(x, y *Value) bool {
} }
yshift = e yshift = e
// check(0 <= yshift && yshift <= 20) // 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)) // check(int(y.exp)+yshift == int(x.exp))
return true return true
} }
@ -1342,4 +1343,4 @@ func (x Value) Clamp(min, max Value) Value {
return max return max
} }
return x return x
} }

View File

@ -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")
})
}

View File

@ -0,0 +1,93 @@
//go:build !dnum
package grid2
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGrid_HasPrice(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.49999999)),
Pin(number(0.63333332)),
Pin(number(0.76666665)),
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.49999999)), "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")
})
}

View File

@ -1,5 +1,3 @@
//go:build !dnum
package grid2 package grid2
import ( import (
@ -126,90 +124,6 @@ func TestGrid_NextLowerPin(t *testing.T) {
assert.Equal(t, Pin(fixedpoint.Zero), next) assert.Equal(t, Pin(fixedpoint.Zero), next)
} }
func TestGrid_HasPrice(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.49999999)),
Pin(number(0.63333332)),
Pin(number(0.76666665)),
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.49999999)), "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")
})
}
func TestGrid_NextHigherPin(t *testing.T) { func TestGrid_NextHigherPin(t *testing.T) {
upper := number(500.0) upper := number(500.0)
lower := number(100.0) lower := number(100.0)
@ -283,6 +197,7 @@ func Test_calculateArithmeticPins(t *testing.T) {
Pin(number(2800.000)), Pin(number(2800.000)),
Pin(number(2866.660)), Pin(number(2866.660)),
Pin(number(2933.330)), Pin(number(2933.330)),
Pin(number("3000.00")),
}, },
}, },
} }
@ -298,4 +213,4 @@ func Test_calculateArithmeticPins(t *testing.T) {
} }
}) })
} }
} }