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. Problem with real values
Forum Updated to NodeBB v4.3 + New Features

Problem with real values

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 6 Posters 1.4k Views 2 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #5

    Hi
    Beware of dragons

    qreal scaleFactorX = 10 / 3; == 3

    qreal scaleFactorX = 10 / 3.0 == 3.33333;

    qreal scaleFactorX = static_cast<float>(10) / 3; == 3.33333;

    the reason is that 2 ints will use the integer division version of operator / so
    even when you assign it to a double the precision is lost.

    1 Reply Last reply
    7
    • clostridium_difficileC clostridium_difficile

      Hello, I divide two integer numbers like this:

      qreal scaleFactorX = ui->workSpace->width() / width;
      

      but in my application it equals 0 (zero!). qDebug prints 0 and debbuger shows it is 0. How to repair it, whats wrong with this code?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #6

      @clostridium_difficile said in Problem with real values:

      qreal scaleFactorX = ui->workSpace->width() / width;

      qreal scaleFactorX = ((qreal)ui->workSpace->width()) / width;

      J.HilkJ 1 Reply Last reply
      3
      • JonBJ JonB

        @clostridium_difficile said in Problem with real values:

        qreal scaleFactorX = ui->workSpace->width() / width;

        qreal scaleFactorX = ((qreal)ui->workSpace->width()) / width;

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #7

        @JonB
        uuh, c-style cast 😨

        still an upvote as you're technically correct, the best kind of correct.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        JonBJ 1 Reply Last reply
        4
        • J.HilkJ J.Hilk

          @JonB
          uuh, c-style cast 😨

          still an upvote as you're technically correct, the best kind of correct.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #8

          @J-Hilk
          Oh dear, damn! Really, you want me to write:

          qreal scaleFactorX = static_cast<qreal>(ui->workSpace->width()) / width;

          is that right?

          Oh, now I see that's effectively what @mrjj wrote at the end of his examples, I hadn't noticed, sorry.

          kshegunovK 1 Reply Last reply
          0
          • JonBJ JonB

            @J-Hilk
            Oh dear, damn! Really, you want me to write:

            qreal scaleFactorX = static_cast<qreal>(ui->workSpace->width()) / width;

            is that right?

            Oh, now I see that's effectively what @mrjj wrote at the end of his examples, I hadn't noticed, sorry.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #9

            @JonB said in Problem with real values:

            is that right?

            As we are long done with this question anyway, you could use constructor-style cast instead:

            qreal scaleFactorX = ui->workSpace->width() / qreal(width);
            

            (moved it to the end for readability)

            Read and abide by the Qt Code of Conduct

            JonBJ 1 Reply Last reply
            0
            • kshegunovK kshegunov

              @JonB said in Problem with real values:

              is that right?

              As we are long done with this question anyway, you could use constructor-style cast instead:

              qreal scaleFactorX = ui->workSpace->width() / qreal(width);
              

              (moved it to the end for readability)

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #10

              @kshegunov
              Indeed you could. All this fuss effectively over qreal(width) versus (qreal)width ;-)

              kshegunovK 1 Reply Last reply
              0
              • JonBJ JonB

                @kshegunov
                Indeed you could. All this fuss effectively over qreal(width) versus (qreal)width ;-)

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #11

                @JonB said in Problem with real values:

                All this fuss

                There's no fuss. They're exactly the same - a C-style cast.

                Read and abide by the Qt Code of Conduct

                JonBJ 1 Reply Last reply
                0
                • kshegunovK kshegunov

                  @JonB said in Problem with real values:

                  All this fuss

                  There's no fuss. They're exactly the same - a C-style cast.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #12

                  @kshegunov said in Problem with real values:

                  They're exactly the same - a C-style cast.

                  Oh dear! I thought we went through this a while ago. You don't really mean they are exactly the same, do you? Only because it's qreal here? If it were ClassName(variable) it would be an instance creation instead of a cast, wouldn't it?

                  kshegunovK 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @kshegunov said in Problem with real values:

                    They're exactly the same - a C-style cast.

                    Oh dear! I thought we went through this a while ago. You don't really mean they are exactly the same, do you? Only because it's qreal here? If it were ClassName(variable) it would be an instance creation instead of a cast, wouldn't it?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #13

                    @JonB said in Problem with real values:

                    I thought we went through this a while ago.

                    We may've, I don't remember ... and I'm bored so I have nothing better to do than to argue uselessly ... :)

                    If it were ClassName(variable) it would be an instance creation instead of a cast, wouldn't it?

                    This is where C++ really shines ... in its stupidity. It's going to be trying a cast if it can't match a suitable constructor, but if there is a matching constructor you're going to get an anonymous object ... fun, ain't it? And compilers differ in their interpretation ... I've had this:

                    QMutex x;
                    {
                       QMutexLocker(&x);
                    }
                    

                    generate "can't cast to QMutexLocker" error with MSVC, but fly seamlessly through gcc ... that language, go figure ...

                    Read and abide by the Qt Code of Conduct

                    JonBJ 1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      @JonB said in Problem with real values:

                      I thought we went through this a while ago.

                      We may've, I don't remember ... and I'm bored so I have nothing better to do than to argue uselessly ... :)

                      If it were ClassName(variable) it would be an instance creation instead of a cast, wouldn't it?

                      This is where C++ really shines ... in its stupidity. It's going to be trying a cast if it can't match a suitable constructor, but if there is a matching constructor you're going to get an anonymous object ... fun, ain't it? And compilers differ in their interpretation ... I've had this:

                      QMutex x;
                      {
                         QMutexLocker(&x);
                      }
                      

                      generate "can't cast to QMutexLocker" error with MSVC, but fly seamlessly through gcc ... that language, go figure ...

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #14

                      @kshegunov said in Problem with real values:

                      This is where C++ really shines ... in its stupidity. It's going to be trying a cast if it can't match a suitable constructor, but if there is a matching constructor you're going to get an anonymous object ... fun, ain't it?

                      Ah ha! That at least explains it, a bit :) Phew! I thought you were going to shoot me down!

                      What I had in mind is: when I see (Something)something I just know it's a cast (yes, static_cast<> is better). But when I see Something(something) am I looking at a function call, an initializer, a cast, even (yikes!) a macro, ...? So personally I wouldn't use that one for a cast, it has enough other uses already!

                      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