1. Novice
Join Date
Jan 2006
Posts
18
Thanks
4
Qt products
Platforms

## float vs double

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

Take this piece of code:

Qt Code:
`float poo1( 0.1 );poo1 = poo1 - 0.1; double poo2( 0.1 );poo2 = poo2 - 0.1; 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. nix
Intermediate user
Join Date
Dec 2008
Location
France
Posts
93
Thanked 23 Times in 22 Posts
Qt products
Platforms

## 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:
`float poo1( 0.1 );poo1 = poo1 - (double)0.1;`
To copy to clipboard, switch view to plain text mode
Give the results you got.

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

3. Novice
Join Date
Jan 2010
Location
Bhubaneswar, Odisha
Posts
25
Thanks
3
Qt products
Platforms

## 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. Novice
Join Date
Jan 2006
Posts
18
Thanks
4
Qt products
Platforms

## 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:
`QString string1("0.9");QString string2("0.1"); float float1(1); float1 = float1 - (float)string1.toFloat();float1 = float1 - (float)string2.toFloat(); 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. Intermediate user
Join Date
Jan 2010
Posts
73
Thanks
6
Thanked 8 Times in 8 Posts
Qt products
Platforms

## 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:
`float x = 0.1;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

#### 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.