Results 1 to 7 of 7

Thread: quint64 conversion question

  1. #1
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default quint64 conversion question

    This may be a bit more C++ than Qt but I'm kind of rusty on the casts issues.

    I want to write code that would handle large values and wanted to use uint64.

    Qt Code:
    1. int myIntVal;
    2. double myDoubleVal;
    3. quint64 myResult = myIntVal * myDoubleVal;
    To copy to clipboard, switch view to plain text mode 

    If the result of the multiplication in line 3 is out of the range of int/double would this code work or should I make some modification to assure the value will be correctly stored in the quint64 variable?

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: quint64 conversion question

    The result of the multiplication will probably be an integer before being converted to quint64, so overflow is expected.
    The traditional way is to cast the int to double -- and hope that double is big enought for the result...:
    quint64 myResult = (quint64)((double)myIntVal * myDoubleVal);

  3. The following user says thank you to mvuori for this useful post:

    frankiefrank (20th November 2011)

  4. #3
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: quint64 conversion question

    Is there anything more certain I can do in my code to make sure? Hope is nice but...

  5. #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: quint64 conversion question

    Make sure your input int and double are either both positive or both negative because your result must be positive.

  6. The following user says thank you to ChrisW67 for this useful post:

    frankiefrank (28th November 2011)

  7. #5
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: quint64 conversion question

    Thanks for that tip, I since moved to qint64 so won't have that issue, at least.

  8. #6
    Join Date
    Nov 2011
    Posts
    9
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: quint64 conversion question

    Don't you get casting errors? Conversion from double to uint looses values. You will definetly loose values anyway, you must use clases that manage large numbers. Also they cannot store infinite amout of data

  9. #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: quint64 conversion question

    The compiler certainly will not complain about this code as it is because its behaviour is dependent on its run time inputs. The int will be cast to double, multiplied with the other double, and result truncated to store in the qint64. If the range of the input values is known (checked) then there need not be any issue at all with this code. The compiler may warn if you try something like this:
    Qt Code:
    1. int a(1e45);
    To copy to clipboard, switch view to plain text mode 
    which is detectable at compile time.

Similar Threads

  1. Replies: 1
    Last Post: 12th May 2010, 09:39
  2. Hex Conversion
    By umulingu in forum Newbie
    Replies: 5
    Last Post: 5th March 2010, 18:07
  3. wchar to T50 conversion
    By rajveer in forum General Programming
    Replies: 1
    Last Post: 29th August 2008, 13:32
  4. Pixmap conversion
    By MrShahi in forum Qt Programming
    Replies: 7
    Last Post: 26th June 2008, 10:03
  5. Reg - Conversion of Qt3 to Qt4
    By suresh in forum Qt Programming
    Replies: 10
    Last Post: 28th August 2006, 23:10

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.