From 9144cf331f360ec49362e276a3333e4e551d6a61 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 20 Mar 2014 13:16:43 -0400 Subject: Fix corner case in color_comparator() and clarify a comment. If anum == 0 and bdenom == 0, we should check anum*sgn(adenom) < bnum*sgn(bdenom), not anum*adenom < bnum*bdenom. --- color.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/color.c b/color.c index 75cd04e..d6ee317 100644 --- a/color.c +++ b/color.c @@ -160,16 +160,16 @@ color_comparator(const void *a, const void *b) // Special-case zero numerators, because we treat 0/0 as 0, not NaN if (anum == 0.0 || bnum == 0.0) { - double lhs = anum*adenom; - double rhs = bnum*bdenom; + double lhs = anum*copysign(1.0, adenom); + double rhs = bnum*copysign(1.0, bdenom); return (lhs > rhs) - (lhs < rhs); } // The points are in the same/comparable quadrants. We can still avoid // calculating atan(n/d) though, because it's an increasing function in n/d. // We can also avoid a division, by noting that an/ad < bn/bd iff - // an*bd*sgn(ad*bd) < bn*ad*sgn(ad*bd). Due to the logic above, both sides of - // the equation must have the same sign, so the sgn()s are redundant. + // an*bd*sgn(ad*bd) < bn*ad*sgn(ad*bd). Due to the logic above, both + // denominators must have the same sign, so the sgn()s are redundant. double lhs = anum*bdenom; double rhs = bnum*adenom; return (lhs > rhs) - (lhs < rhs); -- cgit v1.2.3