Skip to content

More lenient float multipleOf validation #878

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions jsonschema/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ def multipleOf(validator, dB, instance, schema):
quotient = instance / dB
try:
failed = int(quotient) != quotient
if failed and dB != int(dB):
# Checking if floats are integer multiples of non integer
# floats is asking for floating point errors. Use a more
# lenient validation that probably conforms to the users
# expectations where 101 * 0.1 == 10.1 would be true.
# This also conforms to behaviour in javascript jsonschema
# checkers
remainder = instance % dB
tolerance = float_info.epsilon * instance
failed = remainder > tolerance and dB - remainder > tolerance
except OverflowError:
# When `instance` is large and `dB` is less than one,
# quotient can overflow to infinity; and then casting to int
Expand Down