Skip to content

Commit

Permalink
Fix compilation on ARM based systems (#1093)
Browse files Browse the repository at this point in the history
* Handle case where 0 is passed to FloatWidth()

* Change FloatWidth() function to MinNumFracChars(). FloatWidth() was a
misleading name; the original intent was to ensure when writing out
coordinates with a certain step size that the precision was large enough
to accomodate the step (e.g. a step of .001 would need at least a
precision of 3). Also rework the function so it is more accurate for
values that are not exactly powers of 10.

* 6.28.0. Minor revision bump for fixes for ARM; may also slightly affect
  (fix) the output precision of certain X step values in output ASCII
data formats in some cases.
  • Loading branch information
drroe authored Jul 19, 2024
1 parent 601bc1e commit e7482f3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
20 changes: 14 additions & 6 deletions src/StringRoutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,20 @@ int DigitWidth(long int numberIn) {
return (minusSign + numi);
}

// FloatWidth()
/** \return the number of characters necessary to express given float. */
int FloatWidth(double floatIn) {
double float_exponent = fabs( log10( floatIn ) );
++float_exponent;
return (int)float_exponent; // Cast to int implicitly rounds down
/** \return the number of characters necessary for a minimal representation of
* the fractional part of a floating point number, i.e. the number of
* leading zeros plus 1. characters necessary to express given float.
*/
int MinNumFracChars(double floatIn) {
// Split number into integer and fractional part
double intpart = 0;
double frac = modf(floatIn, &intpart);
// Leading zeros
int n_leading_zeros = 0;
double absfrac = fabs(frac);
if (absfrac > 0)
n_leading_zeros = (int)ceil( fabs( log10( absfrac ) ) ) - 1;
return n_leading_zeros + 1;
}

// ---------- STRING CONVERSION ROUTINES ---------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/StringRoutines.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ std::string AppendNumber(std::string const &, int);
int WildcardMatch(std::string const&, std::string const&);
/// \return number of characters needed to represent given digit.
int DigitWidth(long int);
/// \return number of characters needed to represent given floating point.
int FloatWidth(double);
/// \return Min number of chars needed to represent fractional part of given floating point.
int MinNumFracChars(double);
/// Remove any trailing whitespace from string.
void RemoveTrailingWhitespace(std::string &);
/// \return string stripped of trailing whitespace.
Expand Down
2 changes: 1 addition & 1 deletion src/TextFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void TextFormat::SetCoordFormat(size_t maxFrames, double min, double step,
int col_width = DigitWidth( (long int)maxCoord );
// Check if the precision is enough to support the step size.
if (step < 1.0) {
int prec_exp_width = FloatWidth( step );
int prec_exp_width = MinNumFracChars( step );
if (prec_exp_width > col_precision)
col_precision = prec_exp_width;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Whenever a number that precedes <revision> is incremented, all subsequent
* numbers should be reset to 0.
*/
#define CPPTRAJ_INTERNAL_VERSION "V6.27.3"
#define CPPTRAJ_INTERNAL_VERSION "V6.28.0"
/// PYTRAJ relies on this
#define CPPTRAJ_VERSION_STRING CPPTRAJ_INTERNAL_VERSION
#endif

0 comments on commit e7482f3

Please sign in to comment.