From f28399518896bca05c2f179238ebbecb0c80f4ca Mon Sep 17 00:00:00 2001 From: Thijs Damsma Date: Fri, 5 Nov 2021 11:37:26 +0100 Subject: [PATCH] more lenient float multipleOf validation --- jsonschema/_validators.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index 9a07f5ec3..21dedc1fb 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -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