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

Convert precision to localcontext #45

Merged
merged 1 commit into from
Jan 21, 2018

Conversation

njgheorghita
Copy link
Contributor

What was wrong?

DefaultContext was being used to set precision.

How was it fixed?

Added precision as optional kwarg to allow users to set their own precision (@pipermerriam was this the right move?) and set precision using localcontext.

Cute Animal Picture

giphy 45

closes #44

@@ -46,7 +43,7 @@
MAX_WEI = 2 ** 256 - 1


def from_wei(number, unit):
def from_wei(number, unit, precision=999):
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should be configurable. It should just be a constant, otherwise, bad user decisions lead to imprecise monetary calculations.

if d_number < 1 and '.' in s_number:
with localcontext() as ctx:
multiplier = len(s_number) - s_number.index('.') - 1
ctx.prec = multiplier
d_number = decimal.Decimal(value=number, context=ctx) * 10**multiplier
unit_value /= 10**multiplier

result_value = d_number * unit_value
with localcontext() as ctx:
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious if this one is needed. Do tests pass if it's removed? The d_number already has the appropriate precision from the context above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The two conversion_round_trip_* tests fail without this. Specifically when the number is '10000000000.000000000000000001' it drops the trailing 1.

@pipermerriam pipermerriam merged commit 0e1af14 into ethereum:master Jan 21, 2018
@pipermerriam
Copy link
Member

Note: I'll need to make a new v0 branch off of the most recent release since we've started merging some breaking changes into master and backport this fix into that branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default context of decimals changes after import
2 participants