Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes issue when inserting negative zero

The hashcodes differ for Doubles representing 0.0 and -0.0, even
though 0.0 == -0.0. That's bad since we use a sorted tree map with
Double keys. This commit makes sure that -0.0 is coerced into 0.0.
  • Loading branch information...
commit 8689100adf9ebc5f431b22147f0f148cce70f621 1 parent ddb8fa6
Adam Ashenfelter ashenfad authored
2  project.clj
... ... @@ -1,4 +1,4 @@
1   -(defproject histogram "1.9.5"
  1 +(defproject histogram "1.9.6"
2 2 :description "Dynamic/streaming histograms"
3 3 :source-path "src/clj"
4 4 :java-source-path "src/java"
19 src/java/com/bigml/histogram/Bin.java
@@ -4,13 +4,18 @@
4 4 import org.json.simple.JSONArray;
5 5
6 6 public class Bin<T extends Target> {
7   -
  7 +
8 8 public Bin(double mean, double count, T target) {
9   - _mean = mean;
  9 + /* Hack to avoid Java's negative zero */
  10 + if (mean == 0d) {
  11 + _mean = 0d;
  12 + } else {
  13 + _mean = mean;
  14 + }
10 15 _count = count;
11 16 _target = target;
12 17 }
13   -
  18 +
14 19 public Bin(Bin<T> bin) {
15 20 this(bin.getMean(), bin.getCount(), (T) bin.getTarget().clone());
16 21 }
@@ -30,7 +35,7 @@ public double getCount() {
30 35 public double getMean() {
31 36 return _mean;
32 37 }
33   -
  38 +
34 39 public double getWeight() {
35 40 return _mean * (double) _count;
36 41 }
@@ -43,7 +48,7 @@ public void sumUpdate(Bin bin) throws BinUpdateException {
43 48 if (_mean != bin.getMean()) {
44 49 throw new BinUpdateException("Bins must have matching means to update");
45 50 }
46   -
  51 +
47 52 _count += bin.getCount();
48 53 _target.sum(bin.getTarget());
49 54 }
@@ -52,7 +57,7 @@ public void update(Bin bin) throws BinUpdateException {
52 57 if (_mean != bin.getMean()) {
53 58 throw new BinUpdateException("Bins must have matching means to update");
54 59 }
55   -
  60 +
56 61 _count = bin.getCount();
57 62 _target = (T) bin.getTarget();
58 63 }
@@ -61,7 +66,7 @@ public void update(Bin bin) throws BinUpdateException {
61 66 public String toString() {
62 67 return toJSON(new DecimalFormat(Histogram.DEFAULT_FORMAT_STRING)).toJSONString();
63 68 }
64   -
  69 +
65 70 public Bin combine(Bin<T> bin) {
66 71 double count = getCount() + bin.getCount();
67 72 double mean = (getWeight() + bin.getWeight()) / (double) count;
3  test/histogram/test/core.clj
@@ -264,3 +264,6 @@
264 264 (deftest variance-test
265 265 (is (about= (variance (reduce insert! (create) (normal-data 10000)))
266 266 1 0.05)))
  267 +
  268 +(deftest negative-zero-test
  269 + (is (= 1 (count (bins (reduce insert! (create) [0.0 -0.0]))))))

0 comments on commit 8689100

Please sign in to comment.
Something went wrong with that request. Please try again.