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. 32bit Integer cap on 64bit OS
Forum Updated to NodeBB v4.3 + New Features

32bit Integer cap on 64bit OS

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 4.2k Views 1 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.
  • P Offline
    P Offline
    phlucious
    wrote on last edited by
    #1

    I'm using Qt Creator 2.2.0 based on Qt 4.7.4 (32bit) on Windows 7 Enterprise x64. I can't remember where to find my g++ compiler version. I'm also an engineer by training, so the obvious answer to a programmer is probably the right one.

    Given the following code: [edit]Made nrows an int for clarity[/edit]
    @int npts = 71338;
    int nrows = npts*(npts - 1);@
    I always get the result 794,071,610. The expected value is 5,089,038,906.

    Now, I understand that the maximum range of a 32bit integer is 2^32 (or 4,294,967,296). When I subtract that from the expected value using my trusty TI-82, I get the observed result. This suggests to me that this is a type range issue.

    But when I try other data types for nrows @long long, qint64, quint64, int_fast64_t, int_least64_t, __int64@ I get the same result! The only data type I've seen work thus far is a double, but since nrows is a counter I'd prefer to keep it as an integer if possible.

    Anyone have an explanation or solution? Thanks!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      The reason is probably that the compiler is using 32bit ints and you only assign the result to long long or whatever.

      This should work
      @
      long long npts = 71338;
      long long nrows = npts*(npts - 1);
      @

      Sometimes you have to append suffixes. I should be something like:
      @long long nrows = 71338LL * 71337LL; @
      for example.
      Of course, there are numerous other possibilities such as
      @qint64 nrows = qint64 ( 71338 ) * qint64 ( 71337 ) @

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

        This works for me:
        @
        #include <QtCore/QCoreApplication>
        #include <QDebug>

        int main(int argc, char *argv[])
        {
        quint64 val = 71338;
        quint64 result = val * (val - 1);

        qDebug() << result; 
        
        return 0;
        

        }
        @

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

          I cannot reproduce the problem:

          @

          int main(int argc, char *argv[])
          {
          QCoreApplication a(argc, argv);

          int v_int = 71338;
          int v_int_2 = v_int * (v_int-1);
          qDebug() << "int      :" << v_int << " --&gt; " << v_int_2;
          
          long v_long = 71338;
          long v_long_2 = v_long * (v_long-1);
          qDebug() << "long     :" << v_long << " --&gt; " << v_long_2;
          
          long long v_longlong = 71338;
          long long v_longlong_2 = v_longlong * (v_longlong-1);
          qDebug() << "long long:" << v_longlong << " --&gt; " << v_longlong_2;
          
          qint64 v_qint64 = 71338;
          qint64 v_qint64_2 = v_qint64 * (v_qint64-1);
          qDebug() << "qint64   :" << v_qint64 << " --&gt; " << v_qint64_2;
          
          quint64 v_quint64 = 71338;
          quint64 v_quint64_2 = v_quint64 * (v_quint64-1);
          qDebug() << "quint64  :" << v_quint64 << " --&gt; " << v_quint64_2;
          
          return 0;
          

          }
          @

          the output on a 32 bit system is:

          @
          int : 71338 --> 794071610
          long : 71338 --> 794071610
          long long: 71338 --> 5089038906
          qint64 : 71338 --> 5089038906
          quint64 : 71338 --> 5089038906
          @

          The only difference on a 64bit Linux system is that the long version is correct too.

          As koahnig already mentioned, make sure that all variables involved are of the bigger type, otherwise you may suffer from unwanted downcasts.

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

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phlucious
            wrote on last edited by
            #5

            Kaohnig and Volker hit the nail on the head. Because npts was only an int, the computation was downcast regardless of the type I assigned to nrows. Since I can't change the type of npts (it comes from other code... I showed it adjacent for clarity in my post), I had to recast it during the nrows computation:
            @int npts = 71338;
            quint64 nrows = quint64(npts) * ( quint64(npts) - 1 ); //5089038906 as expected@

            Thanks for the help! I knew it was something obvious.

            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