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

Add Copy func #123

Merged
merged 7 commits into from Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 13 additions & 2 deletions decimal.go
Expand Up @@ -1553,7 +1553,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you revert this line? If this is gofmt issue, I will address this in separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I trying to revert it right now

zz := z.Mul(z)

if j == 1 || j == 2 {
Expand Down Expand Up @@ -1640,7 +1640,6 @@ 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,
Expand Down Expand Up @@ -1684,3 +1683,15 @@ func (d Decimal) Tan() Decimal {
}
return y
}

// Copy makes instance of d with same value and different pointer.
//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I think this line is unnecessary :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

func (d Decimal) Copy() Decimal {
d.ensureInitialized()
return Decimal{
value: &(*d.value),
exp: d.exp,
}
}


18 changes: 18 additions & 0 deletions decimal_test.go
Expand Up @@ -3082,3 +3082,21 @@ 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")
}

}