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

Inaccurate Float-to-Decimal Conversion in parse_value of Decimal Scalar #1593

Open
mak626 opened this issue Feb 10, 2025 · 0 comments · May be fixed by #1594
Open

Inaccurate Float-to-Decimal Conversion in parse_value of Decimal Scalar #1593

mak626 opened this issue Feb 10, 2025 · 0 comments · May be fixed by #1594
Labels

Comments

@mak626
Copy link

mak626 commented Feb 10, 2025

Current Behavior

The parse_value function currently converts the input using _Decimal(value). When a float is passed as input, this conversion may lead to precision loss due to the inherent imprecision of floating-point representations.

Steps to Reproduce

  1. Call Decimal.parse_value(0.01).
  2. Observe that the resulting Decimal does not accurately represent the value 0.01 instead gives0.01000000000000000020816681711721685132943093776702880859375 which is inaccurate.

Expected Behavior

The function should convert the input to a string before creating the Decimal object (i.e., using _Decimal(str(value))). This approach ensures that decimal values are accurately converted, even if not provided as string, preserving their intended precision.

see Strawberry implementation

Suggested Fix

Modify the parse_value function as follows:

@staticmethod
def parse_value(value):
    try:
        return _Decimal(str(value))
    except Exception:
        return Undefined
@mak626 mak626 changed the title Inaccurate Float-to-Decimal Conversion in parse_value of Decimal scalar Inaccurate Float-to-Decimal Conversion in parse_value of Decimal Scalar Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant