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. What is the difference between qreal and float double
Forum Updated to NodeBB v4.3 + New Features

What is the difference between qreal and float double

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 6 Posters 6.9k 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    I know the difference between float and double .
    Is qreal close to float? Close to double?
    When I looked for qreal, it was declared as a typedef double qreal.

    Does it change to float depending on the OS?

    Christian EhrlicherC VRoninV 2 Replies Last reply
    0
    • S Offline
      S Offline
      stretchthebits
      wrote on last edited by
      #14

      I guess you are wondering why the Qt boys have decided to invent qreal and the other typedefs.
      The reason for this is flexibility.
      Let’s say at some point in the future, some CPU/FPU offers 128 bit precision floating point, and you want to use this, all you have to do is find the line that says
      #typedef qreal double
      and change it to
      #typedef qreal float128

      instead of going through all your source code and changing every single float to a float128.

      Nobody is forcing you to use qreal in your own code.
      Use float or define your own type.

      A lot of libraries do this kind of thing. For example, OpenGL has GLint, GLuint, GLbyte, GLubyte, GLfloat, GLdouble.

      Or, it could be that you are on a system where double does not exist. Graphic cards use to be like that. They only handled float vertices, normal, texture coordinates.

      1 Reply Last reply
      0
      • I IknowQT

        I know the difference between float and double .
        Is qreal close to float? Close to double?
        When I looked for qreal, it was declared as a typedef double qreal.

        Does it change to float depending on the OS?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @IknowQT said in What is the difference between qreal and float double:

        When I looked for qreal, it was declared as a typedef double qreal.

        So this explained what qreal is - a double

        I know the difference between float and double .

        As I said many times - taking a good c++ book would be really helpful for you. These are basic C stuff which you need to learn before starting with c++.
        See the documentation: https://en.cppreference.com/w/cpp/language/types

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • I IknowQT

          I know the difference between float and double .
          Is qreal close to float? Close to double?
          When I looked for qreal, it was declared as a typedef double qreal.

          Does it change to float depending on the OS?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #3

          @IknowQT said in What is the difference between qreal and float double:

          When I looked for qreal, it was declared as a typedef double qreal.

          It's explained in the docs: https://doc.qt.io/qt-5/qtglobal.html#qreal-typedef

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          I 1 Reply Last reply
          3
          • VRoninV VRonin

            @IknowQT said in What is the difference between qreal and float double:

            When I looked for qreal, it was declared as a typedef double qreal.

            It's explained in the docs: https://doc.qt.io/qt-5/qtglobal.html#qreal-typedef

            I Offline
            I Offline
            IknowQT
            wrote on last edited by IknowQT
            #4

            @VRonin
            @Christian-Ehrlicher

            #if defined(QT_COORD_TYPE)
            typedef QT_COORD_TYPE qreal;
            #else
            typedef double qreal;
            #endif
            

            If I want to use it as a float, can I change the typedef double to float and build it?

            Christian EhrlicherC 1 Reply Last reply
            0
            • I IknowQT

              @VRonin
              @Christian-Ehrlicher

              #if defined(QT_COORD_TYPE)
              typedef QT_COORD_TYPE qreal;
              #else
              typedef double qreal;
              #endif
              

              If I want to use it as a float, can I change the typedef double to float and build it?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @IknowQT said in What is the difference between qreal and float double:

              If I want to use it as a float, can I change the typedef double to float and build it?

              You have to recompile Qt then - so no. Use 'float' directly then. What do you think you will gain from using float instead double?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              I 2 Replies Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                @IknowQT said in What is the difference between qreal and float double:

                If I want to use it as a float, can I change the typedef double to float and build it?

                You have to recompile Qt then - so no. Use 'float' directly then. What do you think you will gain from using float instead double?

                I Offline
                I Offline
                IknowQT
                wrote on last edited by IknowQT
                #6

                @Christian-Ehrlicher

                What you're saying sounds like there's no reason to use qreal.
                I wonder why he made qreal

                Christian EhrlicherC kkoehneK 2 Replies Last reply
                0
                • I IknowQT

                  @Christian-Ehrlicher

                  What you're saying sounds like there's no reason to use qreal.
                  I wonder why he made qreal

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @IknowQT said in What is the difference between qreal and float double:

                  What you're saying sounds like there's no reason to use qreal.

                  Why?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @IknowQT said in What is the difference between qreal and float double:

                    If I want to use it as a float, can I change the typedef double to float and build it?

                    You have to recompile Qt then - so no. Use 'float' directly then. What do you think you will gain from using float instead double?

                    I Offline
                    I Offline
                    IknowQT
                    wrote on last edited by IknowQT
                    #8

                    @Christian-Ehrlicher said in What is the difference between qreal and float double:

                    What do you think you will gain from using float instead double?

                    You asked me again what you can achieve by using float instead of double.

                    It is interpreted as saying that there is no big difference between double or float. So, is qreal really necessary? My question was about why I made qreal and what is the difference.

                    Christian EhrlicherC JonBJ 2 Replies Last reply
                    0
                    • I IknowQT

                      @Christian-Ehrlicher said in What is the difference between qreal and float double:

                      What do you think you will gain from using float instead double?

                      You asked me again what you can achieve by using float instead of double.

                      It is interpreted as saying that there is no big difference between double or float. So, is qreal really necessary? My question was about why I made qreal and what is the difference.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by Christian Ehrlicher
                      #9

                      @IknowQT said in What is the difference between qreal and float double:

                      It is interpreted as saying that there is no big difference between double or float.

                      Did you read the documentation link I gave you?

                      Floating-point types
                      
                      The following three types and their cv-qualified versions are collectively called floating-point types.
                      
                          float - single precision floating-point type. Matches IEEE-754 binary32 format if supported. 
                          double - double precision floating-point type. Matches IEEE-754 binary64 format if supported. 
                      

                      And also look at https://en.cppreference.com/w/cpp/language/types#Range_of_values

                      So if you need single-precision you can use float. But for normal usecases this is not needed - your cpu is fast enough (esp. when you need to ask such basic questions).

                      So, is qreal really necessary?

                      Yes because there are architectures where double-precision computing is to slow so you can compile Qt to use single-precision.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      2
                      • I IknowQT

                        @Christian-Ehrlicher said in What is the difference between qreal and float double:

                        What do you think you will gain from using float instead double?

                        You asked me again what you can achieve by using float instead of double.

                        It is interpreted as saying that there is no big difference between double or float. So, is qreal really necessary? My question was about why I made qreal and what is the difference.

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

                        @IknowQT said in What is the difference between qreal and float double:

                        It is interpreted as saying that there is no big difference between double or float.

                        There is a big difference! double holds twice the range of values compared to float, and occupies twice the space to do so --- 8 bytes instead of 4.

                        qreal is, like many things Qt, an "abstraction" from the specifics of typing/choosing between double versus float. Qt uses qreal for everything --- which is usually double, but can be redefined to be float, but then you have to recompile the whole of Qt that way.

                        In addition to @Christian-Ehrlicher's "architectures where double-precision computing is to slow", if you were to have an array of, say, 1,000,000 qreals then you might decide to store it as 1,000,000 floats to save space if you don't need double precision.

                        At the end of all of this: why do you care whether qreal is double or float, and why are you thinking of changing it over to float? Unless you have some compelling reason I would leave well alone and stick with Qt's default.

                        1 Reply Last reply
                        2
                        • I IknowQT

                          @Christian-Ehrlicher

                          What you're saying sounds like there's no reason to use qreal.
                          I wonder why he made qreal

                          kkoehneK Offline
                          kkoehneK Offline
                          kkoehne
                          Moderators
                          wrote on last edited by
                          #11

                          @IknowQT said in What is the difference between qreal and float double:

                          What you're saying sounds like there's no reason to use qreal.
                          I wonder why he made qreal

                          qreal is an abstraction in Qt source code, because on some embedded platforms you might prefer to define qreal as float. On desktop platforms, however, qreal is always defined as double, and is also the only configuration Qt supports (tests in the CI). So, unless you're building Qt yourself for some embedded platforms and know what you're doing, you can expect qreal to be always double, and it is safe to use double in your own code.

                          Director R&D, The Qt Company

                          1 Reply Last reply
                          3
                          • I Offline
                            I Offline
                            IknowQT
                            wrote on last edited by
                            #12

                            @Christian-Ehrlicher
                            @JonB
                            Thanks for both replies.
                            I was just curious about the intention of making qreal. There is a project under development by several people, and I thought it would be better to set one data type, whether to use float or double. A float is enough for us. But qreal is already used in many places, so I needed to clean up this time.

                            JonBJ 1 Reply Last reply
                            0
                            • I IknowQT

                              @Christian-Ehrlicher
                              @JonB
                              Thanks for both replies.
                              I was just curious about the intention of making qreal. There is a project under development by several people, and I thought it would be better to set one data type, whether to use float or double. A float is enough for us. But qreal is already used in many places, so I needed to clean up this time.

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

                              @IknowQT said in What is the difference between qreal and float double:

                              whether to use float or double

                              For a program using Qt, I would suggest you use qreal rather than explicitly choosing either float or double.

                              A float is enough for us.

                              Are you aware that all calculations in C/C++ promote a float to a double before doing any arithmetic? A float may be "enough" for your purposes, but unless you are trying to save space (or in @Christian-Ehrlicher's unusual platform case) you might as well use double/qreal anyway.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                stretchthebits
                                wrote on last edited by
                                #14

                                I guess you are wondering why the Qt boys have decided to invent qreal and the other typedefs.
                                The reason for this is flexibility.
                                Let’s say at some point in the future, some CPU/FPU offers 128 bit precision floating point, and you want to use this, all you have to do is find the line that says
                                #typedef qreal double
                                and change it to
                                #typedef qreal float128

                                instead of going through all your source code and changing every single float to a float128.

                                Nobody is forcing you to use qreal in your own code.
                                Use float or define your own type.

                                A lot of libraries do this kind of thing. For example, OpenGL has GLint, GLuint, GLbyte, GLubyte, GLfloat, GLdouble.

                                Or, it could be that you are on a system where double does not exist. Graphic cards use to be like that. They only handled float vertices, normal, texture coordinates.

                                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