summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-03-20 13:16:43 -0400
committerTavian Barnes <tavianator@tavianator.com>2014-03-20 13:16:43 -0400
commit9144cf331f360ec49362e276a3333e4e551d6a61 (patch)
tree252238433f92f57b94198226d06112c369f2e44e
parent43075f1ba25d360c9c623b444e3089196e45ef5a (diff)
downloadkd-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.c8
1 files 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);