Commit 3f256bd1 authored by David Thompson's avatar David Thompson
Browse files

Fix floating-point vtkVariant comparison and test.

In theory, all of these variants should compare as equal:
   vtkVariant flt(0.583f);
   vtkVariant dbl(0.583);
   vtkVariant str("0.583");
Currently, they do not because the 32-bit float is promoted
to a 64-bit double instead of the other way around.
This commit demotes doubles to floats when one operand is a float
and tests the result.

It also enables the `TestVariant.cxx` test, which was in the
repository but not being built or run.

Change-Id: I9f2c2cc04ff1ee6961b92f44045c54797e5fe011
parent c2fff570
......@@ -31,6 +31,7 @@ create_test_sourcelist(Tests ${vtk-module}CxxTests.cxx
TestSystemInformation.cxx
TestUnicodeStringAPI.cxx
TestUnicodeStringArrayAPI.cxx
TestVariant.cxx
TestVariantComparison.cxx
TestWeakPointer.cxx
otherArrays.cxx
......
......@@ -175,6 +175,19 @@ int TestVariant(int, char*[])
}
}
vtkVariant flt(0.583f);
vtkVariant dbl(0.583);
vtkVariant str("0.583");
if (
!(flt == dbl) || flt < dbl || flt > dbl ||
!(str == dbl) || str < dbl || str > dbl ||
!(flt == str) || flt < str || flt > str
)
{
cerr << "Comparison of dissimilar-precision floats failed.\n";
errors++;
}
return errors;
}
......@@ -147,7 +147,17 @@ vtkVariant::operator==(const vtkVariant &other) const
// Fifth: floating point dominates integer types.
if (IsFloatingPoint(this->Type) || IsFloatingPoint(other.Type))
// Demote to the lowest-floating-point precision for the comparison.
// This effectively makes the lower-precision number an interval
// corresponding to the range of double values that get rounded to
// that float. Otherwise, comparisons of numbers that cannot fit in
// the smaller mantissa exactly will never be equal to their
// corresponding higher-precision representations.
if (this->Type == VTK_FLOAT || other.Type == VTK_FLOAT)
{
return this->ToFloat() == other.ToFloat();
}
else if (this->Type == VTK_DOUBLE || other.Type == VTK_DOUBLE)
{
return (this->ToDouble() == other.ToDouble());
}
......@@ -216,7 +226,17 @@ vtkVariant::operator<(const vtkVariant &other) const
}
// Fourth: floating point dominates integer types.
if (IsFloatingPoint(this->Type) || IsFloatingPoint(other.Type))
// Demote to the lowest-floating-point precision for the comparison.
// This effectively makes the lower-precision number an interval
// corresponding to the range of double values that get rounded to
// that float. Otherwise, comparisons of numbers that cannot fit in
// the smaller mantissa exactly will never be equal to their
// corresponding higher-precision representations.
if (this->Type == VTK_FLOAT || other.Type == VTK_FLOAT)
{
return this->ToFloat() < other.ToFloat();
}
else if (this->Type == VTK_DOUBLE || other.Type == VTK_DOUBLE)
{
return (this->ToDouble() < other.ToDouble());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment