diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2014-03-20 13:16:43 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2014-03-20 13:16:43 -0400 |
commit | 9144cf331f360ec49362e276a3333e4e551d6a61 (patch) | |
tree | 252238433f92f57b94198226d06112c369f2e44e | |
parent | 43075f1ba25d360c9c623b444e3089196e45ef5a (diff) | |
download | kd-forest-9144cf331f360ec49362e276a3333e4e551d6a61.tar.xz |
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.
-rw-r--r-- | color.c | 8 |
1 files changed, 4 insertions, 4 deletions
@@ -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); |