TinyColor icon indicating copy to clipboard operation
TinyColor copied to clipboard

The problem with float-point-numbers in the "mix" test

Open jt3k opened this issue 9 years ago • 5 comments

It is the problem of storing floating point numbers in javascript

look at this line https://github.com/bgrins/TinyColor/blob/master/test/test.js#L724 here (1 - (i / 100)) in this example if we assign "i" as 8, we get this result:

i = 8;
(1 - (i / 100)); // => 0.9299999999999999

but in real life 1 - 8/100 = 1 - 0.08 = 0.92


Tests are passed well, though contain this error.

Probably the same problem is present in the implementation of the mix method.

jt3k avatar Mar 14 '16 21:03 jt3k

i fixed this problem in the following way: before:

i = 8;
(1 - (i / 100)); // => 0.9299999999999999

after:

i = 8;
(100 - i) / 100; // => 0.92

also i fixed implementation of method "mix"

now is looks very simple

jt3k avatar Mar 14 '16 22:03 jt3k

bump

jt3k avatar Mar 17 '16 11:03 jt3k

Please see the comment at #124

bgrins avatar Mar 21 '16 17:03 bgrins

OK thanks. I am currently a little busy. within a week I will add checks in tests

jt3k avatar Mar 22 '16 07:03 jt3k

bump

jt3k avatar Apr 14 '16 08:04 jt3k