diff options
author | Lode Vandevenne <lode@google.com> | 2013-02-14 17:42:56 +0100 |
---|---|---|
committer | Lode Vandevenne <lode@google.com> | 2013-02-14 17:42:56 +0100 |
commit | acc035299f8dfe1ddcbc93c99f8600269790f88c (patch) | |
tree | f1aecdb841ecdd2b06555782eea7ccddc11cf19c | |
parent | 3c3a3bdeb667cfcdd622ff7fefd2c4934c16a09b (diff) | |
download | zopfli-acc035299f8dfe1ddcbc93c99f8600269790f88c.tar.gz |
Fix floating point issue on some compilers
-rw-r--r-- | tree.c | 6 |
1 files changed, 6 insertions, 0 deletions
@@ -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); } } |