From e10b7cb121e2637ca0002ff91712f1353479f8f6 Mon Sep 17 00:00:00 2001 From: redaktor Date: Thu, 20 Nov 2014 13:07:27 +0100 Subject: [PATCH] Floating number problem and multipleOf Your fix was proposed in https://github.com/geraintluff/tv4/issues/167 and this is an according pull request. This would close https://github.com/geraintluff/tv4/issues/158 and https://github.com/geraintluff/tv4/issues/167 ! --- tv4.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tv4.js b/tv4.js index 2e08453..bf88e56 100644 --- a/tv4.js +++ b/tv4.js @@ -775,9 +775,11 @@ ValidatorContext.prototype.validateMultipleOf = function validateMultipleOf(data return null; } if (typeof data === "number") { - if (data % multipleOf !== 0) { - return this.createError(ErrorCodes.NUMBER_MULTIPLE_OF, {value: data, multipleOf: multipleOf}); - } + var CLOSE_ENOUGH = Math.pow(2, -51); + var remainder = (data/multipleOf)%1; + if (!(remainder <= CLOSE_ENOUGH || remainder >= 1- CLOSE_ENOUGH)) { + return this.createError(ErrorCodes.NUMBER_MULTIPLE_OF, {value: data, multipleOf: multipleOf}); + }; } return null; }; @@ -1615,4 +1617,4 @@ tv4.tv4 = tv4; return tv4; // used by _header.js to globalise. -})); \ No newline at end of file +}));