Skip to content

Commit

Permalink
Fix cargo clippy errors (#175)
Browse files Browse the repository at this point in the history
In #174 the gnp random functions were extended to support values of 0
and 1 for the probability representing either empty of full graphs. In
that change comparisons between the value of probability and 1 were used
to check if it's a full graph and we should just fast path adding an
edge between every node. However, when running 'cargo clippy' on this it
rightfully points out that we should probably check for equality to 1
within an error margin (given that floating point is never exact).
Clippy suggested replacing using std::f64::EPSILON as the error value,
which is ~2.2e-16 [1] and replacing the comparison with:

(probability - 1.0).abs() < error

This commit just makes that change. While this is unlikely to cause any
issues in practice, because probability is a parameter and if someone is
going to want a full graph they'll likely call the function with
probability=1 in python. It's better to be safe just in case someone is
computing the probability value and could have some error on the value.

[1] https://doc.rust-lang.org/std/f64/constant.EPSILON.html
  • Loading branch information
mtreinish committed Oct 18, 2020
1 parent 7db288c commit ba6f6e1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ pub fn directed_gnp_random_graph(
));
}
if probability > 0.0 {
if probability == 1.0 {
if (probability - 1.0).abs() < std::f64::EPSILON {
for u in 0..num_nodes {
for v in 0..num_nodes {
if u != v {
Expand Down Expand Up @@ -1585,7 +1585,7 @@ pub fn undirected_gnp_random_graph(
));
}
if probability > 0.0 {
if probability == 1.0 {
if (probability - 1.0).abs() < std::f64::EPSILON {
for u in 0..num_nodes {
for v in u + 1..num_nodes {
let u_index = NodeIndex::new(u as usize);
Expand Down

0 comments on commit ba6f6e1

Please sign in to comment.