What is the difference between qreal and float double
-
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 float128instead 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.
-
@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 -
@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
-
@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?
-
What you're saying sounds like there's no reason to use qreal.
I wonder why he made qreal -
@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?
-
@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.
-
@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.
-
@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 tofloat
, 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 betweendouble
versusfloat
. Qt usesqreal
for everything --- which is usuallydouble
, but can be redefined to befloat
, 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
qreal
s then you might decide to store it as 1,000,000float
s to save space if you don't needdouble
precision.At the end of all of this: why do you care whether
qreal
isdouble
orfloat
, and why are you thinking of changing it over tofloat
? Unless you have some compelling reason I would leave well alone and stick with Qt's default. -
@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 qrealqreal
is an abstraction in Qt source code, because on some embedded platforms you might prefer to defineqreal
asfloat
. On desktop platforms, however,qreal
is always defined asdouble
, 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 expectqreal
to be alwaysdouble
, and it is safe to usedouble
in your own code. -
@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. -
@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 eitherfloat
ordouble
.A float is enough for us.
Are you aware that all calculations in C/C++ promote a
float
to adouble
before doing any arithmetic? Afloat
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 usedouble
/qreal
anyway. -
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 float128instead 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.