From 6962270db730d6511399ec060fa5e067cd36f545 Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Tue, 30 Sep 2025 16:56:17 +0200 Subject: [PATCH] improve floating point comparison --- .../lib/src/utils/ml/cluster_algo.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/flutter_mozilla_components/lib/src/utils/ml/cluster_algo.dart b/packages/flutter_mozilla_components/lib/src/utils/ml/cluster_algo.dart index e0fca89b..dfcbcff5 100644 --- a/packages/flutter_mozilla_components/lib/src/utils/ml/cluster_algo.dart +++ b/packages/flutter_mozilla_components/lib/src/utils/ml/cluster_algo.dart @@ -1,5 +1,7 @@ import 'dart:math' as math; +const double _epsilon = 1e-10; + /// Performs K-Means clustering with K-Means++ initialization of centroids. /// If an existing cluster is specified with [anchorIndices], then one of the centroids /// is the average of the embeddings of the items in the cluster. @@ -292,7 +294,7 @@ List _computeCentroid( bool _arePointsEqual(List point1, List point2) { if (point1.length != point2.length) return false; for (int i = 0; i < point1.length; i++) { - if (point1[i] != point2[i]) return false; + if ((point1[i] - point2[i]).abs() > _epsilon) return false; } return true; }