Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML and C++, Float Variable not matching
Forum Updated to NodeBB v4.3 + New Features

QML and C++, Float Variable not matching

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 688 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.
  • O Offline
    O Offline
    Olivia Dodonic
    wrote on last edited by
    #1

    Hello :)

    I'm using a QObject to send a signal with a float variable to a QML file. But when I print the the value using console.log, the value doesn't match the precise value. For example, the values 116,804 in C++ becomes 116,8041611 and 116,814 becomes 116,8141632. I thought the reason for this might be the conversion from float to real, but as far as I know both, float and real, are stored in IEEE 754 single precision floating point number.

    The differences are very small and might not be relevant, but the project I work for requires me to explain why exactly the differences occur and proof that they will stay very small for every value.

    Kind regards

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Seems just to be a case of how many digits are printed
      If you do
      qDebug() << qSetRealNumberPrecision( 7 ) << yourfloat;
      on c++ side, dont it match what you see with console.log ?

      O 2 Replies Last reply
      1
      • mrjjM mrjj

        Hi
        Seems just to be a case of how many digits are printed
        If you do
        qDebug() << qSetRealNumberPrecision( 7 ) << yourfloat;
        on c++ side, dont it match what you see with console.log ?

        O Offline
        O Offline
        Olivia Dodonic
        wrote on last edited by
        #3

        @mrjj Thank you for your answer, i will try that and report...

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Seems just to be a case of how many digits are printed
          If you do
          qDebug() << qSetRealNumberPrecision( 7 ) << yourfloat;
          on c++ side, dont it match what you see with console.log ?

          O Offline
          O Offline
          Olivia Dodonic
          wrote on last edited by
          #4

          @mrjj Yes, thank you, if I use qDebug() << qSetRealNumberPrecision( 16 ) << yourfloat; I get a simliar value, but the number of digits varies.
          For example

          std:cout: 0.2
          qDebug: 0.2000000029802322
          qml: 0.20000000298023224

          std:cout: 48.1998
          QDebug: 48.19983673095703
          qml: 48.19983673095703

          Do you maybe know why the number of digits varies?

          mrjjM 1 Reply Last reply
          0
          • O Olivia Dodonic

            @mrjj Yes, thank you, if I use qDebug() << qSetRealNumberPrecision( 16 ) << yourfloat; I get a simliar value, but the number of digits varies.
            For example

            std:cout: 0.2
            qDebug: 0.2000000029802322
            qml: 0.20000000298023224

            std:cout: 48.1998
            QDebug: 48.19983673095703
            qml: 48.19983673095703

            Do you maybe know why the number of digits varies?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Olivia-Dodonic
            Hi
            I think its different default "show values" for how many digits are "printed"
            std::cout has
            http://www.cplusplus.com/reference/iomanip/setprecision/
            to alter it.

            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