Skip to content

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

@lorinkoz

Description

@lorinkoz

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions