Results 1 to 8 of 8

Thread: QVector3D, real

  1. #1
    Join Date
    Mar 2011
    Location
    Germany, Munic
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry QVector3D, real

    hi coderz,

    i'm new with Qt and like lot's of this.
    what i do not know is, why Qt mix double/float for QVector3D.

    ...
    private:
    float xp, yp, zp;
    ...
    public:
    qreal length() const;

    float for internal data presentation;
    but real for access.

    what is the reason for mixing float inside / real outside ?
    thank you for answer,
    greetings

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QVector3D, real

    qreal is an abstraction layer. At the end of the day, however, all such abstractions must resolve to actual data.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QVector3D, real

    This is my version:
    qreal is either float / double based on the type of hardware platform used to run the Qt Application, if the platform does not support float then a double is used.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QVector3D, real

    If the platform does not support double (Symbian, Win CE, ARM, or no FPU) then qreal is defined as float.

    QVector3D uses float internally, which means that some precision may be lost and some valid inputs may not be valid internal values on platform that do support double (most). This seems an odd choice. The reasoning may lie with the need to support QDataStream in a platform independent way, by using the lowest common denominator (float). If the internal representation was either float or double based on platform then the data stream would not be portable.

  5. #5
    Join Date
    Mar 2011
    Location
    Germany, Munic
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector3D, real

    thx for answers.
    Portability seems the reason for real.
    I've seen QVector3D from my point of view wich means - mass data - and i do not like to waste memory.
    One day internal presentation will change to double and what will haben with my float arDots[10000000] ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVector3D, real

    It will overflow the stack
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QVector3D, real

    Quote Originally Posted by zaphod77 View Post
    thx for answers.
    Portability seems the reason for real.
    There is no C++ type called "real" there is only float and double: qreal is a typedef for one or the other depending on platform. Both float and double use a standard binary representation (IEEE 754) and could be portable provided both ends of the transfer are expecting the same sized object, which they may not be. "qreal" does make cross-platform binary portability, but it does enhance cross-platform compilability.

    Some platforms have no hardware support for floating point (i.e. no FPU) and have to handle floating point in software. If you couple that with a low powered CPU (Symbian mobile devices, ARM) then floating point is recipe for slow performance, and double doubly so. In a class with a heavy mathematical bent (matrix multiplications) performance is an issue and float makes sense. Having decided to use float on some platforms then you need to use float consistently if you expect binary compatibility of QDataStreams made from QVector3D objects.

    That doesn't explain why the class interfaces are declared in terms of a variable "qreal" type rather than the underlying type. It is possible at the moment to construct a QVector3D with valid double inputs that overflow the internal float storage. This could be a Bad Thing(tm).

    I've seen QVector3D from my point of view wich means - mass data - and i do not like to waste memory.
    No idea what you mean here. If you don't like to waste memory then float is half the size of double (typically at least). Passing a few arguments in as double uses more RAM on the stack for the passed values: this is only temporary. If your environment is that tight that four extra bytes temporarily on the stack are a problem then perhaps you don't want a high-level OO language.
    One day internal presentation will change to double and what will haben with my float arDots[10000000] ?
    Nothing. The size of a float will not change, so your 10 million floats will be the same size.

    If it were QVector3D data[10000000] then the storage required would change.

  8. #8
    Join Date
    Mar 2011
    Location
    Germany, Munic
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector3D, real

    Quote Originally Posted by wysota View Post
    It will overflow the stack
    float arDots[10000000] can be global and stack isn't touched - this is not the point


    Added after 15 minutes:


    I know that you can typedef everything :-)
    Millions of real (double) values are double hungry.
    We need fast acces on FE (Finite Elements) - > http://en.wikipedia.org/wiki/Finite_element_method AND a nice, crossplatform GUI.
    So we need a lot
    Thinking of use our own Vector-classes from previous project.
    Target platforms are win,linux and mac.

    thx for your posts and best code!
    Last edited by zaphod77; 24th March 2011 at 20:04.

Similar Threads

  1. QDomImplementation real dom Document;
    By patrik08 in forum Qt Programming
    Replies: 11
    Last Post: 18th January 2011, 18:38
  2. Formatting real in QML
    By petermcg in forum Qt Quick
    Replies: 1
    Last Post: 27th November 2010, 01:24
  3. Qt Licensing And Programming In The Real
    By rushtontb in forum Qt Programming
    Replies: 11
    Last Post: 10th September 2008, 22:45
  4. Getting real key in keyPressEvent
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 20th March 2008, 15:39
  5. real time plotting
    By gyre in forum Qwt
    Replies: 4
    Last Post: 11th December 2007, 17:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.