Skip to content

Commit

Permalink
Merge pull request #7772 from NREL/redefine_round_and_trim_in_terms_o…
Browse files Browse the repository at this point in the history
…f_format

Redefine Trim* and Round* functions in terms of {fmt}
  • Loading branch information
mitchute authored Feb 14, 2020
2 parents a7b2149 + db9ff39 commit 9313a70
Showing 1 changed file with 5 additions and 280 deletions.
285 changes: 5 additions & 280 deletions src/EnergyPlus/General.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include <EnergyPlus/General.hh>
#include <EnergyPlus/InputProcessing/InputProcessor.hh>
#include <EnergyPlus/UtilityRoutines.hh>
#include <EnergyPlus/OutputFiles.hh>
// TODO: move DetermineMinuteForReporting to avoid bringing this one in
#include <EnergyPlus/OutputProcessor.hh>

Expand Down Expand Up @@ -1574,302 +1575,26 @@ namespace General {

std::string TrimSigDigits(Real64 const RealValue, int const SigDigits)
{

// FUNCTION INFORMATION:
// AUTHOR Linda K. Lawrie
// DATE WRITTEN March 2002
// MODIFIED na
// RE-ENGINEERED na

// PURPOSE OF THIS FUNCTION:
// This function accepts a number as parameter as well as the number of
// significant digits after the decimal point to report and returns a string
// that is appropriate.

// METHODOLOGY EMPLOYED:
// na

// REFERENCES:
// na

// USE STATEMENTS:
// na

// USE, INTRINSIC :: IEEE_ARITHMETIC, ONLY : IEEE_IS_NAN ! Use IEEE_IS_NAN when GFortran supports it

// Locals
// FUNCTION ARGUMENT DEFINITIONS:

// FUNCTION PARAMETER DEFINITIONS:
static std::string const NAN_string("NAN");
static std::string const ZEROOOO("0.000000000000000000000000000");
static ObjexxFCL::gio::Fmt fmtLD("*");

// INTERFACE BLOCK SPECIFICATIONS
// na

// DERIVED TYPE DEFINITIONS
// na

// FUNCTION LOCAL VARIABLE DECLARATIONS:

if (std::isnan(RealValue)) return NAN_string;

std::string String; // Working string
if (RealValue != 0.0) {
ObjexxFCL::gio::write(String, fmtLD) << RealValue;
} else {
String = ZEROOOO;
}
std::string::size_type const EPos = index(String, 'E'); // Position of E in original string format xxEyy
std::string EString; // E string retained from original string
if (EPos != std::string::npos) {
EString = String.substr(EPos);
String.erase(EPos);
}
std::string::size_type const DotPos = index(String, '.'); // Position of decimal point in original string
std::string::size_type const SLen = len(String); // Length of String (w/o E part)
bool IncludeDot; // True when decimal point output
if (SigDigits > 0 || EString != "") {
IncludeDot = true;
} else {
IncludeDot = false;
}
if (IncludeDot) {
String.erase(min(DotPos + SigDigits + 1, SLen));
String += EString;
} else {
String.erase(DotPos);
}
return stripped(String);
return format("{:.{}T}", RealValue, SigDigits);
}

std::string TrimSigDigits(int const IntegerValue,
Optional_int_const EP_UNUSED(SigDigits) // ignored
)
{

// FUNCTION INFORMATION:
// AUTHOR Linda K. Lawrie
// DATE WRITTEN March 2002
// MODIFIED na
// RE-ENGINEERED na

// PURPOSE OF THIS FUNCTION:
// This function accepts a number as parameter as well as the number of
// significant digits after the decimal point to report and returns a string
// that is appropriate.

// METHODOLOGY EMPLOYED:
// na

// REFERENCES:
// na

// USE STATEMENTS:
// na

// Locals
// FUNCTION ARGUMENT DEFINITIONS:

// FUNCTION PARAMETER DEFINITIONS:
static ObjexxFCL::gio::Fmt fmtLD("*");

// INTERFACE BLOCK SPECIFICATIONS
// na

// DERIVED TYPE DEFINITIONS
// na

// FUNCTION LOCAL VARIABLE DECLARATIONS:
std::string String; // Working string

ObjexxFCL::gio::write(String, fmtLD) << IntegerValue;
return stripped(String);
return format("{}", IntegerValue);
}

std::string RoundSigDigits(Real64 const RealValue, int const SigDigits)
{

// FUNCTION INFORMATION:
// AUTHOR Linda K. Lawrie
// DATE WRITTEN March 2002
// MODIFIED na
// RE-ENGINEERED na

// PURPOSE OF THIS FUNCTION:
// This function accepts a number as parameter as well as the number of
// significant digits after the decimal point to report and returns a string
// that is appropriate.

// METHODOLOGY EMPLOYED:
// na

// REFERENCES:
// na

// USE STATEMENTS:
// na

// USE, INTRINSIC :: IEEE_ARITHMETIC, ONLY : IEEE_IS_NAN ! Use IEEE_IS_NAN when GFortran supports it

// Locals
// FUNCTION ARGUMENT DEFINITIONS:

// FUNCTION PARAMETER DEFINITIONS:
static std::string const DigitChar("01234567890");
static std::string const NAN_string("NAN");
static std::string const ZEROOOO("0.000000000000000000000000000");
static ObjexxFCL::gio::Fmt fmtLD("*");

// INTERFACE BLOCK SPECIFICATIONS
// na

// DERIVED TYPE DEFINITIONS
// na

// FUNCTION LOCAL VARIABLE DECLARATIONS:

if (std::isnan(RealValue)) return NAN_string;

std::string String; // Working string
if (RealValue != 0.0) {
ObjexxFCL::gio::write(String, fmtLD) << RealValue;
} else {
String = ZEROOOO;
}

std::string::size_type const EPos = index(String, 'E'); // Position of E in original string format xxEyy
std::string EString; // E string retained from original string
if (EPos != std::string::npos) {
EString = String.substr(EPos);
String.erase(EPos);
}

std::string::size_type const DotPos = index(String, '.'); // Position of decimal point in original string
assert(DotPos != std::string::npos);
assert(DotPos > 0); // Or SPos will not be valid
char TestChar(DotPos + SigDigits + 1 < String.length()
? String[DotPos + SigDigits + 1]
: ' '); // Test character (digit) for rounding, if position in digit string >= 5 (digit is 5 or greater) then will round
std::string::size_type const TPos = index(DigitChar, TestChar); // Position of Testchar in Digit string

std::string::size_type SPos; // Actual string position being replaced
if (SigDigits == 0) {
SPos = DotPos - 1;
} else {
SPos = DotPos + SigDigits;
}

if ((TPos != std::string::npos) && (TPos >= 5)) { // Must round to next Digit
char const Char2Rep = String[SPos]; // Character (digit) to be replaced
std::string::size_type NPos = index(DigitChar, Char2Rep); // Position of "next" char in Digit String
std::string::size_type TPos1;
assert(NPos != std::string::npos);
String[SPos] = DigitChar[NPos + 1];
while (NPos == 9) { // Must change other char too
if (SigDigits == 1) {
assert(SPos >= 2u);
TestChar = String[SPos - 2];
if (TestChar == '.') {
assert(SPos >= 3u);
TestChar = String[SPos - 3];
SPos -= 2;
}
if (TestChar == ' ') {
TestChar = '0'; // all 999s
} else if (TestChar == '-') { // Autodesk Added to fix bug for values like -9.9999
assert(SPos >= 3u);
String[SPos - 3] = TestChar; // Shift sign left to avoid overwriting it
TestChar = '0'; // all 999s
}
TPos1 = index(DigitChar, TestChar);
assert(TPos1 != std::string::npos);
assert(SPos >= 2u);
String[SPos - 2] = DigitChar[TPos1 + 1];
} else {
assert(SPos >= 1u);
TestChar = String[SPos - 1];
if (TestChar == '.') {
assert(SPos >= 2u);
TestChar = String[SPos - 2];
--SPos;
}
if (TestChar == ' ') {
TestChar = '0'; // all 999s
} else if (TestChar == '-') { // Autodesk Added to fix bug for values like -9.9999
assert(SPos >= 2u);
String[SPos - 2] = TestChar; // Shift sign left to avoid overwriting it
TestChar = '0'; // all 999s
}
TPos1 = index(DigitChar, TestChar);
assert(TPos1 != std::string::npos);
assert(SPos >= 1u);
String[SPos - 1] = DigitChar[TPos1 + 1];
}
--SPos;
NPos = TPos1;
}
}

bool IncludeDot; // True when decimal point output
if (SigDigits > 0 || EString != "") {
IncludeDot = true;
} else {
IncludeDot = false;
}
if (IncludeDot) {
String.erase(min(DotPos + SigDigits + 1, len(String)));
String += EString;
} else {
String.erase(DotPos);
}

return stripped(String);
return format("{:.{}R}", RealValue, SigDigits);
}

std::string RoundSigDigits(int const IntegerValue,
Optional_int_const EP_UNUSED(SigDigits) // ignored
)
{

// FUNCTION INFORMATION:
// AUTHOR Linda K. Lawrie
// DATE WRITTEN March 2002
// MODIFIED na
// RE-ENGINEERED na

// PURPOSE OF THIS FUNCTION:
// This function accepts a number as parameter as well as the number of
// significant digits after the decimal point to report and returns a string
// that is appropriate.

// METHODOLOGY EMPLOYED:
// na

// REFERENCES:
// na

// USE STATEMENTS:
// na

// Locals
// FUNCTION ARGUMENT DEFINITIONS:

// FUNCTION PARAMETER DEFINITIONS:
static ObjexxFCL::gio::Fmt fmtLD("*");

// INTERFACE BLOCK SPECIFICATIONS
// na

// DERIVED TYPE DEFINITIONS
// na

// FUNCTION LOCAL VARIABLE DECLARATIONS:
std::string String; // Working string

ObjexxFCL::gio::write(String, fmtLD) << IntegerValue;
return stripped(String);
return format("{}", IntegerValue);
}

std::string RemoveTrailingZeros(std::string const &InputString)
Expand Down

7 comments on commit 9313a70

@nrel-bot-3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-MacOS-10.13-clang: OK (1960 of 1961 tests passed, 0 test warnings)

Failures:\n

PTHPFixture Test Summary

  • Passed: 2
  • Failed: 1

Build Badge Test Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-Linux-Ubuntu-18.04-gcc-7.4: OK (1981 of 1981 tests passed, 0 test warnings)

Build Badge Test Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-Linux-Ubuntu-18.04-cppcheck: OK (0 of 0 tests passed, 0 test warnings)

Build Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-Linux-Ubuntu-18.04-custom_check: OK (11 of 11 tests passed, 0 test warnings)

Build Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-Linux-Ubuntu-18.04-gcc-7.4-UnitTestsCoverage-Debug: OK (1284 of 1284 tests passed, 0 test warnings)

Build Badge Test Badge Coverage Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - x86_64-Linux-Ubuntu-18.04-gcc-7.4-IntegrationCoverage-Debug: OK (679 of 680 tests passed, 0 test warnings)

Failures:\n

integration Test Summary

  • Passed: 679
  • Timeout: 1

Build Badge Test Badge Coverage Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

develop (mitchute) - Win64-Windows-10-VisualStudio-16: OK (1961 of 1961 tests passed, 0 test warnings)

Build Badge Test Badge

Please sign in to comment.