[SOLVED] QVariant's .toDouble() returns 0.0 incorrectly
I'm using a QMap<QString, QVariant> to pass in parameters to derived classes so that the same function prototypes may be used throughout the inheritance heirarchy. However, in one instance extracting a double precision floating point value refuses to extract correctly.
in calling code:
QMap<QString, QVariant> somParameters;
somParameters["maxEpochs"] = 40;
somParameters["initialAlpha"] = 0.8;
somParameters["initialRadiusRatio"] = 0.4999;
som->initializeTraining( somParameters, normalizer, inputFeatures.front().size() );
in derived function initializeTraining():
void SOM::initializeTraining( QMap<QString, QVariant> somParameters, NormalizerPtr normalizer, int featureSize )
bool ok = true;
// Describes the number of epochs of training the SOM will need ASSERT_MSG( somParameters.contains( "maxEpochs" ), "SOM parameters doesn't contain epoch count" ); maxEpochs = somParameters["maxEpochs"].toInt( &ok ); ASSERT_MSG( ok, "Couldn't convert maximum epoch count to an int" ); // Describes the inital training weight for the SOM ASSERT_MSG( somParameters.contains( "initialAlpha" ), "SOM parameters doesn't contain initial alpha" ); initialAlpha = somParameters["initialAlpha"].toDouble( &ok ); /// @todo Add range check to alpha (hint on values) /// @todo Perhaps use default values if the parameters aren't specified ASSERT_MSG( ok, "Couldn't convert initial alpha to a double" ); // Describes the inital radius of the training neighborhood for the SOM ASSERT_MSG( somParameters.contains( "initialRadiusRatio" ), "SOM parameters doesn't contain initial radius ratio" ); double initialRadiusRatio = somParameters["initialRadiusRatio"].toDouble( &ok ); ASSERT_MSG( ok, "Couldn't convvert initial radius ratio to a double" ); ASSERT_MSG( initialRadiusRatio < 0.5, "Initial radius ratio may not exceed 0.5" ); ASSERT_MSG( initialRadiusRatio > 0.0, "Initial radius ratio must be greater than 0" );
The crazy part is that the initialRadiusRatio parameter gets extracted correctly, but the initialAlpha variable remains 0. When running through the debugger, I can see that the value of the QVariant mapped by "initialAlpha" shows 0.800000, but it is not extracted and the &ok parametere is not set to false. Does anyone have an idea of what might be going on here?
SOLVED & UPDATE:
This was my own ridiculously stupid mistake. The type of initialAlpha in the base class was int. <facepalm> Sorry for wasting anyone's time.
Do not worry. This happens ;-)
Good to know that your problem is solved. Thanks for sharing the resolution. It is also good to leave your post here. May help other to check out their code again.