bgrins / TinyColor

Fast, small color manipulation and conversion for JavaScript
https://bgrins.github.io/TinyColor/
MIT License
5.05k stars 437 forks source link

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

Open jt3k opened 8 years ago

jt3k commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

bump

bgrins commented 8 years ago

Please see the comment at #124

jt3k commented 8 years ago

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

jt3k commented 8 years ago

bump