Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformat code base and update Decimal.Copy docs #249

Merged
merged 1 commit into from Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions decimal.go
Expand Up @@ -414,6 +414,15 @@ func NewFromFloatWithExponent(value float64, exp int32) Decimal {
}
}

// Copy returns a copy of decimal with the same value and exponent, but a different pointer to value.
func (d Decimal) Copy() Decimal {
d.ensureInitialized()
return Decimal{
value: &(*d.value),
exp: d.exp,
}
}

// rescale returns a rescaled version of the decimal. Returned
// decimal may be less precise if the given exponent is bigger
// than the initial exponent of the Decimal.
Expand Down Expand Up @@ -1755,7 +1764,7 @@ func (d Decimal) Sin() Decimal {
sign = !sign
j -= 4
}
z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic
z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic
zz := z.Mul(z)

if j == 1 || j == 2 {
Expand Down Expand Up @@ -1842,12 +1851,12 @@ var _tanQ = [...]Decimal{

// Tan returns the tangent of the radian argument x.
func (d Decimal) Tan() Decimal {

PI4A := NewFromFloat(7.85398125648498535156e-1) // 0x3fe921fb40000000, Pi/4 split into three parts
PI4B := NewFromFloat(3.77489470793079817668e-8) // 0x3e64442d00000000,
PI4C := NewFromFloat(2.69515142907905952645e-15) // 0x3ce8469898cc5170,
M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi

if d.Equal(NewFromFloat(0.0)) {
return d
}
Expand Down Expand Up @@ -1886,14 +1895,3 @@ func (d Decimal) Tan() Decimal {
}
return y
}

// Copy makes instance of d with same value and different pointer.
func (d Decimal) Copy() Decimal {
d.ensureInitialized()
return Decimal{
value: &(*d.value),
exp: d.exp,
}
}


36 changes: 17 additions & 19 deletions decimal_test.go
Expand Up @@ -556,6 +556,22 @@ func TestNewFromBigIntWithExponent(t *testing.T) {
}
}

func TestCopy(t *testing.T) {
origin := New(1, 0)
cpy := origin.Copy()

if cpy.Cmp(origin) != 0 {
t.Error("expecting copy and origin to be equals, but they are not")
}

//change value
cpy = cpy.Add(New(1, 0))

if cpy.Cmp(origin) == 0 {
t.Error("expecting copy and origin to have different values, but they are equal")
}
}

func TestJSON(t *testing.T) {
for _, x := range testTable {
s := x.short
Expand Down Expand Up @@ -2803,7 +2819,7 @@ func TestDecimal_Coefficient(t *testing.T) {

func TestDecimal_CoefficientInt64(t *testing.T) {
type Inp struct {
Dec string
Dec string
Coefficient int64
}

Expand Down Expand Up @@ -3288,21 +3304,3 @@ func ExampleNewFromFloat() {
//0.123123123123123
//-10000000000000
}

//For Copy
func TestCopy(t *testing.T) {
origin := New(1, 0)
cpy := origin.Copy()

if cpy.Cmp(origin) != 0 {
t.Error("copy and origin not equal")
}

//change value
cpy = cpy.Add(New(1, 0))

if cpy.Cmp(origin) == 0 {
t.Error("copy and origin are equal but expected not")
}

}