mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
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:
parent
015f248dd9
commit
13532b0fe4
@ -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
|
||||
|
@ -110,3 +110,39 @@ TEST( SerializeNumbers, SimpleObjectWithFloatValues )
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user