Fix floating point rounding issues with scaled parameteres in settings.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17070
This commit is contained in:
Alex Shvartzkop 2024-02-23 14:48:20 +03:00 committed by dsa-t
parent 7eeb1a19cf
commit fdf38256e8
1 changed files with 9 additions and 6 deletions

View File

@ -349,7 +349,8 @@ public:
m_min(), m_min(),
m_max(), m_max(),
m_use_minmax( false ), m_use_minmax( false ),
m_scale( aScale ) m_scale( aScale ),
m_invScale( 1.0 / aScale )
{ } { }
PARAM_SCALED( const std::string& aJsonPath, ValueType* aPtr, ValueType aDefault, PARAM_SCALED( const std::string& aJsonPath, ValueType* aPtr, ValueType aDefault,
@ -360,7 +361,8 @@ public:
m_min( aMin ), m_min( aMin ),
m_max( aMax ), m_max( aMax ),
m_use_minmax( true ), m_use_minmax( true ),
m_scale( aScale ) m_scale( aScale ),
m_invScale( 1.0 / aScale )
{ } { }
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override
@ -368,14 +370,14 @@ public:
if( m_readOnly ) if( m_readOnly )
return; return;
double dval = m_default * m_scale; double dval = m_default / m_invScale;
if( std::optional<double> optval = aSettings->Get<double>( m_path ) ) if( std::optional<double> optval = aSettings->Get<double>( m_path ) )
dval = *optval; dval = *optval;
else if( !aResetIfMissing ) else if( !aResetIfMissing )
return; return;
ValueType val = KiROUND<ValueType>( dval / m_scale ); ValueType val = KiROUND<double, ValueType>( dval * m_invScale );
if( m_use_minmax ) if( m_use_minmax )
{ {
@ -388,7 +390,7 @@ public:
void Store( JSON_SETTINGS* aSettings) const override void Store( JSON_SETTINGS* aSettings) const override
{ {
aSettings->Set<double>( m_path, *m_ptr * m_scale ); aSettings->Set<double>( m_path, *m_ptr / m_invScale );
} }
ValueType GetDefault() const ValueType GetDefault() const
@ -404,7 +406,7 @@ public:
bool MatchesFile( JSON_SETTINGS* aSettings ) const override bool MatchesFile( JSON_SETTINGS* aSettings ) const override
{ {
if( std::optional<double> optval = aSettings->Get<double>( m_path ) ) if( std::optional<double> optval = aSettings->Get<double>( m_path ) )
return *optval == ( *m_ptr * m_scale ); return *optval == ( *m_ptr / m_invScale );
return false; return false;
} }
@ -416,6 +418,7 @@ private:
ValueType m_max; ValueType m_max;
bool m_use_minmax; bool m_use_minmax;
double m_scale; double m_scale;
double m_invScale;
}; };
template<typename Type> template<typename Type>