added sqrt testing

This commit is contained in:
Geert van Kempen 2018-08-19 14:00:50 -04:00
parent 3954520093
commit aa8a681072
3 changed files with 54 additions and 0 deletions

View file

@ -42,6 +42,33 @@ var Nine = New(9, 0)
// Ten defines value 10 in decimal format
var Ten = New(10, 0)
// Eleven defines value 11 in decimal format
var Eleven = New(11, 0)
// Twelve defines value 12 in decimal format
var Twelve = New(12, 0)
// Thirteen defines value 13 in decimal format
var Thirteen = New(13, 0)
// Fourteen defines value 14 in decimal format
var Fourteen = New(14, 0)
// Fifteen defines value 15 in decimal format
var Fifteen = New(15, 0)
// Sixteen defines value 16 in decimal format
var Sixteen = New(16, 0)
// Seventeen defines value 17 in decimal format
var Seventeen = New(17, 0)
// Eighteen defines value 18 in decimal format
var Eighteen = New(18, 0)
// Nineteen defines value 19 in decimal format
var Nineteen = New(19, 0)
// Twenty defines value 20 in decimal format
var Twenty = New(20, 0)

View file

@ -12,6 +12,9 @@ func (d Decimal) Sqrt() Decimal {
// SqrtRound returns the square root of d, the result will have
// precision digits after the decimal point. The bool precise returns whether the precision was reached
func (d Decimal) SqrtRound(precision int32) (Decimal, bool) {
if d.LessThanOrEqualZero() {
return Zero, false
}
cutoff := New(1, -precision)
lo := Zero
hi := d

24
sqrt_test.go Normal file
View file

@ -0,0 +1,24 @@
package decimal
import (
"testing"
)
func TestSqrt(t *testing.T) {
tables := []struct {
x Decimal
n Decimal
}{
{One, One},
{Four, Two},
{Sixteen, Four},
{Two, NewFromFloat(1.4142135623730951)},
}
for _, table := range tables {
result := table.x.Sqrt()
if result.NotEqual(table.n) {
t.Errorf("Sqrt of (%v) was incorrect, got: %v, want: %v.", table.x.String(), result.String(), table.n.String())
}
}
}