Skip to content

Commit 7a37758

Browse files
authored
Compare floats with epsilon instead of equality check with 0 (UBC-Thunderbots#3509)
Co-authored-by: Apeiros-46B <Apeiros-46B@users.noreply.github.com>
1 parent 75771b1 commit 7a37758

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/software/ai/navigator/obstacle/trajectory_obstacle.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#pragma once
22

3+
#include <cmath>
4+
35
#include "software/ai/navigator/obstacle/geom_obstacle.hpp"
46
#include "software/ai/navigator/trajectory/trajectory_path.h"
57
#include "software/geom/algorithms/contains.h"
68
#include "software/geom/algorithms/distance.h"
79
#include "software/geom/algorithms/intersects.h"
10+
#include "software/geom/geom_constants.h"
811

912
template <typename GEOM_TYPE>
1013
class TrajectoryObstacle : public GeomObstacle<GEOM_TYPE>
@@ -40,7 +43,7 @@ TrajectoryObstacle<GEOM_TYPE>::TrajectoryObstacle(const GEOM_TYPE& geom,
4043
template <typename GEOM_TYPE>
4144
bool TrajectoryObstacle<GEOM_TYPE>::contains(const Point& p, const double t_sec) const
4245
{
43-
if (t_sec == 0)
46+
if (std::abs(t_sec) < FIXED_EPSILON)
4447
{
4548
return ::contains(this->geom_, p);
4649
}
@@ -56,7 +59,7 @@ bool TrajectoryObstacle<GEOM_TYPE>::contains(const Point& p, const double t_sec)
5659
template <typename GEOM_TYPE>
5760
double TrajectoryObstacle<GEOM_TYPE>::distance(const Point& p, const double t_sec) const
5861
{
59-
if (t_sec == 0)
62+
if (std::abs(t_sec) < FIXED_EPSILON)
6063
{
6164
return ::distance(this->geom_, p);
6265
}
@@ -73,7 +76,7 @@ template <typename GEOM_TYPE>
7376
double TrajectoryObstacle<GEOM_TYPE>::signedDistance(const Point& p,
7477
const double t_sec) const
7578
{
76-
if (t_sec == 0)
79+
if (std::abs(t_sec) < FIXED_EPSILON)
7780
{
7881
return ::signedDistance(this->geom_, p);
7982
}
@@ -90,7 +93,7 @@ template <typename GEOM_TYPE>
9093
bool TrajectoryObstacle<GEOM_TYPE>::intersects(const Segment& segment,
9194
const double t_sec) const
9295
{
93-
if (t_sec == 0)
96+
if (std::abs(t_sec) < FIXED_EPSILON)
9497
{
9598
return ::intersects(this->geom_, segment);
9699
}

src/software/geom/algorithms/step_along_perimeter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include "software/geom/algorithms/step_along_perimeter.h"
22

33
#include <algorithm>
4+
#include <cmath>
45
#include <vector>
56

67
#include "software/geom/algorithms/closest_point.h"
78
#include "software/geom/algorithms/distance.h"
9+
#include "software/geom/geom_constants.h"
810
#include "software/geom/segment.h"
911

1012

@@ -21,7 +23,7 @@ Point stepAlongPerimeter(const Polygon& polygon, const Point& start,
2123
// finds the point closest to start point on the segment
2224
Point closest_start = closestPoint(start, polygon_segments[start_segment_index]);
2325

24-
if (travel_distance == 0.0)
26+
if (std::abs(travel_distance) < FIXED_EPSILON)
2527
{
2628
return closest_start;
2729
}

src/software/geom/angle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ inline constexpr double Angle::toDegrees() const
439439

440440
inline constexpr Angle Angle::mod(Angle divisor) const
441441
{
442-
if (divisor.toRadians() == 0)
442+
if (divisor.toRadians() < FIXED_EPSILON)
443443
{
444444
return Angle::fromRadians(toRadians());
445445
}

0 commit comments

Comments
 (0)