Add partial specialization for float and double

In PdmFieldWriter::writeFieldData, the numeric values are converted to string with a selected precision. isEqual() is used when comparing values for field editors in PdmFieldUiCap<FieldType>::valueOptions()
This commit is contained in:
Magne Sjaastad 2024-06-11 11:47:01 +02:00
parent 015f248dd9
commit 13532b0fe4
2 changed files with 78 additions and 0 deletions

View File

@ -195,4 +195,46 @@ public:
} }
}; };
//==================================================================================================
/// Partial specialization for float
//==================================================================================================
template <>
class PdmValueFieldSpecialization<float>
{
public:
static QVariant convert( const float& value ) { return QVariant::fromValue( value ); }
static void setFromVariant( const QVariant& variantValue, float& value ) { value = variantValue.value<float>(); }
static bool isEqual( const QVariant& variantValue, const QVariant& variantValue2 )
{
// See PdmFieldWriter::writeFieldData for the precision used when writing float values
// This function is used when comparing values for field editors in PdmFieldUiCap<FieldType>::valueOptions()
const float epsilon = 1e-6f;
return qAbs( variantValue.value<float>() - variantValue2.value<float>() ) < epsilon;
}
};
//==================================================================================================
/// Partial specialization for double
//==================================================================================================
template <>
class PdmValueFieldSpecialization<double>
{
public:
static QVariant convert( const double& value ) { return QVariant::fromValue( value ); }
static void setFromVariant( const QVariant& variantValue, double& value ) { value = variantValue.value<double>(); }
static bool isEqual( const QVariant& variantValue, const QVariant& variantValue2 )
{
// See PdmFieldWriter::writeFieldData for the precision used when writing double values
// This function is used when comparing values for field editors in PdmFieldUiCap<FieldType>::valueOptions()
const double epsilon = 1e-8;
return qAbs( variantValue.value<double>() - variantValue2.value<double>() ) < epsilon;
}
};
} // End of namespace caf } // End of namespace caf

View File

@ -110,3 +110,39 @@ TEST( SerializeNumbers, SimpleObjectWithFloatValues )
EXPECT_TRUE( diffB < epsilon ); EXPECT_TRUE( diffB < epsilon );
} }
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( SerializeNumbers, TestPresicion )
{
double valueB = 0.1234567890123456789;
QString objectAsText;
{
SimpleObjectWithNumbers obj1;
obj1.m_valueB = valueB;
objectAsText = obj1.writeObjectToXmlString();
}
SimpleObjectWithNumbers obj1;
obj1.readObjectFromXmlString( objectAsText, caf::PdmDefaultObjectFactory::instance() );
// Test the precision of the value serialized to text
// See PdmFieldWriter::writeFieldData for the precision used when writing float values
{
const double epsilon = 1e-15;
auto diff = fabs( obj1.m_valueB - valueB );
EXPECT_FALSE( diff > epsilon );
}
{
const double epsilon = 1e-16;
auto diff = fabs( obj1.m_valueB - valueB );
EXPECT_TRUE( diff > epsilon );
}
}