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. Qreal in QVariant

Qreal in QVariant

Scheduled Pinned Locked Moved General and Desktop
13 Posts 6 Posters 7.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.
  • J Offline
    J Offline
    Jupiter
    wrote on last edited by
    #1

    Hi,

    in my application i put a qreal in a QVariant.

    how ever in very rare situations (not directly reproduce able) converting it back to a qreal fails.
    f.e.

    @qreal d = 0.0f;
    QVariant v = qVariantFromValue<qreal>(d);
    qreal d2 = v.value<qreal>();@

    when i debug the code d = something like 1.8e-308. so almost 0.0. thats ok.
    but when i step further sometimes d2 is something like 1.8e308 thats something completely different. how can i fix the problem? and it happens not every time. only some times.

    thanks in advance

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      a floating point value is always fuzzy 0, not exact :-)
      The reason is the binary representation of the value.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jupiter
        wrote on last edited by
        #3

        yes i know that. but both values aren't the same the second isn't even fuzzy 0 since the exponent has an positive sign!

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          I've done some testing and I cannot reproduce your problem. You might see some jitter due to the copying in and out from QVariant but not such a flip. There is most probably other broken code interfereing or you your hardware is flipping bits.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on last edited by
            #5

            I've tested the following:

            @ qreal qr = 0.0f;
            QVariant qv = qVariantFromValue<qreal>( qr );
            qreal qr2 = qv.value<qreal>();

            qDebug() << "Original value is " << qr;
            qDebug() << "QVariant value is " << qr2;
            qDebug() << "QVariant value is " << qv.value<qreal>();
            

            @

            and all I see is zero either in the output or in the debugger. I'm running 4.7.4 on linux x86_64. Despite the "real" value, having the exponent to change its sign seems really strange....

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              According to K&R C book, the notation 0.0f defines a float value of zero. qreal in in Qt is usually "a typedef for double on all platforms except for those using CPUs with ARM architectures"[1]. So it may be that you run into a conversion problem at the first line (although I don't believe that's the case here). Do you have any specific reasons to fix the constant to type float? I would leave out the f entirely.

              I just tested this on my Mac here, and the variables always have a value of 0.0. Even if I put 1.8e-308 into d manually, v and d2 it always have this value.

              I suspect something else going wrong here.

              fn1. http://doc.qt.nokia.com/4.7/qtglobal.html#qreal-typedef

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jupiter
                wrote on last edited by
                #7

                well actually the initializing with 0.0f was just a test, first it was 0 then it was 0.0 then it is 0.0f

                i also tested to init it with @memset(&d, 0, sizeof(d));@

                i don't see a problem with my other code, since it is working in like 99% of all cases. and only 1% get messed up. i actually works over a signal slots connection, where the signal takes a QVariant where i put the qreal into, and on the slot the QVariant is converted back to a qreal. at that position it has the wrong sign of the exponent sometimes

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dangelog
                  wrote on last edited by
                  #8

                  Zero is always representable without any rounding error -- and if the code is actually

                  @
                  float f = 0.0f;
                  @

                  there's no math involved, therefore there should be no roundoff errors.

                  I agree with Volker and suspect there's something else going on.

                  Software Engineer
                  KDAB (UK) Ltd., a KDAB Group company

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    Well, can you provide a small example that reproduces your problem or at least any real world code. The one you've posted works like a charm. Have you tried to debug your application to find out when and where your value is actually altered?

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jupiter
                      wrote on last edited by
                      #10

                      well its the same code called like 1000 times. and 1 time the error occurs, so you can't really debug that unfortunately

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        fluca1978
                        wrote on last edited by
                        #11

                        [quote author="Jupiter" date="1320931268"]well its the same code called like 1000 times. and 1 time the error occurs, so you can't really debug that unfortunately[/quote]

                        Could you log the output each time and for each iteration? With, of course, as much information as possible. Could this code be in a method that is called with wrong arguments? Is it possible to have a small example to try?

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          Jupiter
                          wrote on last edited by
                          #12

                          in my code its the following:

                          object A creates object B.
                          B sets its member "member" to 0.0 in the constructor.

                          now when A calls the getter b->member() 1 line after it sometimes receives the unexpected number.
                          there is no other class with access to b.

                          when b sets its member a signal with the variant is emitted. the signal signature is signal(const QVariant& v) so the receivers also can't change it.

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            lgeyer
                            wrote on last edited by
                            #13

                            If you really want any reasonable help you won't get around providing some real world code.

                            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