Skip to content

Commit

Permalink
IntUnscaled method (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanodevium authored May 9, 2023
1 parent aae2dfb commit 399acc9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fpdecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func FromFloat[T constraints.Float](v T) Decimal {

func FromIntScaled[T constraints.Integer](v T) Decimal { return Decimal{int64(v)} }

func (a Decimal) Scaled() int64 { return a.v }

func (a Decimal) Float32() float32 { return float32(a.v) / float32(multipliers[FractionDigits]) }

func (a Decimal) Float64() float64 { return float64(a.v) / float64(multipliers[FractionDigits]) }
Expand Down
20 changes: 20 additions & 0 deletions fpdecimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ func FuzzToFloat(f *testing.F) {
})
}

func FuzzScaled(f *testing.F) {
tests := []float64{
0,
0.001,
1,
123.456,
}
for _, tc := range tests {
f.Add(tc)
f.Add(-tc)
}
f.Fuzz(func(t *testing.T, v float64) {
a := fpdecimal.FromFloat(v)

if int64(v*1000) != a.Scaled() {
t.Error(a, a.Scaled(), int64(v*1000))
}
})
}

var floatsForTests = []struct {
name string
vals []string
Expand Down

0 comments on commit 399acc9

Please sign in to comment.