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. QJsonObject insert QJsonValue(double) looses precision
Qt 6.11 is out! See what's new in the release blog

QJsonObject insert QJsonValue(double) looses precision

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

    Hi,

    I have a double value which i want to bring into a QJsonObject. But on inserting the QJsonValue into the QJsonObject the precision of the value changed. But why

    @
    double x = 23.2;
    QJsonObject obj;
    QJsonValue v = QJsonValue(x);
    obj.insert("value",v);

    qDebug() << x;
    qDebug() << v;
    qDebug() << obj;
    

    @

    returns:

    23.2
    QJsonValue(double, 23.2)
    QJsonObject({"value": 23.199999999999999})

    What's going wrong here. Can anybody help me pls?

    Thanks!

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi, and welcome to the Qt Dev Net!

      There are two things you need to be aware of:

      Your loss of precision occured when you assigned 23.2 to x. A

      double, as defined by the "IEEE floating point standard":http://en.wikipedia.org/wiki/IEEE_floating_point, cannot store 23.2 precisely.

      The different outputs you saw is due to "roundings" that qDebug() did before displaying the text. All 3 values are actually the same.

      To check yourself, do this:
      @
      qDebug() << obj;
      qDebug() << obj.value("value");
      qDebug() << obj.value("value").toDouble();
      @

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      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