From: Barak Itkin Date: Fri, 18 May 2012 13:36:03 +0000 (+0300) Subject: Fix the TolerantIsPowerOfTwoLength function X-Git-Tag: p2tc-0.1.0~73 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8b1aa42ed6441182fbee5032423287b9fcfb436f;p=poly2tri-c Fix the TolerantIsPowerOfTwoLength function --- diff --git a/refine/delaunay-terminator.c b/refine/delaunay-terminator.c index e7a9df2..41bf550 100644 --- a/refine/delaunay-terminator.c +++ b/refine/delaunay-terminator.c @@ -397,10 +397,18 @@ LOG2 (gdouble v) static gboolean TolerantIsPowerOfTwoLength (gdouble length) { - gdouble exp = LOG2 (length); - gdouble intpart, frac = modf (exp, &intpart); - - return ABS(frac) < 0.05; + gdouble exp = LOG2 (length); + gdouble intpart, frac = modf (exp, &intpart); + gdouble distance; + + /* If the length is a negative power of 2, the returned fraction will be + * negative */ + frac = ABS(frac); + + /* Find how close is exp to the closest integer */ + distance = MIN(frac, 1-frac); + + return distance < 0.05; } static void