garycourt / JSV

A JavaScript implementation of an extendable, fully compliant JSON Schema validator.
618 stars 84 forks source link

Rounding errors with divisibleBy #35

Closed pospi closed 12 years ago

pospi commented 12 years ago

This is really much more of a JavaScript limitation than any problem with your library.

But just wondering - when using "divisibleBy" validation with fractional values (the common use-case is dollar values, ie. divisibleBy 0.01), JavaScript has a nasty habit of introducing rounding errors.

So you end up getting things like 291.14 / 0.01 = 29113.999999999996, which really breaks things badly. How would you feel about working around this stuff by multiplying by the inverse of the divisor first ((291.14 * (0.01/1)) / (0.01 * (0.01/1)) = 29114)? Good idea, or outside the scope of your work?

cheers

garycourt commented 12 years ago

Rounding errors with "divisibleBy" were fixed in the draft-03 environment in version 4.0.