Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QVariant's .toDouble() returns 0.0 incorrectly
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QVariant's .toDouble() returns 0.0 incorrectly

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dusktreader
    wrote on last edited by
    #1

    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.

    “Machines take me by surprise with great frequency.” -- Alan Mathison Turing

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved