Round bpm and beat to 3 decimal places using std::round() instead of just truncating to 3 decimal places (fixes discrepancy in GS hash generation)

This commit is contained in:
Michael Votaw
2025-03-23 01:26:45 -07:00
committed by teejusb
parent 25351a2f60
commit 0c1603e15e
3 changed files with 15 additions and 7 deletions
+10
View File
@@ -19,6 +19,7 @@
#include <cstdint>
#include <ctime>
#include <functional>
#include <iomanip>
#include <map>
#include <numeric>
#include <sstream>
@@ -370,6 +371,15 @@ RString FormatNumberAndSuffix( int i )
return NUM_PREFIX.GetValue() + ssprintf("%i", i) + sSuffix;
}
RString NormalizeDecimal(float num)
{
float mult = 1000.0f;
float rounded = std::round(num * mult) / mult;
std::ostringstream os;
os << std::fixed << std::setprecision(3) << rounded;
return os.str();
}
struct tm GetLocalTime()
{
const time_t t = time(nullptr);
+2 -1
View File
@@ -349,7 +349,8 @@ inline RString PrettyPercent( int fNumerator, int fDenominator ) { return Pretty
RString Commify( int iNum );
RString Commify(const RString& num, const RString& sep= ",", const RString& dot= ".");
RString FormatNumberAndSuffix( int i );
/* Round num to 3 decimal places and return as string with 3 decimal places */
RString NormalizeDecimal(float num);
struct tm GetLocalTime();
+3 -6
View File
@@ -32,7 +32,6 @@
#include <algorithm>
#include <cstddef>
#include <vector>
#include <iomanip>
#include <regex>
#include "StepParityGenerator.h"
@@ -805,16 +804,14 @@ void Steps::CalculateGrooveStatsHash(bool forceRecalculate)
TimingData * timingData = this->GetTimingData();
std::vector<TimingSegment *> segments = timingData->GetTimingSegments(SEGMENT_BPM);
std::vector<RString> bpmStrings;
bpmStrings.reserve(segments.size());
for (TimingSegment *segment : segments)
{
BPMSegment *bpmSegment = ToBPM(segment);
float beat = bpmSegment->GetBeat();
float bpm = bpmSegment->GetBPM();
std::ostringstream os;
os << std::fixed << std::setprecision(3) << beat;
os << "=";
os << std::fixed << std::setprecision(3) << bpm;
bpmStrings.push_back(os.str());
RString segmentStr = ssprintf("%s=%s", NormalizeDecimal(beat).c_str(), NormalizeDecimal(bpm).c_str());
bpmStrings.push_back(segmentStr);
}
RString bpmString = join(",", bpmStrings);