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