Skip to content

Commit

Permalink
fixing subtle bug due to additional std::abs for long and cast from s…
Browse files Browse the repository at this point in the history
…ize_t to double (instead of long)
  • Loading branch information
bjoern-andres committed Oct 4, 2016
1 parent 6f86885 commit f0b0074
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
10 changes: 6 additions & 4 deletions include/andres/graph/lifting.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

#include <cassert>
#include <cstddef>
#include <cmath>
#include <cstdlib> // std::abs
#include <cmath> // std::abs since c++17
#include <stdexcept>
#include <iterator> // std::iterator_traits
#include <algorithm> // std::fill
Expand Down Expand Up @@ -118,7 +119,7 @@ lift(
{
const std::size_t offsetX = (metric == LiftingMetric::PathLength) ?
distanceUpperBound - offsetY:
::floor(::sqrt(distanceUpperBoundSquared - offsetY * offsetY));
std::floor(std::sqrt(distanceUpperBoundSquared - offsetY * offsetY));

const std::size_t col0 = cv[0] < offsetX ? 0 : cv[0] - offsetX;
std::size_t colN = cv[0] + offsetX;
Expand All @@ -129,7 +130,8 @@ lift(
{
if (metric == LiftingMetric::PathLength)
{
const std::size_t distance = std::abs(x - cv[0]) + std::abs(yPlus - 1 - cv[1]);
const std::size_t distance = std::abs(static_cast<long>(x - cv[0]))
+ std::abs(static_cast<long>(yPlus - 1 - cv[1]));

if (distance > distanceLowerBound)
{
Expand Down Expand Up @@ -189,7 +191,7 @@ lift(
{
const std::size_t offsetX = (metric == LiftingMetric::PathLength) ?
distanceUpperBound - offsetY :
::floor(::sqrt(distanceUpperBoundSquared - offsetY * offsetY));
std::floor(std::sqrt(distanceUpperBoundSquared - offsetY * offsetY));

const std::size_t col0 = cv[0] < offsetX ? 0 : cv[0] - offsetX;
std::size_t colN = cv[0] + offsetX;
Expand Down
22 changes: 22 additions & 0 deletions src/andres/graph/unit-test/lifting.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ void testLiftGridGraphPathLengthMetric() {
andres::graph::Graph<> graphLifted;
andres::graph::lift(graph, graphLifted, distanceUpperBound, distanceLowerBound); // tested by function testLiftGraph

/*
std::cout << "lb=" << distanceLowerBound
<< ", up=" << distanceUpperBound
<< std::endl;
for(size_t v = 0; v < gridGraph.numberOfVertices(); ++v) {
andres::graph::GridGraph<2>::VertexCoordinate vc;
gridGraph.vertex(v, vc);
std::cout << "neighbors of node "
<< "(" << vc[0] << ", " << vc[1] << ")" << std::endl;
for(auto it = graphLifted.verticesFromVertexBegin(v); it != graphLifted.verticesFromVertexEnd(v); ++it) {
gridGraph.vertex(*it, vc);
std::cout << "(" << vc[0] << ", " << vc[1] << ") ";
}
std::cout << std::endl;
for(auto it = gridGraphLifted.verticesFromVertexBegin(v); it != gridGraphLifted.verticesFromVertexEnd(v); ++it) {
gridGraph.vertex(*it, vc);
std::cout << "(" << vc[0] << ", " << vc[1] << ") ";
}
std::cout << std::endl;
}
*/

test(gridGraphLifted.numberOfVertices() == gridGraph.numberOfVertices());
test(gridGraphLifted.numberOfEdges() == graphLifted.numberOfEdges());
for(size_type v = 0; v < graphLifted.numberOfVertices(); ++v)
Expand Down

0 comments on commit f0b0074

Please sign in to comment.