Skip to content

Commit 4b9ade9

Browse files
authored
Merge pull request #3347 from oddbookworm/color-lerp-tolerance
Added tolerance for Color.lerp
2 parents 1c2a083 + 2219caa commit 4b9ade9

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src_c/color.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,19 @@ _color_lerp(pgColorObject *self, PyObject *args, PyObject *kw)
808808
return NULL;
809809
}
810810

811-
if (amt < 0 || amt > 1) {
811+
// TOLERANCE to account for double precison floating point inaccuracy at
812+
// the very limits, like if you're LERP'ing by 0.01. When you hit the end,
813+
// something stupid like this might happen
814+
/* >>> value = 0
815+
>>> offset = 0.01
816+
>>> while value < 1:
817+
... value += offset
818+
...
819+
>>> print(value)
820+
1.0000000000000007
821+
*/
822+
static const double TOLERANCE = 1e-6;
823+
if ((amt < -TOLERANCE) || (amt > (1.0 + TOLERANCE))) {
812824
return RAISE(PyExc_ValueError, "Argument 2 must be in range [0, 1]");
813825
}
814826

0 commit comments

Comments
 (0)