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

Float operations can lead to a false fail of multipleOf #810

Closed
lorinkoz opened this issue May 21, 2021 · 3 comments
Closed

Float operations can lead to a false fail of multipleOf #810

lorinkoz opened this issue May 21, 2021 · 3 comments

Comments

@lorinkoz
Copy link

Hi, much ❤️ to the library.

I found this validation problem when using multipleOf:

>>> from jsonschema import validate
>>> validate(instance=10.1, schema={"type":"number", "multipleOf":0.1})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/python/site-packages/jsonschema/validators.py", line 934, in validate
    raise error
jsonschema.exceptions.ValidationError: 10.1 is not a multiple of 0.1

Failed validating 'multipleOf' in schema:
    {'multipleOf': 0.1, 'type': 'number'}

On instance:
    10.1

When printing the cotient that results from this code:
https://github.com/Julian/jsonschema/blob/0ea12edea319580473bda7e26ef088ff3a0983c5/jsonschema/_validators.py#L169

The cotient is 100.99999999999999

Therefore, int(quotient) can't be quotient on https://github.com/Julian/jsonschema/blob/0ea12edea319580473bda7e26ef088ff3a0983c5/jsonschema/_validators.py#L171

What to do?

@Julian
Copy link
Member

Julian commented May 21, 2021

Hi there, thanks!

If you want fixed point precision (or generally non-float behavior), you should use decimal.Decimal when deserializing your JSON.

@lorinkoz
Copy link
Author

Okay, thanks. I guess that's the solution, then. Keep up the good work!

@tdamsma
Copy link

tdamsma commented Nov 5, 2021

As this is the first issue I stumbled upon, I'm adding these links for reference: #687, #185, #320, #247

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

No branches or pull requests

3 participants