ICAROUS
All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Modules Pages
Public Member Functions | Static Public Member Functions | List of all members
larcfm::Util Class Reference

#include <Util.h>

Public Member Functions

int mod (int x, int y)
 

Static Public Member Functions

static bool almost_less (double a, double b)
 
static bool almost_less (double a, double b, INT64FM maxUlps)
 
static bool almost_greater (double a, double b)
 
static bool almost_greater (double a, double b, INT64FM maxUlps)
 
static bool almost_geq (double a, double b, INT64FM maxUlps)
 
static bool almost_geq (double a, double b)
 
static bool almost_leq (double a, double b, INT64FM maxUlps)
 
static bool almost_leq (double a, double b)
 
static bool almost_equals (double a, double b, INT64FM maxUlps)
 
static bool almost_equals (double a, double b)
 
static bool within_epsilon (double a, double b, double epsilon)
 
static bool within_epsilon (double a, double epsilon)
 
static double discretizeDir (double voz, double nvoz, double discreteUnits)
 
static double sq (const double x)
 
static INT64FM llabs (const INT64FM x)
 
static double sqrt_safe (const double x)
 
static double atan2_safe (const double y, const double x)
 ‍** More...
 
static double asin_safe (double x)
 
static double acos_safe (double x)
 
static double discr (const double a, const double b, const double c)
 
static double root (const double a, const double b, const double c, int eps)
 
static double root2b (const double a, const double b, const double c, const int eps)
 
static int sign (const double x)
 
static int signTriple (const double x)
 
static const double & min (const double &x, const double &y)
 
static const float & min (const float &x, const float &y)
 
static const int & min (const int &x, const int &y)
 
static const long & min (const long &x, const long &y)
 
static const double & max (const double &x, const double &y)
 
static const float & max (const float &x, const float &y)
 
static const int & max (const int &x, const int &y)
 
static const long & max (const long &x, const long &y)
 
static double modulo (double val, double mod)
 
static double safe_modulo (double val, double mod)
 
static double to_pi (double rad)
 
static double to_360 (double deg)
 
static double to_2pi (double rad)
 
static double to_pi2_cont (double rad)
 
static double to_180 (double deg)
 
static bool less_or_equal (std::string s1, std::string s2)
 
static bool clockwise (double alpha, double beta)
 
static int turnDir (double initTrack, double goalTrack)
 
static double turnDelta (double alpha, double beta)
 
static double signedTurnDelta (double alpha, double beta)
 
static double turnDelta (double alpha, double beta, bool turnRight)
 
static double turnDelta (double alpha, double beta, int dir)
 
static bool is_double (const std::string &str)
 
static bool is_boolean (const std::string &s)
 
static double parse_double (const std::string &str)
 
static double decimalDegrees (const std::string &degMinSec)
 
static double parse_time (const std::string &s)
 
static std::string hoursMinutesSeconds (double t)
 

Detailed Description

A broad collection of utility functions

Member Function Documentation

◆ acos_safe()

double larcfm::Util::acos_safe ( double  x)
static

