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

multipleOf 0.1 fails for 92.6 #185

Closed
vlcinsky opened this issue Oct 12, 2014 · 6 comments
Closed

multipleOf 0.1 fails for 92.6 #185

vlcinsky opened this issue Oct 12, 2014 · 6 comments

Comments

@vlcinsky
Copy link

Using jsonschema 2.4.0 my data do not validate if expecting a number to be 92.6 and being

>>> import jsonschema
>>> schema = {"type": "number", "multipleOf": 0.1}
>>> jsonschema.validate(92.6, schema)
ValidationError                           Traceback (most recent call last)
<ipython-input-5-3d74877445e9> in <module>()
----> 1 jsonschema.validate(92.6, schema)

/home/javl/Envs/jsonizer/lib/python2.7/site-packages/jsonschema/validators.pyc in validate(instance, schema, cls, *args, **kwargs)
    426         cls = validator_for(schema)
    427     cls.check_schema(schema)
--> 428     cls(schema, *args, **kwargs).validate(instance)

/home/javl/Envs/jsonizer/lib/python2.7/site-packages/jsonschema/validators.pyc in validate(self, *args, **kwargs)
    115         def validate(self, *args, **kwargs):
    116             for error in self.iter_errors(*args, **kwargs):
--> 117                 raise error
    118 
    119         def is_type(self, instance, type):

ValidationError: 92.6 is not a multiple of 0.1

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

On instance:
    92.6
@Julian
Copy link
Member

Julian commented Oct 12, 2014

Unfortunately this is just floating points. Those numbers aren't exactly representable as floats, so you're going to get False, there's nothing jsonschema can do about it, the numbers you get are not in fact multiples of each other.

If you need this, you can use decimals.

@Julian Julian closed this as completed Oct 12, 2014
@vlcinsky
Copy link
Author

@Julian I am aware of how tricky is this business with float numbers. Anyway, current implementation is attempting to get through this problem by introducing some FLOAT_PRECISION and it helps in many cases. I got a feeling, that using some precision relative to the "divisible by" value could bring better results.

Using decimals is not the way to go - if I read the data from YAML or JSON, I do not have Decimals at hand at that moment.

If the solution for multipleOf is not possible to find, I would understand, if it would be removed from JSON Schema spec, as it is cross-platform present problem.

Another thing is - jsonschema is supporting multipleOf - and this is failing in some cases. What to do with this situation?

@vlcinsky
Copy link
Author

Created pull request #186 adding (failing) test case for this issue.

@Julian
Copy link
Member

Julian commented Oct 12, 2014

How can precision help? Once you have the object that you get by typing (or json.loading) 92.6 you already have the number 92.599999999999994. Completely indistinguishable.

I'm not sure what you mean by decimals not being the way to go because you have JSON or YAML.

And about the spec, the spec doesn't specify how to load some JSON -- if you load it in a way that gets you numbers with the precision you need, it works. There's nothing wrong with the spec. (Although maybe someone's brought up the behavior you'll get with floating points, since it's definitely understandably surprising).

@paultiplady
Copy link

Another library implementing jsonschema has fixed this using the naive approach of "cast floats to Decimals":

horejsek/python-fastjsonschema@2aee6eb#diff-dbeb4514cd90df89f7e13c1d897b9b0aR305-R312

Any reason that wouldn't work here?

@Julian
Copy link
Member

Julian commented Aug 11, 2020

It does work here if you load floats into decimals, jsonschema just doesn't do that for you -- (it can't, it's used after the float has already been loaded).

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