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

optimize conversion #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 1 addition & 4 deletions decimal.go
Expand Up @@ -967,10 +967,7 @@ func (d Decimal) IntPart() int64 {

// BigInt returns integer component of the decimal as a BigInt.
func (d Decimal) BigInt() *big.Int {
scaledD := d.rescale(0)
i := &big.Int{}
i.SetString(scaledD.String(), 10)
return i
return new(big.Int).Set(d.value)
Copy link
Contributor

@serprex serprex Apr 3, 2024

Choose a reason for hiding this comment

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

Suggested change
return new(big.Int).Set(d.value)
scaledD := d.rescale(0)
return new(big.Int).Set(scaledD.value)

@mwoss this corrects your feedback. btw, why does rescale need to return a new Decimal when exp == d.exp? would expect with non-mutable API it'd be fine to return d

ofc, with d.rescale always allocating a fresh bigint, it'd be safe to return scaledD.value itself

Copy link
Contributor

Choose a reason for hiding this comment

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

Created #359

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for working on that :D


btw, why does rescale need to return a new Decimal when exp == d.exp? would expect with non-mutable API it'd be fine to return d

If I remember correctly we have done it because, in a few places in the codebase, we expect that rescale always returns a new decimal, so we can safely perform operations on d.value. It could be improved for sure, but we would need to track down all those places and safely update the code and docs.

}

// BigFloat returns decimal as BigFloat.
Expand Down