Skip to content

Commit 613fd24

Browse files
committed
Merge pull request flann-lib#186 from Adil-Ibragimov/distance_fix
Fixes in accum_dist methods
2 parents c713ad2 + 1b1a60f commit 613fd24

File tree

1 file changed

+10
-7
lines changed
  • src/cpp/flann/algorithms

1 file changed

+10
-7
lines changed

src/cpp/flann/algorithms/dist.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ struct HellingerDistance
642642
typedef typename Accumulator<T>::Type ResultType;
643643

644644
/**
645-
* Compute the histogram intersection distance
645+
* Compute the Hellinger distance
646646
*/
647647
template <typename Iterator1, typename Iterator2>
648648
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = -1) const
@@ -675,7 +675,8 @@ struct HellingerDistance
675675
template <typename U, typename V>
676676
inline ResultType accum_dist(const U& a, const V& b, int) const
677677
{
678-
return sqrt(static_cast<ResultType>(a)) - sqrt(static_cast<ResultType>(b));
678+
ResultType dist = sqrt(static_cast<ResultType>(a)) - sqrt(static_cast<ResultType>(b));
679+
return dist * dist;
679680
}
680681
};
681682

@@ -751,7 +752,7 @@ struct KL_Divergence
751752
Iterator1 last = a + size;
752753

753754
while (a < last) {
754-
if (* a != 0) {
755+
if ( *a != 0 && *b != 0 ) {
755756
ResultType ratio = (ResultType)(*a / *b);
756757
if (ratio>0) {
757758
result += *a * log(ratio);
@@ -774,10 +775,12 @@ struct KL_Divergence
774775
inline ResultType accum_dist(const U& a, const V& b, int) const
775776
{
776777
ResultType result = ResultType();
777-
ResultType ratio = (ResultType)(a / b);
778-
if (ratio>0) {
779-
result = a * log(ratio);
780-
}
778+
if( a != 0 && b != 0 ) {
779+
ResultType ratio = (ResultType)(a / b);
780+
if (ratio>0) {
781+
result = a * log(ratio);
782+
}
783+
}
781784
return result;
782785
}
783786
};

0 commit comments

Comments
 (0)