aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLode Vandevenne <lode@google.com>2013-02-14 17:42:56 +0100
committerLode Vandevenne <lode@google.com>2013-02-14 17:42:56 +0100
commitacc035299f8dfe1ddcbc93c99f8600269790f88c (patch)
treef1aecdb841ecdd2b06555782eea7ccddc11cf19c
parent3c3a3bdeb667cfcdd622ff7fefd2c4934c16a09b (diff)
downloadzopfli-acc035299f8dfe1ddcbc93c99f8600269790f88c.tar.gz
Fix floating point issue on some compilers
-rw-r--r--tree.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/tree.c b/tree.c
index f9ac812..98abe48 100644
--- a/tree.c
+++ b/tree.c
@@ -83,6 +83,12 @@ void CalculateEntropy(const size_t* count, size_t n, double* bitlengths) {
its count is 1.*/
if (count[i] == 0) bitlengths[i] = log2sum;
else bitlengths[i] = log2sum - log(count[i]) * kInvLog2;
+ /* Depending on compiler and architecture, the above subtraction of two
+ floating point numbers may give a negative result very close to zero
+ instead of zero (e.g. -5.973954e-17 with gcc 4.1.2 on Ubuntu 11.4). Clamp
+ it to zero. These floating point imprecisions do not affect the cost model
+ significantly so this is ok. */
+ if (bitlengths[i] < 0 && bitlengths[i] > -1e-5) bitlengths[i] = 0;
assert(bitlengths[i] >= 0);
}
}