Merge pull request #38 from edsrzf/sign

Add Sign method
This commit is contained in:
Victor Quinn 2017-02-15 21:22:53 -05:00 committed by GitHub
commit 4570ccd0d5
2 changed files with 32 additions and 0 deletions

View file

@ -413,6 +413,19 @@ func (d Decimal) Equals(d2 Decimal) bool {
return d.Equal(d2)
}
// Sign returns:
//
// -1 if d < 0
// 0 if d == 0
// +1 if d > 0
//
func (d Decimal) Sign() int {
if d.value == nil {
return 0
}
return d.value.Sign()
}
// Exponent returns the exponent, or scale component of the decimal.
func (d Decimal) Exponent() int32 {
return d.exp

View file

@ -536,6 +536,9 @@ func TestDecimal_Uninitialized(t *testing.T) {
if a.Cmp(b) != 0 {
t.Errorf("a != b")
}
if a.Sign() != 0 {
t.Errorf("a.Sign() != 0")
}
if a.Exponent() != 0 {
t.Errorf("a.Exponent() != 0")
}
@ -1330,6 +1333,22 @@ func TestPow(t *testing.T) {
}
}
func TestDecimal_Sign(t *testing.T) {
if Zero.Sign() != 0 {
t.Errorf("%q should have sign 0", Zero)
}
one := New(1, 0)
if one.Sign() != 1 {
t.Errorf("%q should have sign 1", one)
}
mone := New(-1, 0)
if mone.Sign() != -1 {
t.Errorf("%q should have sign -1", mone)
}
}
func didPanic(f func()) bool {
ret := false
func() {