Hello,

I have identified a bug in the binary search in itkHistogram.hxx that make a loop run forever. The lines in question are these:

```
// Binary search for the bin where this measurement could be
mid = ( end + 1 ) / 2;
median = m_Min[dim][mid];
while ( true )
{
if ( tempMeasurement < median )
{
end = mid - 1;
}
else if ( tempMeasurement > median )
{
// test whether it is inside the current bin by comparing to the max of
// this bin.
if ( tempMeasurement < m_Max[dim][mid]
&& tempMeasurement >= m_Min[dim][mid] )
{
index[dim] = mid;
break;
}
// otherwise, continue binary search
begin = mid + 1;
}
else
{
index[dim] = mid;
break;
}
mid = begin + ( end - begin ) / 2;
median = m_Min[dim][mid];
} // end of while
```

With a few of the data sets Iâ€™m currently testing with, I never reach one of the two break statements. What happens is this:

- tempMeasurement is less than median, and we thus enter the first if statement.
- mid is at the moment end + 1, so the update within the if statement (end = mid - 1) doesnâ€™t change anything.
- In the bottom of the while loop mid and median are updated, but since begin is end + 1, the (end - begin)/2 part becomes zero, and mid is then set to begin - which it already is. So nothing happens here either.
- The median isnâ€™t changed because mid isnâ€™t changed.

So my question is this: has anyone else encountered this bug, and has it perhaps been fixed in later versions? Iâ€™m using 4.12 here, but I have checked the source of 4.13 and these lines seem to be the same.

Let me know if I can provide more details about the bug. Sadly I cannot easily share the input data.

A dump of my debuggerâ€™s state could be:

```
begin = 17344842
end = 17344841
mid = 17344842
median = 224.9340057373047
tempMeasurement = 224.93399047851562
```

Best regards,

Mads