Qreal in QVariant



  • 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



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



  • 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!



  • 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.



  • 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....



  • 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



  • 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



  • 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.



  • 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?



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



  • [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?



  • 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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.