Skip to content

Commit

Permalink
Change trappy float comparison (#31889)
Browse files Browse the repository at this point in the history
Comparing primitive floats with '==' can be trappy because e.g. special handling
of NaN values. It can be avoided by using Float.compare().
  • Loading branch information
Christoph Büscher committed Jul 10, 2018
1 parent a5d5234 commit e60987b
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ private boolean tryRelocateShard(ModelNode minNode, ModelNode maxNode, String id
// simulate moving shard from maxNode to minNode
final float delta = weight.weightShardAdded(this, minNode, idx) - weight.weightShardRemoved(this, maxNode, idx);
if (delta < minCost ||
(candidate != null && delta == minCost && candidate.id() > shard.id())) {
(candidate != null && Float.compare(delta, minCost) == 0 && candidate.id() > shard.id())) {
/* this last line is a tie-breaker to make the shard allocation alg deterministic
* otherwise we rely on the iteration order of the index.getAllShards() which is a set.*/
minCost = delta;
Expand Down

0 comments on commit e60987b

Please sign in to comment.