Skip to content

Commit

Permalink
dec: remove fnorm() call in dec.setInt
Browse files Browse the repository at this point in the history
This is to keep the dec implementation in line with big.nat: all
dec member functions assume right-aligned numbers.
  • Loading branch information
db47h committed May 4, 2020
1 parent f96bd0c commit 7a29e90
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
11 changes: 3 additions & 8 deletions dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ func (z dec) setWord(x Word) dec {
return z
}

// setInt sets z such that z*10**exp = x with 0 < z <= 1.
// Returns z and exp.
func (z dec) setInt(x *big.Int) (dec, uint) {
// setInt sets z = x.mant
func (z dec) setInt(x *big.Int) dec {
bb := x.Bits()
// TODO(db47h): here we cannot directly copy(b, bb)
b := make([]Word, len(bb))
Expand All @@ -107,11 +106,7 @@ func (z dec) setInt(x *big.Int) (dec, uint) {
z[i] = Word(divWVW(b, 0, b, _BD))
}
z = z.norm()
if len(z) == 0 {
return z, 0
}
s := dnorm(z)
return z, uint(len(z))*_WD - uint(s)
return z
}

// sticky returns 1 if there's a non zero digit within the
Expand Down
11 changes: 5 additions & 6 deletions dec_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package decimal

import (
"math/big"
"math/bits"
"math/rand"
"reflect"
Expand Down Expand Up @@ -46,11 +45,11 @@ func Test_mag(t *testing.T) {

// TODO(db47h): remove this function
func Test_dec_setInt(t *testing.T) {
// TODO(db47h): next step
b, _ := new(big.Int).SetString("12345678901234567890", 0)
d, exp := dec{}.make(3).setInt(b)
t.Log(d, exp)
t.Log(string(dtoa(d, 10)))
// // TODO(db47h): next step
// b, _ := new(big.Int).SetString("12345678901234567890", 0)
// d, exp := dec{}.make(3).setInt(b)
// t.Log(d, exp)
// t.Log(string(dtoa(d, 10)))
}

func Test_add10VW(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ func (z *Decimal) SetInt(x *big.Int) *Decimal {
return z
}
// x != 0
exp := uint(0)
z.mant, exp = z.mant.make((int(prec) + _WD - 1) / _WD).setInt(x)
z.setExpAndRound(int64(exp), 0)
z.mant = z.mant.make((int(prec) + _WD - 1) / _WD).setInt(x)
exp := dnorm(z.mant)
z.setExpAndRound(int64(len(z.mant))*_WD-exp, 0)
return z
}

Expand Down
16 changes: 9 additions & 7 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ import (

var intData = []struct {
s string
b int
p uint
d dec
pr uint
e int32
}{
{"1234567890123456789_0123456789012345678_9012345678901234567_8901234567890123456_78901234567890", 0,
{"00000000000000000001232", 10, 0, dec{1232000000000000000}, 19, 4},
{"1234567890123456789_0123456789012345678_9012345678901234567_8901234567890123456_78901234567890", 0, 0,
dec{7890123456789000000, 8901234567890123456, 9012345678901234567, 123456789012345678, 1234567890123456789},
90, 90},
{"1235", 0, dec{1235000000000000000}, _WD, 4},
{"1235", 3, dec{1240000000000000000}, 3, 4},
{"1245", 3, dec{1240000000000000000}, 3, 4},
{"12451", 3, dec{1250000000000000000}, 3, 5},
{"0", 0, nil, _WD, 0},
{"1235", 0, 0, dec{1235000000000000000}, _WD, 4},
{"1235", 0, 3, dec{1240000000000000000}, 3, 4},
{"1245", 0, 3, dec{1240000000000000000}, 3, 4},
{"12451", 0, 3, dec{1250000000000000000}, 3, 5},
{"0", 0, 0, nil, _WD, 0},
}

func TestDnorm(t *testing.T) {
Expand Down Expand Up @@ -58,7 +60,7 @@ func TestDnorm(t *testing.T) {
func TestDecimal_SetInt(t *testing.T) {
for i, td := range intData {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b, _ := new(big.Int).SetString(td.s, 0)
b, _ := new(big.Int).SetString(td.s, td.b)
d := new(Decimal).SetMode(ToNearestEven).SetPrec(td.p).SetInt(b)
if !reflect.DeepEqual(td.d, d.mant) {
t.Fatalf("\nexpected mantissa %v\n got %v", td.d, d.mant)
Expand Down

0 comments on commit 7a29e90

Please sign in to comment.