Class WrappedFloat
Defined in File wrapped_float.hpp
Inheritance Relationships
Derived Type
public tap::algorithms::Angle
(Class Angle)
Class Documentation
-
class WrappedFloat
Wraps a float to allow easy comparison and manipulation of sensor readings that wrap (e.g. 0 to 360). Lower bound is “inclusive” and upper bound is “exclusive”. The range would be represented as [0, 360).
For bounds 0 - 10, logically:
10 + 1 == 1
0 - 1 == 9
0 == 10
Subclassed by tap::algorithms::Angle
Public Functions
-
WrappedFloat(float value, float lowerBound, float upperBound)
- Parameters:
value – [in] value to initialize with (doesn’t have to be wrapped)
lowerBound – [in] lower wrapping bound, must be less than
upperBound
lowerBound – [in] upper wrapping bound, must be higher than
lowerBound
-
inline WrappedFloat withSameBounds(const float value) const
-
bool operator==(const WrappedFloat &other) const
Two WrappedFloats are considered equal if their wrapped values are equal. This does not account for floating point imprecision, so for robust equality checks,
minDifference
should be used.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be compared
this
WrappedFloat.- Throws :
-
void operator+=(const WrappedFloat &other)
Adds a WrappedFloat to
this
WrappedFloat given they have the same lower and upper bounds.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be added to
this
WrappedFloat.- Throws :
-
void operator-=(const WrappedFloat &other)
Subtracts a WrappedFloat from
this
WrappedFloat given they have the same lower and upper bounds.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be subtracted from
this
WrappedFloat.- Throws :
-
WrappedFloat operator+(const WrappedFloat &other) const
Adds a given WrappedFloat to
this
WrappedFloat given they have the same lower and upper bounds, returning the resultant WrappedFloat.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be added with
this
WrappedFloat.- Throws :
- Returns:
: A new WrappedFloat with the additive value of
other
andthis
.
-
WrappedFloat operator-(const WrappedFloat &other) const
Subtracts a given WrappedFloat from
this
WrappedFloat given they have the same lower and upper bounds, returning the resultant WrappedFloat.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be subtracted from
this
WrappedFloat.- Throws :
- Returns:
: A new WrappedFloat with the subtractive value of
other
fromthis
.
-
void operator+=(float other)
Adds a WrappedFloat to
this
WrappedFloat given they have the same lower and upper bounds.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be added to
this
WrappedFloat.- Throws :
-
void operator-=(float other)
Subtracts a WrappedFloat from
this
WrappedFloat given they have the same lower and upper bounds.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be subtracted from
this
WrappedFloat.- Throws :
-
WrappedFloat operator+(float other) const
Adds a given WrappedFloat to
this
WrappedFloat given they have the same lower and upper bounds, returning the resultant WrappedFloat.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be added with
this
WrappedFloat.- Throws :
- Returns:
: A new WrappedFloat with the additive value of
other
andthis
.
-
WrappedFloat operator-(float other) const
Subtracts a given WrappedFloat from
this
WrappedFloat given they have the same lower and upper bounds, returning the resultant WrappedFloat.An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to be subtracted from
this
WrappedFloat.- Throws :
- Returns:
: A new WrappedFloat with the subtractive value of
other
fromthis
.
-
float minDifference(const WrappedFloat &other) const
Finds the minimum difference against another wrapped value. Can be thought of as the minimum distance between two points on a circle’s perimeter.
An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to compute the minDifference with.
- Throws :
- Returns:
: A float with the signed minimum distance.
-
float minDifference(const float &unwrappedValue) const
Finds the minimum difference against another value. Can be thought of as the minimum distance between two points on a circle’s perimeter.
An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
unwrappedValue – [in] The float to compute the minDifference with. It’s wrapped before computing
- Throws :
- Returns:
: A float with the signed minimum distance.
-
WrappedFloat minInterpolate(const WrappedFloat &other, float alpha) const
Interpolates along the smallest difference with another WrappedFloat.
An assertion error if the two WrappedFloats have different lower and upper bounds.
- Parameters:
other – [in] The WrappedFloat to interpolate between.
alpha – [in] A float between 0-1 (0 returns this WrappedFloat’s value, 1 returns the other’s)
- Throws :
-
void shiftBounds(float shiftMagnitude)
Shifts both bounds by the specified amount.
- Parameters:
shiftMagnitude – [in] the amount to add to each bound.
-
bool withinRange(const WrappedFloat &lowerBound, const WrappedFloat &upperBound) const
Checks whether
this
is within the wrapped range defined fromlowerBound
toupperBound
. For example given a value wrapped from 0 to 10, with the following conditions:this: 9, min: 3, max: 7, returns false.
this: 5, min: 3, max: 7, returns true.
this: 9, min: 7, max: 3, returns true.
this: 5, min: 7, max: 3, returns false.
An assertion error if the WrappedFloats themselves have different bounds.
- Parameters:
lowerBound – [in]
upperBound – [in]
- Throws :
- Returns:
whether
this
is within the specified range
-
inline float getUnwrappedValue() const
Returns the unwrapped value.
-
inline float getWrappedValue() const
Returns the wrapped value.
-
inline void setWrappedValue(float newWrappedValue)
Sets the wrapped value.
-
inline void setUnwrappedValue(float newUnwrappedValue)
Sets the unwrapped value.
-
inline WrappedFloat getNormalized() const
-
inline int getRevolutions() const
-
inline float getUpperBound() const
Returns the value’s upper bound.
-
inline float getLowerBound() const
Returns the value’s lower bound.
Public Static Functions
-
static float limitValue(const WrappedFloat &valueToLimit, const WrappedFloat &min, const WrappedFloat &max, int *status)
Limits the passed WrappedFloat between the closest of the min or max value if outside the min and max value’s wrapped range.
The min and max must have the same wrapped bounds as the valueToLimit.
For example given a value wrapped from -10 to 10, with the following conditions:
valueToLimit: 5, min: 1, max: 4, returns 4.
valueToLimit: 9, min: 1, max: 3, returns 1 (since valueToLimit is closest to 1).
valueToLimit: 9, min: 2, max: 1, returns 9 (since the range between min and max starts at 2, goes up to 9, then wraps around to 1).
An assertion error if the WrappedFloats have different lower and upper bounds.
- Parameters:
valueToLimit – [in] the WrappedFloat whose value it is to limit
min – [in] the WrappedFloat with the same bounds as valueToLimit that valueToLimit will be limited below.
max – [in] the WrappedFloat with the same bounds as valueToLimit that valueToLimit will be limited above.
status – [out] the status result (what operation the limitValue function performed). The status codes are described below:
0: No limiting performed
1: Limited to min value
2: Limited to max value
- Throws :
- Returns:
the limited value.
-
static float limitValue(const WrappedFloat &valueToLimit, const float min, const float max, int *status)
Runs the limitValue function from above, wrapping the min and max passed in to the same bounds as those of valueToLimit’s.
An assertion error if the WrappedFloats have different lower and upper bounds.
See also
- Parameters:
valueToLimit – [in] the WrappedFloat whose value it is to limit
min – [in] the WrappedFloat with the same bounds as valueToLimit that valueToLimit will be limited below.
max – [in] the WrappedFloat with the same bounds as valueToLimit that valueToLimit will be limited above.
status – [out] the status result (what operation the limitValue function performed). The status codes are described below:
0: No limiting performed
1: Limited to min value
2: Limited to max value
- Throws :
- Returns:
the limited value.
-
static float rangeOverlap(const WrappedFloat &lowerA, const WrappedFloat &upperA, const WrappedFloat &lowerB, const WrappedFloat &upperB)
Calculates how much of the two given wrapped ranges overlap. If mentally visualizing on a circle, this method takes two arbitrary arcs on the perimeter and returns the length of the overlapping portion(s).
An assertion error if the WrappedFloats themselves have different bounds.
- Parameters:
lowerA – [in] the first range’s lower bound
upperA – [in] the first range’s upper bound
lowerB – [in] the second range’s lower bound
upperB – [in] the second range’s upper bound
- Throws :
- Returns:
the total length of the overlapping region(s) as a float
Public Static Attributes
-
static float EPSILON = 1E-8
Maximum value between floats representing bounds at which they’re considered to be “equal” for assertions.