From 0634c55431af5b763cdcfb71b6338eb788f7f043 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Mon, 18 Oct 2004 03:00:12 +0000 Subject: [PATCH] fix rounding error in displaying percent score --- stepmania/src/PercentageDisplay.cpp | 18 ++++++++++++------ stepmania/src/RageUtil.h | 7 +++++++ stepmania/src/ScreenEvaluation.cpp | 2 +- stepmania/src/StageStats.cpp | 3 +-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/stepmania/src/PercentageDisplay.cpp b/stepmania/src/PercentageDisplay.cpp index 0b24117962..b5c8edc61a 100644 --- a/stepmania/src/PercentageDisplay.cpp +++ b/stepmania/src/PercentageDisplay.cpp @@ -71,7 +71,11 @@ void PercentageDisplay::Refresh() m_Last = iActualDancePoints; CString sNumToDisplay; - if( !PREFSMAN->m_bDancePointsForOni ) + if( PREFSMAN->m_bDancePointsForOni ) + { + sNumToDisplay = ssprintf( "%*d", DANCE_POINT_DIGITS, max( 0, iActualDancePoints ) ); + } + else { float fPercentDancePoints = m_pSource->GetPercentDancePoints( m_PlayerNumber ); @@ -86,16 +90,18 @@ void PercentageDisplay::Refresh() m_textPercentRemainder.SetText( ssprintf(".%01d%%", iPercentRemainder) ); } else - { - float fNumToDisplay = fPercentDancePoints*100; - sNumToDisplay = ssprintf( "%*.*f%%", PERCENT_TOTAL_SIZE, PERCENT_DECIMAL_PLACES, fNumToDisplay ); + { + // TRICKY: printf will round, but we want to truncate. Otherwise, we may display a percent + // score that's too high and doesn't match up with the calculated grade. + float fTruncInterval = powf(0.1f,PERCENT_TOTAL_SIZE); + fPercentDancePoints = ftruncf( fPercentDancePoints, fTruncInterval ); + + sNumToDisplay = ssprintf( "%*.*f%%", PERCENT_TOTAL_SIZE, PERCENT_DECIMAL_PLACES, fPercentDancePoints*100 ); // HACK: Use the last frame in the numbers texture as '-' sNumToDisplay.Replace('-','x'); } } - else - sNumToDisplay = ssprintf( "%*d", DANCE_POINT_DIGITS, max( 0, iActualDancePoints ) ); m_textPercent.SetText( sNumToDisplay ); } diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index 9806f9ca3f..66b4e38025 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -150,11 +150,18 @@ inline float randomf( const float low=-1.0f, const float high=1.0f ) return RandomFloat( low, high ); } +/* return f rounded to the nearest multiple of fRoundInterval */ inline float froundf( const float f, const float fRoundInterval ) { return int( (f + fRoundInterval/2)/fRoundInterval ) * fRoundInterval; } +/* return f truncated to the nearest multiple of fTruncInterval */ +inline float ftruncf( const float f, const float fTruncInterval ) +{ + return int( (f)/fTruncInterval ) * fTruncInterval; +} + // Move val toward other_val by to_move. void fapproach( float& val, float other_val, float to_move ); diff --git a/stepmania/src/ScreenEvaluation.cpp b/stepmania/src/ScreenEvaluation.cpp index 6e45d7636e..3b572a313e 100644 --- a/stepmania/src/ScreenEvaluation.cpp +++ b/stepmania/src/ScreenEvaluation.cpp @@ -511,7 +511,7 @@ void ScreenEvaluation::Init() /* Use "ScreenEvaluation Percent" for the [metric set], but position and * tween it with "PercentP1X", etc. */ m_Percent[p].SetName( "ScreenEvaluation Percent" ); - m_Percent[p].Load( (PlayerNumber) p, &g_CurStageStats, true ); + m_Percent[p].Load( p, &g_CurStageStats, true ); m_Percent[p].SetXY( THEME->GetMetricF(m_sName, ssprintf("PercentP%dX",p+1)), THEME->GetMetricF(m_sName,ssprintf("PercentP%dY",p+1)) ); m_Percent[p].Command( THEME->GetMetric(m_sName,ssprintf("PercentP%dOnCommand",p+1)) ); diff --git a/stepmania/src/StageStats.cpp b/stepmania/src/StageStats.cpp index 3b43a02d3a..fb2ed6aa80 100644 --- a/stepmania/src/StageStats.cpp +++ b/stepmania/src/StageStats.cpp @@ -197,14 +197,13 @@ Grade StageStats::GetGrade( PlayerNumber pn ) const LOG->Trace( "GetGrade: Actual: %f, Possible: %f", Actual, Possible ); -#define ROUNDING_ERROR 0.00001f Grade grade = GRADE_FAILED; float fPercent = (Possible == 0) ? 0 : Actual / Possible; FOREACH_Grade(g) { - if( fPercent >= PREFSMAN->m_fGradePercent[g]-ROUNDING_ERROR ) + if( fPercent >= PREFSMAN->m_fGradePercent[g] ) { grade = g; break;