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. Strange behaviour with double numbers
QtWS25 Last Chance

Strange behaviour with double numbers

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k Views
  • 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.
  • P Offline
    P Offline
    psuri
    wrote on last edited by
    #1

    Hi All,

    For some reason the QString::toDouble method does not return the right double value of a string. So I decided to write my own toDouble method.. I am trying to get a double number with a 17 digit precision.

    When I run my code, the behaviour is different when I have a qDebug() statement in between.

    I am posting the code below

    ...
    int precision = 0;
    int factor_max = 17;
    if (num != (int) num) {
    int factor = 1;
    bool done = false;
    double curnum = num;
    while (!done) {
    curnum = curnum * 10;
    if (curnum == (int)curnum) {
    qDebug() << "Loop curnum value " << curnum << endl;
    precision = factor;
    done = true;
    }
    else {
    qDebug() << "Loop curnum value " << curnum << endl;
    factor++;
    if (factor == factor_max) {
    precision = factor_max;
    done = true;
    }
    }
    }
    }
    ...

    If I uncomment the qDebug statements, then the code yields correct results if not I get incorrect results..

    I tested it with the following

    "-46.2" returns as -46.2 with the qDebug statements in place

    and comes out as 46.20000000000000284 if I comment the qDebug statements out.. I am puzzled about this.. Does anyone have any idea?

    Thanks in advance for your time,

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexander
      wrote on last edited by
      #2

      See this: "http://bugreports.qt.nokia.com/browse/QTBUG-8239":http://bugreports.qt.nokia.com/browse/QTBUG-8239

      1 Reply Last reply
      0
      • P Offline
        P Offline
        psuri
        wrote on last edited by
        #3

        Thanks! Alexander

        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