Hey folks,
I already figured, that it’s some kind of floating point issue, but I’m really not sure, how to fix it…
I wrote a method:
+ Collection {
groupWithinBandwidth {arg bandwidth = 0;
var sorted = this.sort;
var groups = [];
sorted.do { |item, i|
if (groups.size == 0){
groups = groups.add([item]);
}{
if ((groups[groups.size-1].first.postln) >= (item-bandwidth)){
groups[groups.size-1] = groups[groups.size-1].add(item)}{
groups = groups.add([item])
}
};
};
^groups
}
}
the minimal version of my problem is like this:
if I run it with integers:
a = [21,54,51,24,70]
a.groupWithinBandwidth(3)
-> [ [ 21, 24 ], [ 51, 54 ], [ 70 ] ]
it works just fine, but with floating point numbers:
a = [2.1, 5.1, 2.4,5.4,7]
a.groupWithinBandwidth(0.3)
-> [ [ 2.1, 2.4 ], [ 5.1 ], [ 5.4 ], [ 7 ] ]
it somehow doesn’t
but:
a = [2.1, 5.1, 2.4,5.4,7]*10
a.groupWithinBandwidth(3)
-> [ [ 21.0, 24.0 ], [ 51.0, 54.0 ], [ 70 ] ]
works…
when I investigate, I find that at around 2.pow(-52) a problem occurs:
a = [2.1, 5.1, 2.4,5.4,7]
a.groupWithinBandwidth(0.3+2.pow(-51))
-> [ [ 2.1, 2.4 ], [ 5.1, 5.4 ], [ 7 ] ]
but
a.groupWithinBandwidth(0.3+2.pow(-52))
-> [ [ 2.1, 2.4 ], [ 5.1 ], [ 5.4 ], [ 7 ] ]
I would love to understand, what is happening here
Best
Sem