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

Fix loss of precision decimal numbers #179

Merged
merged 6 commits into from
Nov 21, 2022
Merged

Fix loss of precision decimal numbers #179

merged 6 commits into from
Nov 21, 2022

Conversation

dudzicz
Copy link
Collaborator

@dudzicz dudzicz commented Nov 17, 2022

Fixed the issue of loss of precision in the decimal representation of large floats. Converted float to Decimals and changed the precision of the context to 256 i.e. getcontext().prec = 256. Eventually returns the string representation of the Decimal object.

This fixes the issue of scientific notation representation of floats both in Argument value field and also other fields in models from decoded_model.py

@kchojn
Copy link
Collaborator

kchojn commented Nov 17, 2022

review done at the meeting

Copy link
Collaborator

@kchojn kchojn left a comment

Choose a reason for hiding this comment

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

@dudzicz, small changes needed

@@ -53,6 +59,12 @@ class Argument(BaseModel):
type: str
value: Any

@validator("value")
def decimal_conv(cls, v):
Copy link
Collaborator

Choose a reason for hiding this comment

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

types missing


from ethtx.models.base_model import BaseModel
from ethtx.models.objects_model import BlockMetadata
from ethtx.models.semantics_model import AddressSemantics, ERC20Semantics

# Set decimal operation precision to avoid loss of digits with arithmetic operations
getcontext().prec = 256
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe lets move it to module where BaseModel is declared

return "".join(new_str_format)
else:
return val_str
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

the same ^


new_str_format = ["0.0", "0" * num_zeros, digit_part]
return "".join(new_str_format)
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

else not needed

@@ -195,3 +201,25 @@ def create_transformation_context(
context["__print_input__"] = decode_call

return context


def _handle_decimal_representations(val):
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you add types?

.gitignore Outdated
@@ -110,5 +110,4 @@ dmypy.json
.pytype/

# Cython debug symbols
cython_debug/

cython_debug/
Copy link
Collaborator

Choose a reason for hiding this comment

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

git rollback, delete all changes for this file

@kchojn
Copy link
Collaborator

kchojn commented Nov 21, 2022

LGTM!

@dudzicz dudzicz merged commit 6c5bd2f into EthTx:master Nov 21, 2022
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.

2 participants