How to write qreal literals correctly?



  • How do I write qreal literals, so they do not need to be cast regardless on what CPU architecture I compile?

    E.g. I use qMax

    @
    qreal someValue = calcFunc();
    qreal min = qMin(someValue, 0.0);
    @

    The qMin compiles fine on a PC, but throws a compile error on an ARM platform - no qMin exists, which would allow two different types to be compared.

    Of course I can write the ugly
    @
    qreal min = qMin(someValue, static_cast<qreal>(0.0));
    @

    but I feel there must be a better way to write a qreal-literal.

    Do you know it?



  • Hi,
    In basic this has noting to do with qReal values! It is stated in the docs that a qreal is a double on all platforms excluding the ARM achitecture, there it is a float.
    For the qMin to work you only need a template capable type, and float is just that.
    Do you include the QtGlobal at the top of the file? Maybe the compiler for the PC application runs a bit different then your ARM compiler? That might do the trick.



  • If you read the code above carefully, you'll see that on a PC, it evaluates to
    qMin<double, double>
    whereas on ARM it evaluates to
    qMin<float, double>
    since a literal written like '0.0' is a double.

    If I wrote '0.0f', it would work on ARM but not on the PC.

    I am looking for something like '0.0q', which makes the literal either float or double - depending on the platform.

    Edit: Typo



  • Well, maybe a compiler switch should do the trick.
    @#if Q_PROCESSOR_ARM
    qmin (float, float)
    #else
    qMin(double, double)
    #endif@



  • As I wrote above, it does work when using the static_cast<qreal>, it's just ugly.

    A compiler switch would be even uglier in my book, unless it's something I could do once and then forget about. But I don't see how I would be able to pull off that trick.


Log in to reply
 

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