diff --git a/src/StringRoutines.cpp b/src/StringRoutines.cpp index 97d81a92d5..d85a388630 100644 --- a/src/StringRoutines.cpp +++ b/src/StringRoutines.cpp @@ -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 --------------------------------------- diff --git a/src/StringRoutines.h b/src/StringRoutines.h index 8d5cc72ae4..fb5fb7d7a6 100644 --- a/src/StringRoutines.h +++ b/src/StringRoutines.h @@ -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. diff --git a/src/TextFormat.cpp b/src/TextFormat.cpp index 706c2d3e85..93f3ea9b5a 100644 --- a/src/TextFormat.cpp +++ b/src/TextFormat.cpp @@ -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; } diff --git a/src/Version.h b/src/Version.h index e017aaca6a..d1e831b38d 100644 --- a/src/Version.h +++ b/src/Version.h @@ -12,7 +12,7 @@ * Whenever a number that precedes 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