a safe (won't return NaN or throw exceptions) version of arc-cosine

Parameters
xangle
Returns
the arc-cosine of x, between [0,pi)

◆ almost_equals() [1/2]

bool larcfm::Util::almost_equals ( double  a,
double  b 
)
static

Are these two numbers almost equal, given the PRECISION_DEFAULT

Parameters
aone number
banother number
Returns
true, if almost equals

◆ almost_equals() [2/2]

bool larcfm::Util::almost_equals ( double  a,
double  b,
INT64FM  maxUlps 
)
static

Determines if these two doubles, relative to each other, are almost equal. The "nearness" metric is captured in maxUlps. Mathematically, a == b is the same as a - b == 0. Due to quirks in floating point, generally almostEquals(a, b) is not the same as almostEquals(a - b, 0). The form without the subtraction is preferred.

Consistent with the IEEE-754 floating point specification, "not a number" (NaN) won't compare as equal to anything (including itself or another NaN).

If two doubles are almost_equals() with a maxUlps parameter of 16348, then this means there can be at most 16347 floating point numbers between them. A value of 16348 for "units of least precision" (ulps) corresponds to a and b agreeing to about 13 decimal places. Said another way, the two numbers have an absolute difference of (approximately) 1e-13 if the two floating point numbers are near 1.

The maxUlps parameter must be positive and smaller than 2^50

The implementation is based on the discussion (but not the code) in (google: comparing floats cygnus)

Parameters
aone number
banother number
maxUlpsthe precision, or more specifically, the maximum units of least precision
Returns
true, if almost equals

◆ almost_geq() [1/2]

bool larcfm::Util::almost_geq ( double  a,
double  b 
)
static

Determines if a is greater than or almost equal to b, according to the definition of the almostEquals() method.

Parameters
aone number
banother number
Returns
true, if almost great or equal

◆ almost_geq() [2/2]

bool larcfm::Util::almost_geq ( double  a,
double  b,
INT64FM  maxUlps 
)
static

Determines if a is greater than or almost equal to b, according to the definition of the almostEquals() method.

Parameters
aone number
banother number
maxUlpsmaximum units of least precision
Returns
true, if almost greater or equal

◆ almost_greater() [1/2]

bool larcfm::Util::almost_greater ( double  a,
double  b 
)
static

Determines if a > b, without being almost equal, according to the definition of the almostEquals() method.

Parameters
aone number
banother number
Returns
true, if almost_greater

◆ almost_greater() [2/2]

bool larcfm::Util::almost_greater ( double  a,
double  b,
INT64FM  maxUlps 
)
static

Determines if a > b, without being almost equal, according to the definition of the almostEquals() method.

Parameters
aone number
banother number
maxUlpsmaximum units of least precision
Returns
true, if almost_greater

◆ almost_leq() [1/2]

bool larcfm::Util::almost_leq ( double  a,
double  b 
)
static

Determines if a is less than or almost equal to b, according to the definition of the almostEquals() method.

Parameters
aone number
banother number
Returns
true, if almost less or equal

◆ almost_leq() [2/2]

bool larcfm::Util::almost_leq ( double  a,
double  b,
INT64FM  maxUlps 
)
static

Determines if a is less than or almost equal to b, according to the definition of the almostEquals() method.

Parameters
aone number
banother number
maxUlpsmaximum units of least precision
Returns
true, if almost less or equal

◆ almost_less() [1/2]

bool larcfm::Util::almost_less ( double  a,
double  b 
)
static

Determines if a < b, without being almost equal, according to the definition of the almostEquals() method..

Parameters
aone number
banother number
Returns
true, if almost_less

◆ almost_less() [2/2]

bool larcfm::Util::almost_less ( double  a,
double  b,
INT64FM  maxUlps 
)
static

Determines if a < b, without being almost equal, according to the definition of the almostEquals() method..

Parameters
aone number
banother number
maxUlpsmaximum units of least precision
Returns
true, if almost_less

◆ asin_safe()

double larcfm::Util::asin_safe ( double  x)
static

a safe (won't return NaN or throw exceptions) version of arc-sine

Parameters
xvalue
Returns
arc-sine of value

◆ atan2_safe()

double larcfm::Util::atan2_safe ( const double  y,
const double  x 
)
static

a safe (won't return NaN or throw exceptions) version of arc-tangent

Parameters
yordinate coordinate
xabscissa coordinate
Returns
arc-tangent

◆ clockwise()

bool larcfm::Util::clockwise ( double  alpha,
double  beta 
)
static

Returns true if a turn from track angle alpha to track angle beta is clockwise (by the shortest path). If the two angles are equal, then this function returns true.

Parameters
alphaone angle
betaanother angle
Returns
true, if clockwise

◆ decimalDegrees()

double larcfm::Util::decimalDegrees ( const std::string &  degMinSec)
static
Parameters
degMinSecLat/Lon String of the form "46:55:00" or "-111:57:00"
Returns
numbers of degrees in decimal form
Parameters
degMinSecLat/Lon string of the form "46:55:00" or "-111:57:00"
Returns
numbers of degrees decimal

◆ discr()

double larcfm::Util::discr ( const double  a,
const double  b,
const double  c 
)
static

Discriminant of a quadratic

Parameters
aa coefficient of quadratic
bb coefficient of quadratic
cc coefficient of quadratic
Returns
discriminant

◆ discretizeDir()

double larcfm::Util::discretizeDir ( double  voz,
double  nvoz,
double  discreteUnits 
)
static

Discretize the value of nvoz in the direction from voz in units of discreteUnits

Parameters
vozThe value nvoz was derived from
nvozThe value to be discretized
discreteUnitsthe size of discretization, e.g. 0.1, 1.0, 10.0, 100.0 etc
Returns
nvoz discretized to units if discreteUnits

◆ hoursMinutesSeconds()

string larcfm::Util::hoursMinutesSeconds ( double  t)
static

Convert the decimal time (in seconds) into a 0:00:00 string. This prints as many digits as necessary for the hours (one or two, typically). See time_str(double) for an alternate format.

Parameters
ttime in seconds
Returns
String of hours:mins:secs
Parameters
ttime in seconds
Returns
String of hours:mins:secs

◆ is_boolean()

bool larcfm::Util::is_boolean ( const std::string &  s)
static

Returns true if the stored value for key is likely a boolean

Parameters
sname
Returns
true if string value is true/false/t/f, false otherwise

◆ less_or_equal()

bool larcfm::Util::less_or_equal ( std::string  s1,
std::string  s2 
)
static

Returns true if string s1 is less than or equal to string s2.

Parameters
s1one string
s2another string
Returns
true, if s1 is less or equals to s2

◆ llabs()

INT64FM larcfm::Util::llabs ( const INT64FM  x)
static

return the absolute value

◆ max()

const double & larcfm::Util::max ( const double &  x,
const double &  y 
)
static

A maximum function that has the same behavior as the Java Math.max function: NaN is propagated for both x and y, and -0.0 is "less than" +0.0 Note that std::max(double,double) does NOT have the same behavior, especially if NaN is involved!

◆ min()

const double & larcfm::Util::min ( const double &  x,
const double &  y 
)
static

A minimum function that has the same behavior as the Java Math.min function: NaN is propagated for both x and y, and -0.0 is "less than" +0.0 Note that std::min(double,double) does NOT have the same behavior, especially if NaN is involved!

◆ mod()

int larcfm::Util::mod ( int  x,
int  y 
)

The behavior of the xy operator is different between Java and C++ if either x or y is negative. Use this to always return a value between 0 and y.

Parameters
xvalue
yrange
Returns
x mod y, having the same sign as y (Java behavior)

◆ modulo()

double larcfm::Util::modulo ( double  val,
double  mod 
)
static

Computes the modulo of val and mod. The returned value is in the range [0,mod)

Parameters
valnumerator
moddenominator, assumed/required to be non-zero
Returns
modulo value

Computes the modulo of val and mod. The returned value is in the range [0,mod)

Parameters
valnumerator
moddenominator
Returns
modulo value

◆ parse_double()

double larcfm::Util::parse_double ( const std::string &  str)
static

Returns a double value which is a representation of the given string. If the string does not represent a number, an arbitrary value is returned. In many cases, but not all, if the string is not a number then 0.0 is returned. However, on some platforms, "1abc" will return 1. If one wants to know the fact that the string is not a number, then use Util.is_double() method.

◆ parse_time()

double larcfm::Util::parse_time ( const std::string &  s)
static

Reads in a clock string and converts it to seconds. Accepts hh:mm:ss, mm:ss, and ss.

Parameters
sstring value
Returns
time in [s]

Reads in a clock string and converts it to seconds. Accepts hh:mm:ss, mm:ss, and ss.

◆ root()

double larcfm::Util::root ( const double  a,
const double  b,
const double  c,
int  eps 
)
static

Quadratic equation, eps = -1 or +1

Parameters
aa coefficient of quadratic
bb coefficient of quadratic
cc coefficient of quadratic
eps-1 or +1 (to indicate which solution you want)
Returns
root of quadratic

◆ root2b()

double larcfm::Util::root2b ( const double  a,
const double  b,
const double  c,
const int  eps 
)
static

root2b(a,b,c,eps) = root(a,2*b,c,eps) , eps = -1 or +1

Parameters
aa coefficient of quadratic
bb coefficient of quadratic
cc coefficient of quadratic
eps-1 or +1 (to indicate which solution you want)
Returns
root of quadratic

◆ safe_modulo()

double larcfm::Util::safe_modulo ( double  val,
double  mod 
)
static

Computes the modulo of val and mod. If mod > 0, the returned value is in the r ange [0,mod). Otherwise, returns val.

Parameters
valnumerator
moddenominator
Returns
modulo value

◆ sign()

int larcfm::Util::sign ( const double  x)
static

Returns +1 if the argument is positive or 0, -1 otherwise. Note: This is not the classic signum function from mathematics that returns 0 when a 0 is supplied.

Parameters
xvalue
Returns
sign of value

◆ signedTurnDelta()

double larcfm::Util::signedTurnDelta ( double  alpha,
double  beta 
)
static

Returns the smallest angle between two track angles [-PI,PI]. The sign indicates the direction of the turn, positive is clockwise, negative counterclockwise.

Parameters
alphaone angle
betaanother angle
Returns
angle difference

◆ signTriple()

int larcfm::Util::signTriple ( const double  x)
static

Return +1 if the value is greater than 0, -1 if it is less than 0, or 0.

Parameters
xvalue to be checked
Returns
sign as a number (including 0)

◆ sq()

double larcfm::Util::sq ( const double  x)
static

Square

Parameters
xvalue
Returns
square of value

◆ sqrt_safe()

double larcfm::Util::sqrt_safe ( const double  x)
static

a safe (won't return NaN or throw exceptions) version of square root

Parameters
xvalue
Returns
square root of value

◆ to_180()

double larcfm::Util::to_180 ( double  deg)
static

Converts deg degrees to the range (-180, 180].

Parameters
degDegrees
Returns
deg in the range (-180, 180].

◆ to_2pi()

double larcfm::Util::to_2pi ( double  rad)
static

Converts rad radians to the range [0, 2*pi].

Note: this should not be used for argument reduction for trigonometric functions (Math.sin(to_2pi(x)). Bad roundoff errors could occur.

Parameters
radRadians
Returns
rad in the range [0, 2*pi).

◆ to_360()

double larcfm::Util::to_360 ( double  deg)
static

Converts deg degrees to the range [0, 360).

Parameters
degDegrees
Returns
deg in the range [0, 360).

◆ to_pi()

double larcfm::Util::to_pi ( double  rad)
static

Converts rad radians to the range (-pi, pi].

Note: this should not be used for argument reduction for trigonometric functions (Math.sin(to_pi(x))

Parameters
radRadians
Returns
rad in the range (-pi, pi].

◆ to_pi2_cont()

double larcfm::Util::to_pi2_cont ( double  rad)
static

Converts rad radians to the range [-Math.PI/2, Math.PI/2). This function is continuous, so to_pi2_cont(PI/2+eps) equals PI/2-eps.

Parameters
radRadians
Returns
rad in the range [-Math.PI/2, Math.PI/2).

◆ turnDelta() [1/3]

double larcfm::Util::turnDelta ( double  alpha,
double  beta 
)
static

Returns the smallest angle between two track angles [0,PI]. Reminder: This is also the angle from BOT to EOT from the point of view of the turn center.
It is not angle between two segments from the vertex's point of view.

Parameters
alphaone angle
betaanother angle
Returns
non-negative difference in angle

◆ turnDelta() [2/3]

double larcfm::Util::turnDelta ( double  alpha,
double  beta,
bool  turnRight 
)
static

Returns the angle between two tracks when turning in direction indicated by turnRight flag [0,2PI] Note: this function can return an angle larger than PI!

Parameters
alphaone angle
betaanother angle
turnRightwhen true, measure angles from the right
Returns
angle difference

◆ turnDelta() [3/3]

double larcfm::Util::turnDelta ( double  alpha,
double  beta,
int  dir 
)
static

Returns the angle between two tracks when turning in direction indicated by turnRight flag [0,2PI] Note: this function can return an angle larger than PI!

Parameters
alphaone angle
betaanother angle
dir= +/- 1 + right, - left
Returns
angle difference

◆ turnDir()

int larcfm::Util::turnDir ( double  initTrack,
double  goalTrack 
)
static

Returns 1 if the minimal turn to goalTrack (i.e. less than pi) is to the right, else -1

Parameters
initTrackinitial track [rad]
goalTracktarget track [rad]
Returns
direction of turn

◆ within_epsilon() [1/2]

bool larcfm::Util::within_epsilon ( double  a,
double  b,
double  epsilon 
)
static

Comparison of two values to determine if their absolute difference is within a value epsilon. If the epsilon value is too small relative to the a and b values in question, then this is essentially the same as ==. Epsilon must be positive.

Parameters
aone number
banother number
epsilonmaximum difference
Returns
true, if values are within epsilon

◆ within_epsilon() [2/2]

bool larcfm::Util::within_epsilon ( double  a,
double  epsilon 
)
static

Returns true if the magnitude of a is less than epsilon. Epsilon must be positive.

Parameters
aa number
epsilonmaximum value
Returns
true, if value is within epsilon

The documentation for this class was generated from the following files: