Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
ae4ac3e9
Unverified
Commit
ae4ac3e9
authored
Jul 09, 2019
by
望哥
Committed by
GitHub
Jul 09, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #7 from aliiohs/feature/DecimalChange
change some decimal method
parents
b8f25b42
129db7b4
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
37 deletions
+42
-37
decimal.go
math/big/decimal.go
+10
-5
decimal_benchmark_test.go
math/big/decimal_benchmark_test.go
+1
-1
decimal_test.go
math/big/decimal_test.go
+31
-31
No files found.
math/big/decimal.go
View file @
ae4ac3e9
...
...
@@ -391,8 +391,13 @@ func (d *Decimal) ToString() (str []byte) {
return
}
// FromString parses decimal from string.
func
(
d
*
Decimal
)
FromString
(
str
[]
byte
)
error
{
// FromBytes parses decimal from string.
func
(
d
*
Decimal
)
FromString
(
str
string
)
error
{
return
d
.
FromBytes
([]
byte
(
str
))
}
// FromBytes parses decimal from bytes.
func
(
d
*
Decimal
)
FromBytes
(
str
[]
byte
)
error
{
for
i
:=
0
;
i
<
len
(
str
);
i
++
{
if
!
isSpace
(
str
[
i
])
{
str
=
str
[
i
:
]
...
...
@@ -1077,7 +1082,7 @@ func (d *Decimal) ToUint() (uint64, error) {
// FromFloat64 creates a decimal from float64 value.
func
(
d
*
Decimal
)
FromFloat64
(
f
float64
)
error
{
s
:=
strconv
.
FormatFloat
(
f
,
'g'
,
-
1
,
64
)
return
d
.
From
String
([]
byte
(
s
))
return
d
.
From
Bytes
([]
byte
(
s
))
}
// ToFloat64 converts decimal to float64 value.
...
...
@@ -2277,7 +2282,7 @@ func NewDecFromFloatForTest(f float64) *Decimal {
// NewDecFromStringForTest creates a Decimal from string, as it returns no error, it should only be used in test.
func
NewDecFromStringForTest
(
s
string
)
*
Decimal
{
dec
:=
new
(
Decimal
)
_
=
dec
.
From
String
([]
byte
(
s
))
_
=
dec
.
From
Bytes
([]
byte
(
s
))
//todo terror.Log(errors.Trace(err))
return
dec
}
...
...
@@ -2295,7 +2300,7 @@ func NewMaxOrMinDec(negative bool, prec, frac int) *Decimal {
}
str
[
1
+
prec
-
frac
]
=
'.'
dec
:=
new
(
Decimal
)
_
=
dec
.
From
String
(
str
)
_
=
dec
.
From
Bytes
(
str
)
//todo terror.Log(errors.Trace(err))
return
dec
}
math/big/decimal_benchmark_test.go
View file @
ae4ac3e9
...
...
@@ -45,7 +45,7 @@ func BenchmarkRound(b *testing.B) {
}
for
i
:=
0
;
i
<
len
(
tests
);
i
++
{
tests
[
i
]
.
inputDec
.
From
String
([]
byte
(
tests
[
i
]
.
input
))
tests
[
i
]
.
inputDec
.
From
Bytes
([]
byte
(
tests
[
i
]
.
input
))
}
b
.
StartTimer
()
...
...
math/big/decimal_test.go
View file @
ae4ac3e9
...
...
@@ -78,7 +78,7 @@ func TestToInt(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
dec
Decimal
dec
.
From
String
([]
byte
(
tt
.
input
))
dec
.
From
Bytes
([]
byte
(
tt
.
input
))
result
,
ec
:=
dec
.
ToInt
()
assert
.
Equal
(
t
,
ec
,
tt
.
err
)
assert
.
Equal
(
t
,
result
,
tt
.
output
)
...
...
@@ -102,7 +102,7 @@ func TestToUint(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
dec
Decimal
_
=
dec
.
From
String
([]
byte
(
tt
.
input
))
_
=
dec
.
From
Bytes
([]
byte
(
tt
.
input
))
result
,
ec
:=
dec
.
ToUint
()
assert
.
Equal
(
t
,
ec
,
tt
.
err
)
assert
.
Equal
(
t
,
result
,
tt
.
output
)
...
...
@@ -140,7 +140,7 @@ func TestToFloat(t *testing.T) {
}
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
_
=
dec
.
From
String
([]
byte
(
ca
.
s
))
_
=
dec
.
From
Bytes
([]
byte
(
ca
.
s
))
f
,
_
:=
dec
.
ToFloat64
()
assert
.
Equal
(
t
,
f
,
ca
.
f
)
}
...
...
@@ -164,7 +164,7 @@ func TestToHashKey(t *testing.T) {
keys
:=
make
([]
string
,
0
,
len
(
ca
.
numbers
))
for
_
,
num
:=
range
ca
.
numbers
{
var
dec
Decimal
assert
.
Equal
(
t
,
dec
.
From
String
([]
byte
(
num
)),
nil
)
assert
.
Equal
(
t
,
dec
.
From
Bytes
([]
byte
(
num
)),
nil
)
key
,
err
:=
dec
.
ToHashKey
()
assert
.
Equal
(
t
,
err
,
nil
)
keys
=
append
(
keys
,
string
(
key
))
...
...
@@ -200,14 +200,14 @@ func TestToHashKey(t *testing.T) {
keys
:=
make
([]
string
,
0
,
len
(
ca
.
hashNumbers
)
+
len
(
ca
.
binNumbers
))
for
_
,
num
:=
range
ca
.
hashNumbers
{
var
dec
Decimal
assert
.
Equal
(
t
,
dec
.
From
String
([]
byte
(
num
)),
nil
)
assert
.
Equal
(
t
,
dec
.
From
Bytes
([]
byte
(
num
)),
nil
)
key
,
err
:=
dec
.
ToHashKey
()
assert
.
Equal
(
t
,
err
,
nil
)
keys
=
append
(
keys
,
string
(
key
))
}
for
_
,
num
:=
range
ca
.
binNumbers
{
var
dec
Decimal
assert
.
Equal
(
t
,
dec
.
From
String
([]
byte
(
num
)),
nil
)
assert
.
Equal
(
t
,
dec
.
From
Bytes
([]
byte
(
num
)),
nil
)
prec
,
frac
:=
dec
.
PrecisionAndFrac
()
// remove leading zeros but trailing zeros remain
key
,
err
:=
dec
.
ToBin
(
prec
,
frac
)
assert
.
Equal
(
t
,
err
,
nil
)
...
...
@@ -231,7 +231,7 @@ func TestRemoveTrailingZeros(t *testing.T) {
}
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
assert
.
Equal
(
t
,
dec
.
From
String
([]
byte
(
ca
)),
nil
)
assert
.
Equal
(
t
,
dec
.
From
Bytes
([]
byte
(
ca
)),
nil
)
// calculate the number of digits after point but trailing zero
digitsFracExp
:=
0
...
...
@@ -263,7 +263,7 @@ func TestShift(t *testing.T) {
var
dotest
=
func
(
t
*
testing
.
T
,
tests
[]
tcase
)
{
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
err
:=
dec
.
From
String
([]
byte
(
ca
.
input
))
err
:=
dec
.
From
Bytes
([]
byte
(
ca
.
input
))
//assert.Equal(t, err, IsNil)
//origin := dec
err
=
dec
.
Shift
(
ca
.
shift
)
...
...
@@ -394,7 +394,7 @@ func TestRoundWithHalfEven(t *testing.T) {
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
dec
.
From
String
([]
byte
(
ca
.
input
))
dec
.
From
Bytes
([]
byte
(
ca
.
input
))
var
rounded
Decimal
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
ModeHalfEven
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
...
...
@@ -428,7 +428,7 @@ func TestRoundWithTruncate(t *testing.T) {
}
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
dec
.
From
String
([]
byte
(
ca
.
input
))
dec
.
From
Bytes
([]
byte
(
ca
.
input
))
var
rounded
Decimal
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
ModeTruncate
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
...
...
@@ -463,7 +463,7 @@ func TestRoundWithCeil(t *testing.T) {
}
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
dec
.
From
String
([]
byte
(
ca
.
input
))
dec
.
From
Bytes
([]
byte
(
ca
.
input
))
var
rounded
Decimal
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
modeCeiling
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
...
...
@@ -490,7 +490,7 @@ func TestFromString(t *testing.T) {
}
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
err
:=
dec
.
From
String
([]
byte
(
ca
.
input
))
err
:=
dec
.
From
Bytes
([]
byte
(
ca
.
input
))
if
err
!=
nil
{
assert
.
Equal
(
t
,
err
,
ca
.
err
)
}
...
...
@@ -504,7 +504,7 @@ func TestFromString(t *testing.T) {
}
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
err
:=
dec
.
From
String
([]
byte
(
ca
.
input
))
err
:=
dec
.
From
Bytes
([]
byte
(
ca
.
input
))
assert
.
Equal
(
t
,
err
,
ca
.
err
)
result
:=
string
(
dec
.
ToString
())
assert
.
Equal
(
t
,
result
,
ca
.
output
)
...
...
@@ -524,7 +524,7 @@ func TestToString(t *testing.T) {
}
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
_
=
dec
.
From
String
([]
byte
(
ca
.
input
))
_
=
dec
.
From
Bytes
([]
byte
(
ca
.
input
))
result
:=
dec
.
ToString
()
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
}
...
...
@@ -557,7 +557,7 @@ func TestToBinFromBin(t *testing.T) {
}
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
err
:=
dec
.
From
String
([]
byte
(
ca
.
input
))
err
:=
dec
.
From
Bytes
([]
byte
(
ca
.
input
))
assert
.
Equal
(
t
,
err
,
nil
)
buf
,
err
:=
dec
.
ToBin
(
ca
.
precision
,
ca
.
frac
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
...
...
@@ -605,8 +605,8 @@ func TestCompare(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
a
,
b
Decimal
_
=
a
.
From
String
([]
byte
(
tt
.
a
))
_
=
b
.
From
String
([]
byte
(
tt
.
b
))
_
=
a
.
From
Bytes
([]
byte
(
tt
.
a
))
_
=
b
.
From
Bytes
([]
byte
(
tt
.
b
))
assert
.
Equal
(
t
,
a
.
Compare
(
&
b
),
tt
.
cmp
)
}
}
...
...
@@ -721,8 +721,8 @@ func TestSub(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
a
,
b
,
sum
Decimal
a
.
From
String
([]
byte
(
tt
.
a
))
b
.
From
String
([]
byte
(
tt
.
b
))
a
.
From
Bytes
([]
byte
(
tt
.
a
))
b
.
From
Bytes
([]
byte
(
tt
.
b
))
err
:=
DecimalSub
(
&
a
,
&
b
,
&
sum
)
assert
.
Equal
(
t
,
err
,
tt
.
err
)
result
:=
sum
.
ToString
()
...
...
@@ -752,8 +752,8 @@ func TestMul(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
a
,
b
,
product
Decimal
a
.
From
String
([]
byte
(
tt
.
a
))
b
.
From
String
([]
byte
(
tt
.
b
))
a
.
From
Bytes
([]
byte
(
tt
.
a
))
b
.
From
Bytes
([]
byte
(
tt
.
b
))
err
:=
DecimalMul
(
&
a
,
&
b
,
&
product
)
assert
.
Equal
(
t
,
err
,
tt
.
err
)
result
:=
product
.
String
()
...
...
@@ -787,8 +787,8 @@ func TestDivMod(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
a
,
b
,
to
Decimal
a
.
From
String
([]
byte
(
tt
.
a
))
b
.
From
String
([]
byte
(
tt
.
b
))
a
.
From
Bytes
([]
byte
(
tt
.
a
))
b
.
From
Bytes
([]
byte
(
tt
.
b
))
err
:=
doDivMod
(
&
a
,
&
b
,
&
to
,
nil
,
5
)
assert
.
Equal
(
t
,
err
,
tt
.
err
)
if
tt
.
err
==
ErrDivByZero
{
...
...
@@ -809,8 +809,8 @@ func TestDivMod(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
a
,
b
,
to
Decimal
a
.
From
String
([]
byte
(
tt
.
a
))
b
.
From
String
([]
byte
(
tt
.
b
))
a
.
From
Bytes
([]
byte
(
tt
.
a
))
b
.
From
Bytes
([]
byte
(
tt
.
b
))
ec
:=
doDivMod
(
&
a
,
&
b
,
nil
,
&
to
,
0
)
assert
.
Equal
(
t
,
ec
,
tt
.
err
)
if
tt
.
err
==
ErrDivByZero
{
...
...
@@ -829,8 +829,8 @@ func TestDivMod(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
a
,
b
,
to
Decimal
a
.
From
String
([]
byte
(
tt
.
a
))
b
.
From
String
([]
byte
(
tt
.
b
))
a
.
From
Bytes
([]
byte
(
tt
.
a
))
b
.
From
Bytes
([]
byte
(
tt
.
b
))
ec
:=
DecimalDiv
(
&
a
,
&
b
,
&
to
,
DivFracIncr
)
assert
.
Equal
(
t
,
ec
,
tt
.
err
)
if
tt
.
err
==
ErrDivByZero
{
...
...
@@ -847,8 +847,8 @@ func TestDivMod(t *testing.T) {
}
for
_
,
tt
:=
range
tests
{
var
a
,
b
,
to
Decimal
_
=
a
.
From
String
([]
byte
(
tt
.
a
))
_
=
b
.
From
String
([]
byte
(
tt
.
b
))
_
=
a
.
From
Bytes
([]
byte
(
tt
.
a
))
_
=
b
.
From
Bytes
([]
byte
(
tt
.
b
))
ec
:=
DecimalMod
(
&
a
,
&
b
,
&
to
)
assert
.
Equal
(
t
,
ec
,
tt
.
err
)
if
tt
.
err
==
ErrDivByZero
{
...
...
@@ -892,7 +892,7 @@ func BenchmarkBigDecimalToBin(b *testing.B) {
decs
:=
make
([]
*
Decimal
,
0
,
len
(
cases
))
for
_
,
ca
:=
range
cases
{
var
dec
Decimal
if
err
:=
dec
.
From
String
([]
byte
(
ca
));
err
!=
nil
{
if
err
:=
dec
.
From
Bytes
([]
byte
(
ca
));
err
!=
nil
{
b
.
Fatal
(
err
)
}
decs
=
append
(
decs
,
&
dec
)
...
...
@@ -915,7 +915,7 @@ func BenchmarkBigDecimalToHashKey(b *testing.B) {
decs
:=
make
([]
*
Decimal
,
0
,
len
(
cases
))
for
_
,
ca
:=
range
cases
{
var
dec
Decimal
if
err
:=
dec
.
From
String
([]
byte
(
ca
));
err
!=
nil
{
if
err
:=
dec
.
From
Bytes
([]
byte
(
ca
));
err
!=
nil
{
b
.
Fatal
(
err
)
}
decs
=
append
(
decs
,
&
dec
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment