Results 1 to 5 of 5

Thread: float vs double

  1. #1
    Join Date
    Jan 2006
    Posts
    18
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question float vs double

    A possibly stupid question, but I don't understand.

    Take this piece of code:

    Qt Code:
    1. float poo1( 0.1 );
    2. poo1 = poo1 - 0.1;
    3.  
    4. double poo2( 0.1 );
    5. poo2 = poo2 - 0.1;
    6.  
    7. QMessageBox::information( 0, "Help", QString("Float : %1\nDouble : %2").arg( poo1 ).arg( poo2 ) );
    To copy to clipboard, switch view to plain text mode 

    poo1 will return 1.49012e-09
    poo2 will return 0

    Why is that? Shouldn't they both be zero?

    Thanks,
    Royce

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: float vs double

    Hi,

    Don't worry, this is just a numeric rounding problem (nothing to do with QT).

    Not following results :

    Qt Code:
    1. float poo1( 0.1 );
    2. poo1 = poo1 - (double)0.1;
    To copy to clipboard, switch view to plain text mode 
    Give the results you got.

    But with
    Qt Code:
    1. float poo1( 0.1 );
    2. poo1 = poo1 - (float)0.1;
    To copy to clipboard, switch view to plain text mode 
    Give you a perfect 0.

  3. #3
    Join Date
    Jan 2010
    Location
    Bhubaneswar, Odisha
    Posts
    25
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: float vs double

    Internally the compiler takes every decimal value as double.
    Whenever you are substracting a float with double ( ie.. pool - 0.1) the compiler doesn't take pool as 0.1, it take less than 0.1

  4. #4
    Join Date
    Jan 2006
    Posts
    18
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: float vs double

    Thanks for the information. I tried using the examples given in my program but have still come up against a problem.

    In my program I am receiving in certain amounts of stock in less that 1kg quantities. The user enters the amount into a QLineEdit and that amount is then subtracted from the outstanding amount. They then enter another amount up until the point where we have received in all outstanding stock.

    For example I have 1kg outstanding and I receive in 0.9kg and then 0.1kg. I then hit a problem with 0.9kg + 0.1kg not equaling 1kg.

    It looks as if using float or double is a bad idea as it seems to be non-precise. I have included a test piece of code that demonstrates this.

    Qt Code:
    1. QString string1("0.9");
    2. QString string2("0.1");
    3.  
    4. float float1(1);
    5.  
    6. float1 = float1 - (float)string1.toFloat();
    7. float1 = float1 - (float)string2.toFloat();
    8.  
    9. QMessageBox::information( 0, "float1", QString("float1 : %1").arg( float1 ) );
    To copy to clipboard, switch view to plain text mode 

    It gives the result of float1 to be : 2.23517e-08

    How do people normally work around this problem?

    Thanks,
    Royce

  5. #5
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: float vs double

    First, understand the problem. The internal representation of a floating point number in a computer is usually in base 2, and you are entering numbers in base 10. The result is that simple numbers in base 10 (such as 0.1) are not represented exactly by the computer. As a single precision number (a float), this is stored as (3C23D70A) 9.9999998e-3 = 0.0099999998.

    if I told you that I measured my pencil and it is 5.499322234432232 inches long, what is your first thought? Probably, that you do not believe my answer. This is much more accurate than I can measure. My point is that when I perform calculations, I understand that I am only interested in a certain level of accuracy.

    You must decide how many decimal places that you will use, and then, I suggest that you round your answers. This process is easier if you know for certain that the values that you will use are in a specific range (so you can ignore things such as overflow errors).

    As an example, are you able to use something like this:

    Qt Code:
    1. float x = 0.1;
    2. float y = round((x-0.1) * 10000.0) / 10000.0
    To copy to clipboard, switch view to plain text mode 

    This code performs the calculations, multiplies by 10,000, rounds the value, and then divides by 10,000. This should throw away trailing decimals beyond 0.001

Similar Threads

  1. Replies: 2
    Last Post: 24th June 2009, 16:38
  2. Replies: 1
    Last Post: 10th February 2009, 10:42
  3. float a = textedit???
    By Colx007 in forum Qt Programming
    Replies: 3
    Last Post: 10th October 2007, 18:25
  4. How Do I Float a QToolBar?
    By dvmorris in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2007, 20:01
  5. float
    By mickey in forum General Programming
    Replies: 5
    Last Post: 25th July 2006, 12:52

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.