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. QString .toDouble() lose precision
Qt 6.11 is out! See what's new in the release blog

QString .toDouble() lose precision

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    I am convert gpx file point to QPainterPath, when I use QString .toDouble() lose precision.

    QFile file(filePath);
    if(!file.open(QFile::ReadOnly))
        return;
    QDomDocument doc;
    if (!doc.setContent(&file)) {
        file.close();
        return;
    }
    file.close();
    QDomElement root = doc.documentElement();
    QDomNodeList DNL = root.elementsByTagName("trkpt");
    QPainterPath PP;
    for (int i=0; i<DNL.count(); i++) {
        QDomNode DN = DNL.at(i);
        if (DN.isElement()) {
            QDomElement DE = DN.toElement();
            qreal x = DE.attribute("lon").toDouble();
            qreal y = DE.attribute("lat").toDouble();
            qDebug() << DE.attribute("lon") << "," << DE.attribute("lat") << ","  << x << "," << y;
            if (i == 0)
                PP.moveTo(x,y);
            else
                PP.lineTo(x,y);
        }
    }
    QPen pen(Qt::blue, 2);
    scene->addPath(PP, pen);
    

    替代文字

    https://github.com/sonichy

    JonBJ 1 Reply Last reply
    0
    • sonichyS sonichy

      I am convert gpx file point to QPainterPath, when I use QString .toDouble() lose precision.

      QFile file(filePath);
      if(!file.open(QFile::ReadOnly))
          return;
      QDomDocument doc;
      if (!doc.setContent(&file)) {
          file.close();
          return;
      }
      file.close();
      QDomElement root = doc.documentElement();
      QDomNodeList DNL = root.elementsByTagName("trkpt");
      QPainterPath PP;
      for (int i=0; i<DNL.count(); i++) {
          QDomNode DN = DNL.at(i);
          if (DN.isElement()) {
              QDomElement DE = DN.toElement();
              qreal x = DE.attribute("lon").toDouble();
              qreal y = DE.attribute("lat").toDouble();
              qDebug() << DE.attribute("lon") << "," << DE.attribute("lat") << ","  << x << "," << y;
              if (i == 0)
                  PP.moveTo(x,y);
              else
                  PP.lineTo(x,y);
          }
      }
      QPen pen(Qt::blue, 2);
      scene->addPath(PP, pen);
      

      替代文字

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @sonichy
      First, you should be aware that all C++ doubles are approximate values. It cannot represent all the possible floating point values, only certain discrete ones. This is why, for example, you should not compare floating points values for equality with ==, and Qt provides qFuzzyCompare.

      Second, you are using qDebug() << doubleValue. Your output looks like that prints doubles with 6 significant digits. You will have to use a dedicated outputter with more significant digits to see more closely what the double value actually is.

      sonichyS 1 Reply Last reply
      3
      • JonBJ JonB

        @sonichy
        First, you should be aware that all C++ doubles are approximate values. It cannot represent all the possible floating point values, only certain discrete ones. This is why, for example, you should not compare floating points values for equality with ==, and Qt provides qFuzzyCompare.

        Second, you are using qDebug() << doubleValue. Your output looks like that prints doubles with 6 significant digits. You will have to use a dedicated outputter with more significant digits to see more closely what the double value actually is.

        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        @JonB The QString.toDouble() is correct, the problem is the qDebug().

        qDebug() << DE.attribute("lon") << "," << DE.attribute("lat") << ","  << QString("%1").arg(x, 0, 'f', 14) << "," << QString("%1").arg(y, 0, 'f', 14); //approximate
        

        https://github.com/sonichy

        1 Reply Last reply
        1

        • Login

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